Download with extensions
https://datatables.net/download/
HTML
1 2 3 4 |
<link href="{{BASE_URL}}/plugins/DataTables/datatables.min.css" rel="stylesheet" /> <script src="{{BASE_URL}}/plugins/DataTables/datatables.js"></script> |
Javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
var dt_app = function(){ var $c = null; var $t = null; var datatable = null; function init(){ $c = $("main"); $t = $c.find('#dt_table'); init_actions(); init_dt(); } function init_dt(){ datatable = $t.DataTable({ "searching": true, "info": true, "lengthChange": true, "paging": true, 'serverSide': true, 'processing': true, "order": [[ 0, "desc" ]], "ajax": { 'url': base_url + '/admin/' + controller + '/all', 'data': function(d){ //d.pid = pid; } }, 'columns': [ { 'data': 'id', 'title': 'ID' }, { 'data': 'action', 'title': '動作' }, { 'data': 'd_name', 'title': '設備名稱' }, { 'data': 'alias', 'title': '別名' }, { 'data': 'tag_id', 'title': 'Tag ID' }, { 'data': 'device_id', 'title': '設備編號' }, { 'data': 'created_at', 'title': '建立時間' } ], "drawCallback": function( settings ) { var api = this.api(); //wrap.loaded(settings); loaded(settings); }, 'initComplete': function(settings, json){ //loaded(settings); }, dom: 'lBfrtip', buttons: [ 'copy', { extend: 'csv', title: 'WashHandRecords', bom : true }, { extend: 'excel', title: 'WashHandRecords', bom : true }, //'excel', //{ //extend: 'pdf', //text: 'PDF', //bom : true, //'chartset':'utf-8'}, 'print' ] }); } function loaded(oSettings){ } function refresh(){ if(datatable != null){ datatable.destroy(); } init_dt(); } function init_actions(){ $t.delegate('tbody tr .action.edit', 'click', function(e){ e.preventDefault(); var $this = $(this); var id = $this.data('id'); var params = { id: id } window.location.href = base_url + '/admin/' + controller + '/edit?' + $.param(params); }); $t.delegate('tbody tr .action.delete', 'click', function(e){ e.preventDefault(); var $this = $(this); var id = $this.data('id'); if(confirm('確定刪除這筆資料?') == true){ delete_item(id); } }); } function delete_item(id){ var params = { id: id } //console.log(params); //return; $.ajax({ type: 'post', url: base_url + '/admin/' + controller + '/delete', data: params, dataType: 'json', success: function(data){ if(typeof data.status == 'undefined'){ alert(data); }else if(data.status == 'fail'){ alert(data.message); }else{ //alert('Item is deleted.'); refresh(); } }, error: function(data){ console.log(data); var errors = data.responseJSON; show_response_error(errors); } }); } return { init: init, refresh: refresh } }(); |