| 本帖最后由 周伟斌 于 2025-7-1 11:36 编辑 
 
 实现方式如下: 
   
 1、添加用于操作的列: 
 PS:历史填报表增加列时,需调整回写规则
 
 
 2、设置选择框左父格为ID的单元格 
 PS:避免合并单元格
 
 
 3、全局宏设置 
 也可参照全局宏代码编写私有宏
 function main(spreadsheetReport, isAjaxRefreshCallback) {//console.log(spreadsheetReport, isAjaxRefreshCallback);
 var allCheckBoxCell; //全选单元格
 var detailCheckbocCell; //明细选择单元格
 var idCheckbocCell; //明细ID单元格
 var dataSource; //数据源
 var tablename; //表名
 switch (spreadsheetReport.reportId) {
 case 'I2c91808c0192ea75ea751718019323a528e06a7a':
 allCheckBoxCell = "U2";
 detailCheckbocCell = "U3";
 idCheckbocCell = "B3";
 dataSource = "DS.StrongBiPublic";
 tablename = "labor_cost_adjust_import";
 break;
 default:
 return;
 }
 render(spreadsheetReport, allCheckBoxCell, detailCheckbocCell, idCheckbocCell, dataSource, tablename);
 }
 
 /**
 * 工具栏添加批量删除按钮
 * @param spreadsheetReport 填报表对象
 * @param allCheckBoxCell 全选单元格
 * @param detailCheckbocCell 明细选择单元格
 * @param idCheckbocCell 明细ID单元格
 * @param dataSource 数据源
 * @param tablename 表名
 * @returns
 */
 function render(spreadsheetReport, allCheckBoxCell, detailCheckbocCell, idCheckbocCell, dataSource, tablename) {
 if (!spreadsheetReport._batchDeleteBtn) {
 var input = document.createElement("button");
 input.className = "button-buttonbar icon-queryview-toolbar-button-normal";
 input.innerText = "批量删除";
 input.style.width = "70px";
 input.style.height = "25px";
 input.style.verticalAlign = 'top';
 input.style.marginLeft = "12px";
 var target = spreadsheetReport.elem_space2.parentNode.nextSibling;
 var newBtn = target.parentNode.insertBefore(input, target);
 
 spreadsheetReport.addListener(newBtn, "click", function() {
 doNewButtonClick(spreadsheetReport, dataSource, tablename);
 }, spreadsheetReport);
 
 spreadsheetReport._batchDeleteBtn = newBtn;
 }
 
 try {
 
 //增加全选复选框
 var allCheckBox_td = spreadsheetReport.getCell(allCheckBoxCell); //获取全选单元格
 if (allCheckBox_td) {
 var checkboxElement = allCheckBox_td.querySelector('input[type="checkbox"]');
 
 if (checkboxElement) {
 //console.log('复选框存在1');
 return;
 }
 var _allChceckBtn = document.createElement('input');
 _allChceckBtn.id = 'allCheckBox';
 _allChceckBtn.type = 'checkbox';
 
 //监听处理全选
 _allChceckBtn.addEventListener('change', function() {
 allCheckChange(spreadsheetReport);
 });
 
 allCheckBox_td.insertBefore(_allChceckBtn, allCheckBox_td.firstChild);
 spreadsheetReport._allChceckBtn = _allChceckBtn;
 }
 
 var checkboxCells = spreadsheetReport.getExpandedPositions(detailCheckbocCell); //获checkbox所有单元格
 var idCells = spreadsheetReport.getExpandedPositions(idCheckbocCell); //获ID所有单元格
 spreadsheetReport._allChceckBtns = [];
 for (var i = 0; i < checkboxCells.length; i++) {
 var checkbox_td = spreadsheetReport.getCell(checkboxCells);
 var id_td = spreadsheetReport.getCell(idCells);
 if (checkbox_td && id_td) {
 var id = id_td.innerText; //获取id
 
 var checkbox = document.createElement('input');
 checkbox.type = 'checkbox';
 checkbox.setAttribute('data_id', id);
 
 // 将复选框添加到单元格中
 checkbox_td.appendChild(checkbox);
 spreadsheetReport._allChceckBtns.push(checkbox);
 }
 }
 } catch (error) {
 //新增行会报一个Cannot read properties of undefined (reading 'toUpperCase')忽略处理
 alert('批量删除功能异常请联系管理员');
 console.error('发生异常:', error);
 }
 }
 
 function allCheckChange(spreadsheetReport) {
 if (spreadsheetReport._allChceckBtns) {
 spreadsheetReport._allChceckBtns.forEach(function(checkbox) {
 checkbox.checked = spreadsheetReport._allChceckBtn.checked;
 });
 }
 }
 
 function doNewButtonClick(spreadsheetReport, dataSource, tablename) {
 var del_ids = [];
 spreadsheetReport._allChceckBtns.forEach(function(checkbox) {
 if (checkbox.checked) {
 var id = checkbox.getAttribute('data_id');
 del_ids.push(id);
 }
 });
 if (del_ids.length == 0) {
 alert("请选择要删除的数据!");
 return;
 }
 var modalWindow = jsloader.resolve("freequery.common.modalWindow");
 var flag = confirm("确认删除以下数据吗?\r\n" + JSON.stringify(del_ids));
 if (flag) {
 // 将数组中的字符串格式化为 'x' 格式
 var formattedStrings = del_ids.map(function(str) {
 return "'" + str + "'";
 });
 
 // 使用逗号连接格式化后的字符串
 var result = formattedStrings.join(',');
 
 var util = jsloader.resolve("freequery.common.util");
 var sql = "delete from " + tablename + " where id in (" + result + ")";
 var ret = util.remoteInvoke("DataSourceService", "executeUpdate", [dataSource, sql]); //执行删除数据的sql
 if (ret.succeeded) {
 alert("删除成功");
 } else {
 alert("删除失败");
 }
 spreadsheetReport.doRefresh(); //刷新报表
 }
 }
 
 
 
   
   |