Я использую Codegniter Библиотека календаря для отображения календаря на три месяца за один снимок (страницу) с предыдущими и следующими кнопками вверху страницы.
Моя проблема: Работает нормально с кнопкой «Далее», но с кнопкой «Предыдущий» я не могу «уменьшить год».
Т.е. изначально на первой странице отображается календарь с [марта 2019 года по май 2019 года], при нажатии кнопки «Далее» он показывает календарь с [июня 2019 года по август 2019 года]: работает нормально.
Но при нажатии кнопки «Предыдущий» отображается календарь месяцев [февраль 2019, январь 2019, март 2019 ]. Это должно быть декабрь 2018 вместо марта 2019.
Вот мой код:
приложение / контроллеры / Calendar_controller1.php
public function display($year=null,$month=null){
if(!$year){
$year = date('Y');
}
if(!$month){
$month = date('m');
}
$this->load->model("cal_model1");
$data["cur_month_calendar"] = $this->cal_model1->generate($year,$month);
$data["cur_month1_calendar"] = $this->cal_model1->generate($year,$month+1);
$data["cur_month2_calendar"] = $this->cal_model1->generate($year,$month+2);
$data["year"] = $year;
$data["nxt_month"] = $month+2;
$data["prev_month"] = $month;
$this->load->view("cal_view1",$data);
}
public function build_next_display(){
$varCurMonth = $this->input->post('month');
$varCurYear = $this->input->post('year');
$this->load->model("cal_model1");
$data["nxt_month1"] = $this->cal_model1->generate($varCurYear,$varCurMonth+1);
$data["nxt_month2"] = $this->cal_model1->generate($varCurYear,$varCurMonth+2);
$data["nxt_month3"] = $this->cal_model1->generate($varCurYear,$varCurMonth+3);
$data["year"] = $varCurYear;
$data["month"] = $varCurMonth+3;
//$this->load->view("cal_view1",$data);
echo json_encode($data);
}
public function build_prev_display(){
$varCurMonth = $this->input->post('month');
$varCurYear = $this->input->post('year');
$this->load->model("cal_model1");
$data["nxt_month1"] = $this->cal_model1->generate($varCurYear,$varCurMonth-1);
$data["nxt_month2"] = $this->cal_model1->generate($varCurYear,$varCurMonth-2);
$data["nxt_month3"] = $this->cal_model1->generate($varCurYear,$varCurMonth-3);
$data["year"] = $varCurYear;
$data["month"] = $varCurMonth-1;
//$this->load->view("cal_view1",$data);
echo json_encode($data);
}
применение / модели / Cal_model1.php
public $conf = array();
public function __construct(){
$this->conf = array(
'start_day' => 'monday',
//'show_next_prev' => true,
//'next_prev_url' => base_url().'Calendar_Controller/display',
'day_type' => 'short'
);
$this->conf['template'] = '
{table_open}<table border="0" cellpadding="0" cellspacing="0" class="calendar">{/table_open}
{heading_row_start}<tr class="heading">{/heading_row_start}
{heading_previous_cell}<th><a href="{previous_url}"><<</a></th>{/heading_previous_cell}
{heading_title_cell}<th colspan="{colspan}">{heading}</th>{/heading_title_cell}
{heading_next_cell}<th><a href="{next_url}">>></a></th>{/heading_next_cell}
{heading_row_end}</tr>{/heading_row_end}
{week_row_start}<tr class="weekdays">{/week_row_start}
{week_day_cell}<td>{week_day}</td>{/week_day_cell}
{week_row_end}</tr>{/week_row_end}
{cal_row_start}<tr class="days">{/cal_row_start}
{cal_cell_start}<td class="day">{/cal_cell_start}
{cal_cell_start_today}<td>{/cal_cell_start_today}
{cal_cell_start_other}<td class="other-month">{/cal_cell_start_other}
{cal_cell_content}
<div class="day_num">{day}</div>
<div class="content">{content}</div>
{/cal_cell_content}
{cal_cell_content_today}
<div class="day_num highlight">{day}</div>
<div class="content">{content}</div>
{/cal_cell_content_today}
{cal_cell_no_content}
<div class="day_num">{day}</div>
{/cal_cell_no_content}
{cal_cell_no_content_today}
<div class="day_num highlight">{day}</div>
{/cal_cell_no_content_today}
{cal_cell_blank} {/cal_cell_blank}
{cal_cell_other}
<div class="day_num">{day}</div>
{/cal_cel_other}
{cal_cell_end}</td>{/cal_cell_end}
{cal_cell_end_today}</td>{/cal_cell_end_today}
{cal_cell_end_other}</td>{/cal_cell_end_other}
{cal_row_end}</tr>{/cal_row_end}
{table_close}</table>{/table_close}
';
}
public function startsWith($string, $startString){
$len = strlen($startString);
return (substr($string, 0, $len) === $startString);
}
public function get_calendar_data($year,$month){
$varMonthLen = strlen($month);
if($varMonthLen == 1){
$month = '0'.$month;
}
$this->db->select('holiday_date,holiday_name');
$this->db->from('holiday_list');
$this->db->where('country_id',1);
$this->db->like('holiday_date',"$year-$month",'after');
$query=$this->db->get();
//print_r($this->db->last_query());exit;
//print_r($query->result());
$cal_data = array();
foreach($query->result() as $row){
$holiday_date = substr($row->holiday_date,8,2);
$startsWithZero = $this->startsWith($holiday_date,"0");
if($startsWithZero == true){
$holiday_date = str_replace('0','',$holiday_date);
}
$cal_data[$holiday_date] = $row->holiday_name;
}
return $cal_data; //Issue : 01 -09 Dates , If you change , this to 1 -9 , it works
}
public function generate($year,$month){
$this->load->library("calendar",$this->conf);
$cal_data = $this->get_calendar_data($year,$month);
//print_r($cal_data);
return $this->calendar->generate($year,$month,$cal_data);
}
Приложение / просмотров / cal_view1.php
<script src="<?php echo base_url();?>assets/js/jquery.min.js"></script>
<script type="text/javascript">
var year = "<?php echo $year; ?>";
var nxtMonth = "<?php echo $nxt_month; ?>";
var prevMonth = "<?php echo $prev_month; ?>";
$('.previous').click(function(){
//alert("previous");
//alert("year"+year);
//alert("month"+prevMonth);
$.ajax({
url: "<?php echo base_url();?>Calendar_controller1/build_prev_display",
type: "POST",
data: {'month':prevMonth,'year':year},
dataType: "json",
success: function (res) {
console.log(res.nxt_month1);
$('#cur_month_calendar').html('');
$('#cur_month_calendar').html(res.nxt_month1);
$('#cur_month1_calendar').html('');
$('#cur_month1_calendar').html(res.nxt_month2);
$('#cur_month2_calendar').html('');
$('#cur_month2_calendar').html(res.nxt_month3);
year = res.year;
prevMonth = res.month;
},
});
});
$('.next').click(function(){
//alert("next");
//alert("year"+year);
//alert("month"+nxtMonth);
$.ajax({
url: "<?php echo base_url();?>Calendar_controller1/build_next_display",
type: "POST",
data: {'month':nxtMonth,'year':year},
dataType: "json",
success: function (res) {
console.log(res.nxt_month1);
$('#cur_month_calendar').html('');
$('#cur_month_calendar').html(res.nxt_month1);
$('#cur_month1_calendar').html('');
$('#cur_month1_calendar').html(res.nxt_month2);
$('#cur_month2_calendar').html('');
$('#cur_month2_calendar').html(res.nxt_month3);
year = res.year;
nxtMonth = res.month;
},
});
});
</script>
.calendar{
font-family:Arial;
font-size:12px;
}
table.calendar{
margin:auto;
border-collapse:collapse;
}
.calendar .days td{
width:50px;
height:50px;
border:2px solid black;
padding:4px;
vertical-align:top;
background-color:#DEF;
}
.calendar .days td:hover{
background-color:#FFF;
}
.calendar .weekdays td{
border:2px solid black;
padding:2px;
text-align:center;
background-color : #C0C0C0;
}
.calendar .heading th{
text-transform:uppercase;
font-size:16px;
padding:10px;
}
.calendar .highlight{
font-weight:bold;
color:blue;
}
.calendar .content{
width: 50px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
border: 2px solid green;
text-align: center;
}
a {
text-decoration: none;
display: inline-block;
padding: 8px 16px;
}
a:hover {
background-color: #ddd;
color: black;
}
.previous {
background-color: #f1f1f1;
color: black;
}
.next {
background-color: #4CAF50;
color: white;
}
<a href="#" class="previous">« </a>
<a href="#" class="next"> »</a>
<?php //echo $calendar; ?>
<div id="cur_month_calendar">
<?php echo $cur_month_calendar; ?>
</div>
<div id="cur_month1_calendar">
<?php echo $cur_month1_calendar; ?>
</div>
<div id="cur_month2_calendar">
<?php echo $cur_month2_calendar; ?>
</div>