来自控制器 Codeigniter 的复选框未显示在数据表 ajax 调用中
checkbox from controller Codeigniter not show in datatables ajax call
我的代码没有什么问题。
我有在数据表中显示数据的代码,我想在第一列中显示复选框 select。我的数据表是由 Codeigniter 中的 ajax 调用和控制器创建的。但是当我加载代码时,复选框不显示,但是 eachether 列可以显示。
这是我的代码
我的控制器
function data_schedule_emp()
{
$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));
$year = $this->input->post("year");
$month = $this->input->post("month");
$dept = $this->input->post("dept");
$pos = $this->input->post("pos");
$date_slct = $year.'-'.$month.'-';
$shifts = $this->m_general->get_data('tb_shift')->result();
$rows = $this->m_employee->list_schedule_emp($date_slct)->result();
if($dept=='99'){
if($pos=='ALL'){
$pin_emp = $this->m_employee->get_detail_emp()->result();
}else{
$pin_emp = $this->m_employee->get_detail_emp($pos)->result();
}
}else{
if($pos=='ALL'){
$pin_emp = $this->m_employee->get_detail_emp($dept)->result();
}else{
$pin_emp = $this->m_employee->get_detail_emp($dept,$pos)->result();
}
}
$total_days = tglakhir($year,$month);
$data = array();
$no = 1;
$all_data = array();
$array_pin = array();
$row = array();
$date = array();
foreach ($rows as $row) {
// this may seem convoluted, take your time to understand
$people[$row->date][$row->id_nip] = $row;
}
foreach($pin_emp AS $q)
{
$array_pin[]=$q->emp_pin;
$details[$q->emp_pin] = $q;
}
foreach($shifts AS $shift_e){
$array_shift[$shift_e->id_shift] = $shift_e;
}
for($m=1; $m <= $total_days; $m++){
if($m<10){
$m = '0'.$m;
}else{
$m = $m;
}
$dates[]=$m;
}
foreach ($array_pin AS $pin){
$x=$bg=$n_shift='';
$date_range = array();
$n_shift = $details[$pin]->emp_shift;
foreach ($dates as $date) {
$x_shift = $n_shift;
$full_date = $year.'-'.$month.'-'.$date;
$day = date('D', strtotime($full_date));
if(($n_shift==1) && ($day=='Fri')){
$x_shift=2;
}if(($day == 'Sun') OR ($day == 'Sat')){
$x_shift=13;
}
$bg = $array_shift[$x_shift]->background;
$label = $array_shift[$x_shift]->code_shift;
$note = $array_shift[$x_shift]->note;
$x='<a class="btn '.$bg.' btn-xs" data-toggle="tooltip" data-placement="top" title="'.$note.'">'.$label.'</a>';
$check='<input type="checkbox">';
if(isset($people[$full_date][$pin]->id_shift_emp)==TRUE){
$get_shift = $people[$full_date][$pin]->id_shift_emp;
$code_shift = $people[$full_date][$pin]->code_shift;
$bg = $array_shift[$get_shift]->background;
$label = $array_shift[$get_shift]->code_shift;
$note = $array_shift[$get_shift]->note;
$x='<a class="btn '.$bg.' btn-xs" data-toggle="tooltip" data-placement="top" title="'.$note.'>'.$code_shift.'</a>';
}
$date_range[]=$x;
}
$data = array(
$check,
$pin,
$details[$pin]->emp_name
);
$no++;
$all_data[]=array_merge($data,$date_range);
}
$output = array(
"draw" => $draw,
"recordsTotal" => $rows,
"recordsFiltered" => $rows,
"data" => $all_data
);
echo json_encode($output);
}
这是我的 ajax 电话
$('#list_schedule_emp').dataTable({
"ajax": {
"url": base_url+'employee/data_schedule_emp',
"type": "POST",
"data": {
"year": $('#year').val(),
"month": $('#month').val(),
"dept": $('#dept').val(),
"pos": $('#pos').val()
}
},
"className": 'select-checkbox',
drawCallback: function() {
$('[data-toggle="tooltip"]').tooltip({
container: 'body'
});
}
});
这是我的观点
<div class="table-responsive">
<input type="hidden" name="year" id="year" value="<?php echo $year; ?>">
<input type="hidden" name="month" id="month" value="<?php echo $month; ?>">
<input type="hidden" name="dept" id="dept" value="<?php echo $dept; ?>">
<input type="hidden" name="pos" id="pos" value="<?php echo $pos; ?>">
<table class="table table-bordered table-striped table-hover" id="list_schedule_emp">
<thead>
<tr>
<th>Select</th>
<th>Pin</th>
<th>Nama</th>
<?php foreach ($date_range AS $date) { ?>
<th><?php echo $date ?></th>
<?php } ?>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
哪里是我的错误?请告诉我。
谢谢大家。
Datatables 提供自动添加复选框作为第一列的功能,请参阅 the docs
或 this similar question on their forums
基本上,完全从您的数据中删除所有复选框创建并将您的初始化代码更改为类似下面的内容应该可行。
注意
您还没有告诉我们您正在使用的数据表的版本,并且它们的 api 这些年来在许多细微的方面发生了变化,因此您可能需要通过阅读特定版本的相关文档来调整此示例.
$('#list_schedule_emp').dataTable({
"ajax": {
"url": base_url+'employee/data_schedule_emp',
"type": "POST",
"data": {
"year": $('#year').val(),
"month": $('#month').val(),
"dept": $('#dept').val(),
"pos": $('#pos').val()
}
},
drawCallback: function() {
$('[data-toggle="tooltip"]').tooltip({
container: 'body'
});
}
columnDefs: [ {
orderable: false,
className: 'select-checkbox',
targets: 0
} ],
select: {
style: 'os',
selector: 'td:first-child'
},
order: [[ 1, 'asc' ]],
});
我的代码没有什么问题。
我有在数据表中显示数据的代码,我想在第一列中显示复选框 select。我的数据表是由 Codeigniter 中的 ajax 调用和控制器创建的。但是当我加载代码时,复选框不显示,但是 eachether 列可以显示。
这是我的代码
我的控制器
function data_schedule_emp()
{
$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));
$year = $this->input->post("year");
$month = $this->input->post("month");
$dept = $this->input->post("dept");
$pos = $this->input->post("pos");
$date_slct = $year.'-'.$month.'-';
$shifts = $this->m_general->get_data('tb_shift')->result();
$rows = $this->m_employee->list_schedule_emp($date_slct)->result();
if($dept=='99'){
if($pos=='ALL'){
$pin_emp = $this->m_employee->get_detail_emp()->result();
}else{
$pin_emp = $this->m_employee->get_detail_emp($pos)->result();
}
}else{
if($pos=='ALL'){
$pin_emp = $this->m_employee->get_detail_emp($dept)->result();
}else{
$pin_emp = $this->m_employee->get_detail_emp($dept,$pos)->result();
}
}
$total_days = tglakhir($year,$month);
$data = array();
$no = 1;
$all_data = array();
$array_pin = array();
$row = array();
$date = array();
foreach ($rows as $row) {
// this may seem convoluted, take your time to understand
$people[$row->date][$row->id_nip] = $row;
}
foreach($pin_emp AS $q)
{
$array_pin[]=$q->emp_pin;
$details[$q->emp_pin] = $q;
}
foreach($shifts AS $shift_e){
$array_shift[$shift_e->id_shift] = $shift_e;
}
for($m=1; $m <= $total_days; $m++){
if($m<10){
$m = '0'.$m;
}else{
$m = $m;
}
$dates[]=$m;
}
foreach ($array_pin AS $pin){
$x=$bg=$n_shift='';
$date_range = array();
$n_shift = $details[$pin]->emp_shift;
foreach ($dates as $date) {
$x_shift = $n_shift;
$full_date = $year.'-'.$month.'-'.$date;
$day = date('D', strtotime($full_date));
if(($n_shift==1) && ($day=='Fri')){
$x_shift=2;
}if(($day == 'Sun') OR ($day == 'Sat')){
$x_shift=13;
}
$bg = $array_shift[$x_shift]->background;
$label = $array_shift[$x_shift]->code_shift;
$note = $array_shift[$x_shift]->note;
$x='<a class="btn '.$bg.' btn-xs" data-toggle="tooltip" data-placement="top" title="'.$note.'">'.$label.'</a>';
$check='<input type="checkbox">';
if(isset($people[$full_date][$pin]->id_shift_emp)==TRUE){
$get_shift = $people[$full_date][$pin]->id_shift_emp;
$code_shift = $people[$full_date][$pin]->code_shift;
$bg = $array_shift[$get_shift]->background;
$label = $array_shift[$get_shift]->code_shift;
$note = $array_shift[$get_shift]->note;
$x='<a class="btn '.$bg.' btn-xs" data-toggle="tooltip" data-placement="top" title="'.$note.'>'.$code_shift.'</a>';
}
$date_range[]=$x;
}
$data = array(
$check,
$pin,
$details[$pin]->emp_name
);
$no++;
$all_data[]=array_merge($data,$date_range);
}
$output = array(
"draw" => $draw,
"recordsTotal" => $rows,
"recordsFiltered" => $rows,
"data" => $all_data
);
echo json_encode($output);
}
这是我的 ajax 电话
$('#list_schedule_emp').dataTable({
"ajax": {
"url": base_url+'employee/data_schedule_emp',
"type": "POST",
"data": {
"year": $('#year').val(),
"month": $('#month').val(),
"dept": $('#dept').val(),
"pos": $('#pos').val()
}
},
"className": 'select-checkbox',
drawCallback: function() {
$('[data-toggle="tooltip"]').tooltip({
container: 'body'
});
}
});
这是我的观点
<div class="table-responsive">
<input type="hidden" name="year" id="year" value="<?php echo $year; ?>">
<input type="hidden" name="month" id="month" value="<?php echo $month; ?>">
<input type="hidden" name="dept" id="dept" value="<?php echo $dept; ?>">
<input type="hidden" name="pos" id="pos" value="<?php echo $pos; ?>">
<table class="table table-bordered table-striped table-hover" id="list_schedule_emp">
<thead>
<tr>
<th>Select</th>
<th>Pin</th>
<th>Nama</th>
<?php foreach ($date_range AS $date) { ?>
<th><?php echo $date ?></th>
<?php } ?>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
哪里是我的错误?请告诉我。
谢谢大家。
Datatables 提供自动添加复选框作为第一列的功能,请参阅 the docs 或 this similar question on their forums
基本上,完全从您的数据中删除所有复选框创建并将您的初始化代码更改为类似下面的内容应该可行。
注意
您还没有告诉我们您正在使用的数据表的版本,并且它们的 api 这些年来在许多细微的方面发生了变化,因此您可能需要通过阅读特定版本的相关文档来调整此示例.
$('#list_schedule_emp').dataTable({
"ajax": {
"url": base_url+'employee/data_schedule_emp',
"type": "POST",
"data": {
"year": $('#year').val(),
"month": $('#month').val(),
"dept": $('#dept').val(),
"pos": $('#pos').val()
}
},
drawCallback: function() {
$('[data-toggle="tooltip"]').tooltip({
container: 'body'
});
}
columnDefs: [ {
orderable: false,
className: 'select-checkbox',
targets: 0
} ],
select: {
style: 'os',
selector: 'td:first-child'
},
order: [[ 1, 'asc' ]],
});