From 585f1c430118e33c79115145b62b9da9b33d8ef4 Mon Sep 17 00:00:00 2001 From: sunxiaobin89 <470459819@qq.com> Date: Wed, 3 May 2023 18:56:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20treeTable=20=E5=86=85?= =?UTF-8?q?=E9=83=A8=20`formatNumber`=20=E7=9A=84=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E7=BB=86=E8=8A=82=E5=87=8F=E5=B0=91=E4=B8=80=E4=BA=9B=E4=B8=8D?= =?UTF-8?q?=E5=BF=85=E8=A6=81=E7=9A=84=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/treeTable.js | 53 +++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/src/modules/treeTable.js b/src/modules/treeTable.js index fa5b3e07..7e924057 100644 --- a/src/modules/treeTable.js +++ b/src/modules/treeTable.js @@ -10,6 +10,7 @@ layui.define(['table'], function (exports) { var form = layui.form; var table = layui.table; var hint = layui.hint(); + var timer = {}; // 记录定时器 index // api var treeTable = { @@ -119,6 +120,19 @@ layui.define(['table'], function (exports) { var treeOptions = thatOptionsTemp.tree; var childrenKey = treeOptions.customName.children; // 处理属性 + delete options.hasNumberCol; + delete options.hasChecboxCol; + delete options.hasRadioCol; + table.eachCols(null, function (i1, item1) { + if (item1.type === 'numbers') { + options.hasNumberCol = true; + } else if (item1.type === 'checkbox') { + options.hasChecboxCol = true; + } else if (item1.type === 'radio') { + options.hasRadioCol = true; + } + }, thatOptionsTemp.cols) + var parseData = options.parseData; var done = options.done; @@ -788,33 +802,31 @@ layui.define(['table'], function (exports) { expandNode({trElem: trDefaultExpand.first()}, true); }); - treeTable.formatNumber(tableId); + options.hasNumberCol && that.formatNumber(tableId); form.render(null, tableFilterId); } - - /** - * 重新编号 - * @param {String} id 表格id - * @return {Object} layui.treeTable - * */ - treeTable.formatNumber = function (id) { - var that = getThisTable(id); - if(!that) return; - + var formatNumber = function (that) { var options = that.getOptions(); var tableViewElem = options.elem.next(); var num = 0; - layui.each(that.treeToFlat(table.cache[id]), function (i1, item1) { + layui.each(that.treeToFlat(table.cache[options.id]), function (i1, item1) { if (layui.isArray(item1)) { return; } - var itemData = that.getNodeDataByIndex(item1.LAY_DATA_INDEX); + var itemData = that.getNodeDataByIndex(item1[LAY_DATA_INDEX]); itemData['LAY_NUM'] = ++num; - tableViewElem.find('tr[lay-data-index="' + item1.LAY_DATA_INDEX + '"] .laytable-cell-numbers').html(itemData['LAY_NUM']); + tableViewElem.find('tr[lay-data-index="' + item1[LAY_DATA_INDEX] + '"] .laytable-cell-numbers').html(itemData['LAY_NUM']); }) - return treeTable; + } + + Class.prototype.formatNumber = function (id) { + var that = this; + clearTimeout(id); + timer[id] = setTimeout(function () { + formatNumber(that); + }, 10) } // 树表渲染 @@ -1023,16 +1035,17 @@ layui.define(['table'], function (exports) { layui.each(table.cache[id], function (i4, item4) { tableView.find('tr[data-level="0"][lay-data-index="' + item4[LAY_DATA_INDEX] + '"]').attr('data-index', i4); }) - treeTable.formatNumber(id); + options.hasNumberCol && that.formatNumber(id); } /** * 新增数据节点 * @param {String} id 树表id - * @param {String|Number} parentIndex 指定的父节点,如果增加根节点,请设置 parentIndex 为 null 即可 - * @param {Number} index:Number 新节点插入的位置(从 0 开始)index = -1(默认) 时,插入到最后 - * @param {Object|Array} data 新增的节点,单个或者多个 - * @param {Boolean} focus 新增的节点,单个或者多个 + * @param {Object} opts + * @param {String|Number} opts.parentIndex 指定的父节点,如果增加根节点,请设置 parentIndex 为 null 即可 + * @param {Number} opts.index 新节点插入的位置(从 0 开始)index = -1(默认) 时,插入到最后 + * @param {Object|Array} opts.data 新增的节点,单个或者多个 + * @param {Boolean} opts.focus 新增的节点,单个或者多个 * @return {Array} 新增的节点 * */ treeTable.addNodes = function (id, opts) {