$(document).ready(function () { loadDataTable(); function cleanBtnFun() { if (confirm('確定刪除資料?')) { $.ajax({ url: "/BackEndApi/clean_vote", type: "POST", data: null, success: function (data, textStatus, jqXHR) { if (data.ret == "yes") { $('#myTable').DataTable().ajax.reload(); alert('刪除成功'); } else { alert(data.message); if (data.err_code == "9999") { location.href = "/BackEnd/Index"; } } }, error: function (jqXHR, textStatus, errorThrown) { alert('網路或伺服器發生錯誤,請稍後重試!'); } }); } } function loadDataTable() { var dataTables = { init: function init() { this.bindUIActions(); }, bindUIActions: function bindUIActions() { // event handlers this.table = this.handleDataTables(); // add buttons this.table.buttons().container().appendTo('#dt-buttons').unwrap(); }, handleDataTables: function handleDataTables() { //$('#myTable').append(""); return $('#myTable').DataTable({ dom: '<\'text-muted\'Bif>\n <\'table-responsive\'trl>\n <\'mt-4\'p>', lengthChange: true, lengthMenu: [[25, 50, 100, -1], [25, 50, 100, "All"]], pageLength: 100, buttons: [ //{ // text: '休假設定', // action: function (e, dt, node, config) { // vacationBtnFun(); // } //}, { text: '清除10/10(含)之前紀錄', action: function (e, dt, node, config) { cleanBtnFun(); } } ], language: { paginate: { previous: '', next: '' }, buttons: { copyTitle: 'Data copied', copyKeys: 'Use your keyboard or menu to select the copy command' } }, autoWidth: false, ajax: { url: '/BackEndApi/voted_list', type: 'POST', data: function (d) { Object.assign(d, { ticket_type: '' }); return d; }, dataSrc: 'data' }, rowId: 'vote_sn', deferRender: true, initComplete: function () { $('#myTable').on('click', 'a', function () { buttonClick(this); }); $('#myTable').on('click', 'input[name="selectedRow[]"]', function () { checkboxClick(this); }); }, order: [[1, 'desc']], info: true, search: "搜尋:", searching: true, columns: [{ data: 'dj_name', className: 'align-middle', orderable: false, searchable: true }, { data: 'vote_createdatestr', className: 'align-middle text-left', orderable: true, searchable: true }, { data: 'vote_line_uid', className: 'align-middle text-left', orderable: false, searchable: true }, { data: 'vote_point', className: 'align-middle text-right' }, { data: 'vote_token', className: 'align-middle text-left' }], columnDefs: [ //{ // targets: 0, // className: 'align-middle', // orderable: false, // searchable: false, // render: function render(data, type, row, meta) { // return ""; // //var editRet = ' '; // //if (row.quotationUser_perm == "A" || row.quotationUser_perm == "D" || row.quotationUser_perm == "admin" || row.quotationUser_perm == "pro") { // // editRet = editRet + ' '; // //} // //return editRet; // } //}, { // targets: 1, // orderable: false, // searchable: false, // render: function render(data, type, row, meta) { // return '' + row.company_name + ''; // } //}, { // targets: 2, // orderable: false, // searchable: false, // render: function render(data, type, row, meta) { // return row.company_serial; // } //}, { // targets: 3, // orderable: false, // searchable: false, // render: function render(data, type, row, meta) { // if (row.company_enabled == "Y") { // return ''; // } else { // return ''; // } // } //}, { // targets: 4, // orderable: false, // searchable: false, // render: function render(data, type, row, meta) { // return (new Date(row.company_createdate)).format("yyyy/MM/dd hh:mm:ss"); // } //}, { // targets: 5, // orderable: false, // searchable: false, // render: function render(data, type, row, meta) { // var editRet = ' '; // return editRet; // } //} ], }); }, handleSearchRecords: function handleSearchRecords() { var self = this; $('#table-search, #filterBy').on('keyup change focus', function (e) { var filterBy = $('#filterBy').val(); var hasFilter = filterBy !== ''; var value = $('#table-search').val(); self.table.search('').columns().search('').draw(); if (hasFilter) { self.table.columns(filterBy).search(value).draw(); } else { self.table.search(value).draw(); } }); } } dataTables.init(); } });