hdw
2018-12-26 b040ac179af00dfe38204ebb708aed8c87f285ad
gx_tieta/WebRoot/pages/js/mylayui.js
@@ -389,3 +389,68 @@
    }
};
//layui表格内容更新
var LayuiTbl = function(options, layui, laytpl, cache) {
    this.opts = options;
    this.cache = cache;
    this.layui = layui;
    this.tpl = laytpl;
    this.ele = options.elem;
    this.col = options.cols[0];
};
// 设置原型函数
LayuiTbl.prototype = {
    updateTr: function(index, data) {
        var layui = this.layui;
        var preData = this.cache[index];
        var _data = $.extend(preData, data||{});
        var tds = this._getBodyTds(index);
        console.log(preData);
        // 遍历
        var _this = this;
        layui.each(_data, function(key, value) {
            // 遍历tds的值
            tds.each(function() {
                var _field = $(this).data('field');
                if(key == _field) {
                    var cell = $(this).find('.layui-table-cell');
                    var content = _this._getContent(key, value, _data);
                    cell.html(content);
                }
            });
        });
    }
    ,_getBodyTds: function(index) {
        var layuiTableView = $(this.ele).next('.layui-table-view');
        var layuiTableBody = layuiTableView.find('.layui-table-body .layui-table tbody');
        var trList = layuiTableBody.find('tr');
        return trList.eq(index).find('td');
    }
    ,_getContent: function(key, value, data) {
        var col = this.col;             //  所有的列
        var laytpl = this.tpl;
        var rs = '';
        var templet = '';
        // 遍历所有的列
        for(var i=0; i<col.length; i++) {
            var _col = col[i];
            // 判断当前内容是否为对应的需要更新的数据
            if(key == _col.field) {
                // 判断是否有模板
                templet = _col.templet?$(_col.templet).html():_col.toolbar?$(_col.toolbar).html():value;
                // 根据模板构成content
                laytpl(templet).render(data, function(html) {
                    rs = html;
                });
                return rs;
            }
        }
        return false;
    }
};