Jquery Datatable

前台js

 1         tabelConfig.sAjaxSource = "<?php echo url; ?>";
 2         tabelConfig.oLanguage = {
 3             "sLengthMenu": "每页显示 _MENU_ 条记录",
 4             "sSearch":"标题检索",
 5             "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
 6             "sInfoFiltered": "",
 7             "sInfoEmpty": "没有数据",
 8             "sZeroRecords": "抱歉, 没有找到"
 9         };
10         tabelConfig.aLengthMenu = [[4, 10, 20, 100], [4, 10, 20, 100]];
11         tabelConfig.aoColumns = [
12             {"mDataProp":"id"},
13             {"mDataProp":"title"},
14             {
15                 "mDataProp":"id",
16                 "mRender":function(data, type, row){
17                     var html = '<div class="visible-md visible-lg hidden-sm hidden-xs action-buttons">';
18                     html +=        '<a class="blue" id="'+data+'" role="button" class="green" data-toggle="modal" href="javascript:void(0)" onclick="aClick(this.id)">';
19                     html +=            '<i class="icon-zoom-in bigger-130">显示详情</i>';
20                     html +=        '</a>';
21                     html +=        '<a class="green" id="'+data+'" role="button" class="green" data-toggle="modal" href="javascript:void(0)" onclick="eClick(this.id)">';
22                     html +=            '<i class="icon-pencil bigger-130">编辑模板</i>';
23                     html +=        '</a>';
24                     html +=        '<a class="red" id="'+data+'" role="button" class="red" data-toggle="modal" href="javascript:void(0)" onclick="dClick(this.id)">';
25                     html +=            '<i class="icon-trash bigger-130">删除模板</i>';
26                     html +=        '</a>';
27               html +=    '</div>';
28                     return html;
29                 }
30             },
31         ];
32         var dt = $('#table_list').dataTable(tabelConfig);
33 
34         //add button
35         $("#add").click( function() {
36             layer.open({
37             type: 2,
38             title: '添加站内信发送模板',
39             maxmin: true,
40             shadeClose: true, //点击遮罩关闭层
41             area : ['800px' , '520px'],
42             content: '<?php echo url;?>'
43           });
44 
45         });
46 
47         //add button
48         function aClick(id){
49           layer.open({
50           type: 2,
51           title: '添加站内信发送模板',
52           maxmin: true,
53           shadeClose: true, //点击遮罩关闭层
54           area : ['800px' , '520px'],
55           content: '<?php echo url;?>?id='+id
56         });
57         }
58 
59         //edit button
60         function eClick(id){
61           layer.open({
62           type: 2,
63           title: '编辑站内信模板',
64           maxmin: true,
65           shadeClose: true, //点击遮罩关闭层
66           area : ['800px' , '520px'],
67           content: '<?php echo url('operation/instation/edit');?>?id='+id
68           });
69         }
70 
71         //delete button
72         function dClick(id){
73           $.post("",{
74               id:id,
75               ci_csrf_token:$("input[name='ci_csrf_token']").val(),
76           },function(data){
77             layer.alert(data, function(index){
78                 window.top.location.href='/operation/instation/list';
79                 layer.close(index);
80             });
81           },'text');
82         }
83 
84 
85     </script>

后台代码

  1   public function getData()
  2     {
  3         $get = $this->input->get('sEcho iTotalRecords iDisplayLength sSearch myCurPage');
  4         if (!isset($get['myCurPage']) || empty($get['myCurPage']) || !is_numeric($get['myCurPage'])) {
  5             $get['myCurPage'] = 1;;
  6         }
  7         $pagenum = 'page_' . $get['myCurPage'];
  8         $pageSize = isset($get['iDisplayLength']) &&
  9         is_numeric($get['iDisplayLength']) &&
 10         $get['iDisplayLength'] > 0 ? $get['iDisplayLength'] : 10;
 11 
 12         $whereSql = array();
 13         if (isset($get['sSearch']) && !empty($get['sSearch']) && $get['sSearch'] != 'undefined') {
 14             if (is_array($search = json_decode($get['sSearch'], true)) && !empty($search)) {
 15 
 16                 //判断没有选中的
 17                 foreach($search as $k=>$v) {
 18                     if(-1 == $v) {
 19                         unset($search[$k]);
 20                     }
 21                 }
 22                 //判断时间类型
 23                 if (isset($search['start_time'])&&!empty($search['start_time'])) {
 24                     if (is_string($search['start_time'])) {
 25                         $whereSql['create_time >= '] = $search['start_time']." 00:00:00";
 26                     }
 27                 } // endif
 28 
 29                 if (isset($search['end_time'])&&!empty($search['end_time'])) {
 30                     if (is_string($search['end_time'])) {
 31                         $whereSql['create_time <= '] = $search['end_time']." 23:59:59";
 32                     }
 33                 } // endif
 34 
 35                 //判断手机类型
 36                 if (isset($search['phone'])&&!empty($search['phone'])) {
 37                     if (is_mobile_num($search['phone'])) {
 38                         $whereSql['phone'] = $search['phone'];
 39                     }
 40                 } // endif
 41                 if (!empty($search['code_type']) && $search['code_type'] != 100) {
 42                     if (is_numeric($search['code_type'])) {
 43                         $whereSql['code_type'] = $search['code_type'];
 44                     }
 45                 } else {
 46                     $search['code_type'] = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 30, 50, 51, 52, 53, 54, 55, 100);
 47                     $whereSql['code_type'] = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 30, 50, 51, 52, 53, 54, 55, 100);
 48 
 49                 }// endif
 50                 // 判断发送状态
 51                 if (isset($search['send_type'])) {
 52                     switch ($search['send_type']) {
 53                         case '0':
 54                             $whereSql['send_type'] = 0; // 发送失败
 55                             break;
 56                         case '1':
 57                             $whereSql['send_type'] = 1; // 发送成功
 58                             break;
 59                         case '2':
 60                             $whereSql['send_type'] = array(0, 1); // 全部
 61                         default:
 62                             $whereSql['send_type'] = array(0, 1); // 全部
 63                             break;
 64                     }
 65                 } else {
 66                     $whereSql['send_type'] = 1;
 67                 } // endif
 68             } else {
 69                 $whereSql['phone' . " like '%" . addslashes($get['sSearch']) . "%'"] = '';
 70             }
 71 
 72     }
 73              $page = $this->Plan_sms->get_page($whereSql, $pagenum, $pageSize, array(
 74                  '$field' => array(
 75                      'id',
 76                      'send_type',
 77                      'create_time',
 78                      'phone',
 79                      'content',
 80                      'user_id',
 81                      'service_type',
 82                      'code_type',
 83                      'reg_ip',
 84                      'errormsg'
 85                  ),
 86                  '$order_by' => array(
 87                      'create_time' => 2,
 88                  ),
 89                  false,
 90                  true
 91              ));
 92 
 93              //返给前台json格式的数据
 94              $result = array(
 95                  'aaData' => $page['data']?$page['data']:'',
 96                  'sEcho' => isset($get['sEcho'])&&is_numeric($get['sEcho']) ? $get['sEcho'] : 0,
 97                  'iTotalRecords'=> isset($page['pagecount'])?$page['pagecount']:0,
 98                  'iTotalDisplayRecords'=>isset($page['count'])?$page['count']:0,
 99                  'myCurPage' => $get['myCurPage'],
100                   'sql' => $this->Plan_sms->db->last_query(),
101              );
102              echo json_encode($result);
103              die();
104     }
原文地址:https://www.cnblogs.com/zhoupufelix/p/6727499.html