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 |
var datatable = { init: function(){ var wrap = this; d_table = $('#datatable').dataTable( { "ajax": function (data, callback, settings) { wrap.get_data(function(res){ callback(res); }); }, "order": [[ 4, "desc" ]], "columnDefs": [ { "targets": [ 12 ], "visible": true, "searchable": false } ], 'language': { 'search': '資料過濾:' }, //Don't use this or event handlers won't be triggered after first page //"initComplete": wrap.loaded, "drawCallback": function( settings ) { var api = this.api(); wrap.loaded(settings); } }); }, get_params: function(){ var params = { from: $('#search_from').val(), to: $('#search_to').val(), order_type: $('#order_type').val(), order_status: $('#order_status').val(), keyword: $('#keyword').val() } return params; }, get_data: function(callback){ var wrap = this; var params = wrap.get_params(); $.get(base_url + 'index.php/api/order/all/', params, function(data){ //console.log(data); var result = {data:[]}; if(typeof data.status == 'undefined'){ alert(d); return result; } if(data.status == 'fail'){ alert(data.message); return result; } callback.call(null, data); }); }, loaded: function(oSettings){ var wrap = datatable; var $table = $('#datatable'); $table.find('tbody tr').unbind().on( 'click', function () { if ( $(this).hasClass('selected') ) { $(this).removeClass('selected'); }else { d_table.$('tr.selected').removeClass('selected'); $(this).addClass('selected'); } //console.log( table.row( this ).data() ); selected_id = wrap.get_id($(this)); //console.log(selected_id); $('#item_id').val(selected_id); }); $table.find('.edit_item').unbind().on( 'click', function (e) { e.preventDefault(); window.location.href = base_url + 'admin/manager/place_order/?action=update&id=' + selected_id; }); $table.find('.sort_up').unbind().on( 'click', function (e) { e.preventDefault(); var cat_id = $('#cat_id').val(); wrap.sort(cat_id, selected_id, 'up'); }); $table.find('.sort_down').unbind().on( 'click', function (e) { e.preventDefault(); var cat_id = $('#cat_id').val(); wrap.sort(cat_id, selected_id, 'down'); }); $table.find('.view').unbind().on( 'click', function (e) { e.preventDefault(); $('#modal_details').modal(); details_app.get(selected_id); }); }, get_id: function($row){ return $row.find('>td:first').text(); }, refresh: function(){ var wrap = this; d_table.fnDestroy(); wrap.init(); }, sort: function(cat_id, selected_id, direction){ var wrap = this; var params = { cat_id: cat_id, id: selected_id, direction: direction, sort_direction: 'desc' } $.ajax(base_url + 'index.php/admin/product/sort', { type: 'POST', data: params, success: function(data, textStatus, jqXHR){ //console.log(data.err_code == 0 && err == ''); //return; if(typeof data.err_code == 'undefined'){ show_msg('Error', data); }else if(data.err_code == 0 && data.err == ''){ //console.log(wrap); //window.location.reload(); wrap.refresh(); }else{ show_error(data.err_code, data.err); } }, error: function(jqXHR, textStatus, errorThrown){ alert('Error:' + jqXHR.responseText); } }); } } |