From 90d1542205112454f401c8f55fdd259eaf21178c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B4=A4=E5=BF=83?= <3277200+sentsim@users.noreply.github.com> Date: Sat, 9 Jul 2022 19:04:49 +0800 Subject: [PATCH 1/2] optimizing code --- examples/form.html | 6 ++--- src/modules/form.js | 62 ++++++++++++++++++--------------------------- 2 files changed, 27 insertions(+), 41 deletions(-) diff --git a/examples/form.html b/examples/form.html index e82fae3b..a728226d 100644 --- a/examples/form.html +++ b/examples/form.html @@ -16,7 +16,6 @@ -
@@ -264,9 +263,7 @@


- - - + diff --git a/src/modules/table.js b/src/modules/table.js index cf121321..660f9cde 100644 --- a/src/modules/table.js +++ b/src/modules/table.js @@ -1144,16 +1144,16 @@ layui.define(['laytpl', 'laypage', 'form', 'util'], function(exports){ } }; - //数据合计行 + // 数据合计行 Class.prototype.renderTotal = function(data, totalRowData){ - var that = this - ,options = that.config - ,totalNums = {}; + var that = this; + var options = that.config; + var totalNums = {}; if(!options.totalRow) return; layui.each(data, function(i1, item1){ - //若数据项为空数组,则不往下执行(因为删除数据时,会将原有数据设置为 []) + // 若数据项为空数组,则不往下执行(因为删除数据时,会将原有数据设置为 []) if(layui.type(item1) === 'array' && item1.length === 0) return; that.eachCols(function(i3, item3){ @@ -1171,56 +1171,60 @@ layui.define(['laytpl', 'laypage', 'form', 'util'], function(exports){ var tds = []; that.eachCols(function(i3, item3){ var field = item3.field || i3; + + // 合计数据的特定字段 + var TOTAL_NUMS = totalRowData && totalRowData[item3.field]; - //td 内容 + // td 内容 var content = function(){ - var text = item3.totalRowText || '' - ,decimals = 'totalRowDecimals' in item3 ? item3.totalRowDecimals : 2 - ,thisTotalNum = parseFloat(totalNums[field]).toFixed(decimals) - ,tplData = { + var text = item3.totalRowText || ''; + var decimals = 'totalRowDecimals' in item3 ? item3.totalRowDecimals : 2; + var thisTotalNum = parseFloat(totalNums[field]).toFixed(decimals); + var tplData = { LAY_COL: item3 - } - ,getContent; + }; tplData[field] = thisTotalNum; - //获取自动计算的合并内容 - getContent = item3.totalRow ? (parseTempData.call(that, { + // 获取自动计算的合并内容 + var getContent = item3.totalRow ? (parseTempData.call(that, { item3: item3 ,content: thisTotalNum ,tplData: tplData }) || text) : text; - //如果直接传入了合计行数据,则不输出自动计算的结果 - return totalRowData ? (totalRowData[item3.field] || getContent) : getContent; - }() - ,td = ['' - ,'
' + function(){ var totalRow = item3.totalRow || options.totalRow; - //如果 totalRow 参数为字符类型,则解析为自定义模版 + // 如果 totalRow 参数为字符类型,则解析为自定义模版 if(typeof totalRow === 'string'){ return laytpl(totalRow).render($.extend({ - TOTAL_NUMS: totalNums[field] - ,LAY_COL: item3 + TOTAL_NUMS: TOTAL_NUMS || totalNums[field], + LAY_COL: item3 }, item3)); } return content;