var projectTable; var projectRowID; var projectPos; $(document).ready(function () { deptList(); companyList(); loadyearmonth(); var actualDate = new Date(); // convert to actual date var prevDate = new Date(actualDate.getFullYear(), actualDate.getMonth() - 13, actualDate.getDate()); var startTxt = prevDate.getFullYear().toString() + "/" + padding(prevDate.getMonth() + 1, 2); var endTxt = actualDate.getFullYear().toString() + "/" + padding(actualDate.getMonth() + 1, 2); const fpStart = flatpickr("#dateStart", { "locale": "zh_tw", "plugins": [ new monthSelectPlugin({ shorthand: true, //defaults to false dateFormat: "Y/m", //defaults to "F Y" altFormat: "Y M", //defaults to "F Y" theme: "light" // defaults to "light" }) ] }); const fpEnd = flatpickr("#dateEnd", { "locale": "zh_tw", "plugins": [ new monthSelectPlugin({ shorthand: true, //defaults to false dateFormat: "Y/m", //defaults to "F Y" altFormat: "Y M", //defaults to "F Y" theme: "light" // defaults to "light" }) ] }); fpStart.setDate(new Date(startTxt + '/1')) fpEnd.setDate(new Date(endTxt + '/1')) $('#projectSaveBtn').on('click', function () { var company_uid = $('#company_select').val(); var project_name = $('#project_name').val(); var project_ps = $('#project_ps').val(); var project_uid = $('#project_uid').val(); var method = $('#project_method').val(); var dept_uid = $('#dept_select').val(); var msg = ''; if (company_uid == "") { msg += '請選擇客戶公司!\n'; } if (project_name == '') { msg += '請輸入專案名稱!\n'; } if (msg != '') { alert(msg); return; } var formData = { method: method, project_uid: project_uid, project_name: project_name, company_uid: company_uid, project_ps: project_ps, dept_uid: dept_uid } $.ajax({ url: "/Api/addEditDelGetProject", type: "post", data: formData, success: function (data, textStatus, jqXHR) { if (data.ret == "yes") { var obj = data.projectViews[0]; if (method == "add") { projectTable.fnAddData(obj); } if (method == "edit") { projectTable.fnUpdate(obj, projectRowPos); } $('#clientProjectModal').modal('toggle'); } else { alert(data.message); if (data.err_code == "99999") { location.href = "/Root/Login"; } } }, error: function (jqXHR, textStatus, errorThrown) { alert('網路或伺服器發生錯誤,請稍後重試!'); } }); }); $('#dept_select').on('change', function () { $('#dt-responsive').DataTable().ajax.reload(); }); $('#projectNewBtn').on('click', function () { $('#project_method').val('add'); $('#clientProjectModal').modal("toggle"); }); function loadyearmonth() { var actualDate = new Date(); // convert to actual date var nowYear = actualDate.getFullYear(); for (tmpY = nowYear; tmpY >= 2019; tmpY--) { $("#project_year").append($("").attr("value", tmpY).text(tmpY + " 年度")); } for (tmpM = 1; tmpM <= 12; tmpM++) { $("#project_month").append($("").attr("value", tmpM).text(tmpM + " 月")); } } }); function buttonClick(obj) { var type = obj.getAttribute('data-method'); var uid = obj.getAttribute('data-uid'); var dept_uid = $('#dept_select').val(); projectRowID = $('#' + uid); projectRowPos = projectTable.fnGetPosition($('#' + uid)[0]); if (type == "edit") { var formData = { method: 'get', project_uid: uid, dept_uid: dept_uid } $.ajax({ url: "/Api/addEditDelGetProject", type: "post", data: formData, success: function (data, textStatus, jqXHR) { if (data.ret == "yes") { var obj = data.projectViews[0]; $("#project_method").val('edit'); $("#project_uid").val(obj.project_uid); $("#project_name").val(obj.project_name).trigger('change'); $("#company_select").val(obj.company_uid); $("#project_ps").val(obj.project_ps).trigger('change'); $('#clientProjectModal').modal('toggle'); } else { alert(data.message); if (data.err_code == "99999") { location.href = "/Root/Login"; } } }, error: function (jqXHR, textStatus, errorThrown) { alert('網路或伺服器發生錯誤,請稍後重試!'); } }); } if (type == "del") { if (confirm('確定刪除此筆專案資料? 所有此專案的報價單項目也會一併刪除~')) { if (confirm('再次確認是否刪除所有此專案與報價單項目?')) { var formData = { method: 'del', project_uid: uid, dept_uid: dept_uid } $.ajax({ url: "/Api/addEditDelGetProject", type: "del", data: formData, success: function (data, textStatus, jqXHR) { if (data.ret == "yes") { var row = projectTable.api().row(projectRowID).remove().draw(false); alert('刪除成功'); } else { alert(data.message); if (data.err_code == "99999") { location.href = "/Root/Login"; } } }, 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 $('#dt-responsive').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: 50, buttons: [ //{ // text: '休假設定', // action: function (e, dt, node, config) { // vacationBtnFun(); // } //}, //'excelHtml5' ], language: { paginate: { previous: '', next: '' }, buttons: { copyTitle: 'Data copied', copyKeys: 'Use your keyboard or menu to select the copy command' } }, autoWidth: false, ajax: { url: '/Api/projectViewList', type: 'POST', data: function (d) { Object.assign(d, { dept_uid: $('#dept_select').val(), start_txt: $('#dateStart').val(), end_txt: $('#dateEnd').val() }); return d; }, dataSrc: 'projectViews' }, rowId: 'project_uid', deferRender: true, initComplete: function () { projectTable = $('#dt-responsive').dataTable(); $('#dt-responsive').on('click', 'a', function () { buttonClick(this); }); $('#dt-responsive').on('click', 'button', function () { buttonClick(this); }); }, order: [[0, 'desc']], info: true, search: "搜尋:", searching: true, columns: [ { data: 'project_datetime', className: 'align-top text-left', orderable: true, searchable: true }, { data: 'project_name', className: 'align-top text-left', orderable: false, searchable: true }, { data: 'company_name', className: 'align-top text-left', orderable: false, searchable: true }, { data: 'project_ps', className: 'align-top text-left', orderable: false, searchable: false }, { data: 'project_uid', className: 'align-top text-center', orderable: false, searchable: false } ], columnDefs: [ { targets: 1, className: 'align-middle text-left', orderable: false, searchable: true, render: function render(data, type, row, meta) { return '' + row.project_name + ''; } }, { targets: 4, orderable: false, searchable: false, render: function render(data, type, row, meta) { var ret = ''; ret += ''; ret += ''; return ret; } } ], responsive: { details: { display: $.fn.dataTable.Responsive.display.childRowImmediate, type: '' } } }); }, 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(); } function companyList() { $.ajax({ url: "/Api/companyList", type: "post", data: null, success: function (data, textStatus, jqXHR) { if (data.ret == "yes") { var obj = data.companys; var items = ""; $("#company_select").append($("