(function ($) { "use strict"; var Table = function (selector) { this.$selector = $(selector); this.selector = selector; this.table; this.selectedData; this.onDataTableClick; this.onInit; this.idColumnIndex; this.selectedIds = []; }; Table.prototype.updateSelectedIds = function(element, isSelected) { if (this.idColumnIndex === undefined) { return; } let idColumnName = this.columns[this.idColumnIndex].data; let id = this.table.row(element).data()[idColumnName]; if (isSelected) { if (!this.selectedIds.includes(id) && id !== null) { this.selectedIds.push(id); } } else { this.selectedIds = this.selectedIds.filter(item => item !== id); } }; Table.prototype.onToggleCheckAll = function (element) { if (!element) return; let tr = this.table.rows().nodes(); let isChecked = $(element).prop("checked"); console.log(isChecked); let allData = this.table.rows().data().toArray(); if (isChecked) { this.selectedIds = allData .map(row => row[this.columns[this.idColumnIndex].data]) .filter(email => email && email.trim() !== ""); } else { this.selectedIds = []; } if (isChecked) { this.table.rows().select(); } else { this.table.rows().deselect(); } let checkboxes = this.table.$('input[type="checkbox"]'); checkboxes.prop("checked", isChecked); // $.each(tr, function () { // $(this).find('input[type="checkbox"]').prop("checked", isChecked); // }); if ($.isFunction(this.onAfterUpdate)) { this.onAfterUpdate(); } }; Table.prototype.isAllOrZeroRowsSelected = function () { var totalRows = this.table.rows().count(); var selectedRows = this.table.rows({ selected: true }).count(); return totalRows === selectedRows || selectedRows === 0; }; Table.prototype.onToggleCheckbox = function (element) { var tr = $(element); var checkbox = tr.find('input[type="checkbox"]'); var isChecked = tr.hasClass("selected"); checkbox.prop("checked", isChecked); this.updateSelectedIds(element, isChecked); if ($.isFunction(this.onAfterUpdate)) { this.onAfterUpdate(); } }; Table.prototype.onRowClick = function (element, multi) { var tr = $(element); this.selectedData = this.table.row(tr).data(); if (tr.hasClass("selected")) { tr.removeClass("selected"); if ($.isFunction(this.onDeselect)) { this.onDeselect(this.selectedData, element); } // this.updateSelectedIds(element, false); this.selectedData = null; } else { if (!multi) { this.table.$("tr.selected").removeClass("selected"); } tr.addClass("selected"); if ($.isFunction(this.onDataTableClick)) { this.onDataTableClick(this.selectedData, element); } // this.updateSelectedIds(element, true); } }; Table.prototype.dataTableExtendBuild = function (params) { var responsive = { details: { renderer: function (api, rowIdx, columns) { var outer = ""; var data = $.map(columns, function (col, i) { return col.hidden ? `
${col.title}
${col.data}
` : ``; }).join(``); outer += data; outer += ""; return outer ? $("").append(outer) : false; } } }; var tableBuildParams = $.extend(true, {}, params, { responsive: responsive }); this.dataTableBuild(tableBuildParams); }; Table.prototype.dataTableBuild = function (params) { var self = this; var defaults = { data: [], stateSave: true, destroy: true, processing: true, serverSide: false, scrollX: true, responsive: false, columns: this.columns, rowsGroup: null, columnDefs: [], isAddCheckbox: false, isDeleteUtil: false, select: {}, pagingType: "simple_numbers", oLanguage: { oPaginate: { sNext: '', sPrevious: '' } }, order: [[0, "asc"]], buttons: [ "copy", { extend: "csv", charset: "UTF-8", bom: true }, "excel", "print", { extend: "pdfHtml5", orientation: "landscape", pageSize: "LEGAL" } ], language: { processing: "", loadingRecords: ' 데이터를 처리 중입니다.' } }; if (params.idColumnIndex !== undefined) { this.idColumnIndex = params.idColumnIndex; } var tableBuildParams = $.extend({}, defaults, params); tableBuildParams.initComplete = function () { var dataTableLength = $(".dataTables_length").find("select:eq(0)"); var tableWrapper = $(self.selector + "_wrapper"); dataTableLength.addClass("darkBack"); dataTableLength.children().css("background", "#3B3D40"); dataTableLength.css("border-radius", "5px"); dataTableLength.css("min-height", "30px"); tableWrapper.css("border-top", "1px solid rgba(51, 51, 51, 0.3)"); tableWrapper.css("padding-top", "5px"); tableWrapper.find("input[type=search]").css("width", "80px"); if (tableBuildParams.isDeleteUtil) { $(self.selector + "_length").remove(); $(self.selector + "_filter").remove(); $(self.selector + "_info").remove(); $(self.selector + "_paginate").remove(); } if ($.isFunction(self.onInit)) { self.onInit(); } }; if (tableBuildParams.isAddCheckbox) { tableBuildParams.columnDefs = [ { targets: 0, orderable: false, searchable: false, data: null, defaultContent: "", className: "select-checkbox" }, { targets: 1, aDataSort: [1] } ].concat(tableBuildParams.columnDefs); tableBuildParams.columns = [ { data: null, title: '', render: function () { return ''; }, className: "dt-body-center" } ].concat(tableBuildParams.columns); tableBuildParams.select = { style: "multi" }; } this.table = this.$selector.DataTable(tableBuildParams); $(this.selector + " tbody").on("click", "tr", function () { self.onRowClick(this, tableBuildParams.isAddCheckbox); if (tableBuildParams.isAddCheckbox) { self.onToggleCheckbox(this); } }); if (tableBuildParams.isAddCheckbox) { $(this.selector + "_wrapper .dataTables_scrollHead thead").on("change", 'input[name="checkall"]', function () { self.onToggleCheckAll(this); }); } $.fn.dataTable.ext.errMode = function () { jError("Notification : Ajax Error, retry plz !"); }; }; Table.prototype.clear = function () { this.table.clear(); this.selectedIds = []; }; Table.prototype.addRows = function (rows) { this.table.rows.add(rows).draw(false); }; Table.prototype.reDraw = function (rows) { this.selectedIds = []; this.selectedData = null; this.clear(); this.addRows(rows); }; Table.prototype.empty = function () { this.table.destroy(); this.$selector.empty(); }; Table.prototype.removeRows = function (callback) { var selectedRows = this.table.rows({ selected: true }); var selectedDatas = selectedRows.data().toArray(); selectedRows.remove().draw(); if ($.isFunction(callback)) { callback(selectedDatas); } }; Table.prototype.getSelectedDataList = function () { return this.table.rows({ selected: true }).data().toArray(); }; Table.prototype.getDatas = function () { return this.table.rows().data().toArray(); }; Table.prototype.getSelectedIds = function () { return this.selectedIds; }; $.fn.Table = Table; })(jQuery);