layui table在不发送请求时更新templet操作列

发布于:2024-05-23 ⋅ 阅读:(87) ⋅ 点赞:(0)

使用的layui的版本为2.4.5 更高的版本好像已经有了更新templet列的方法 而低版本的好像是没有

// 在列表中添加 templet: '#opTpl'
, {field: 'opTpl', title: '操作', fixed: 'left', width: 290, templet: '#opTpl'}

// 所定义的操作列按钮
<script id="opTpl" type="text/html">
    <a class="layui-btn layui-btn-xs" lay-event="viewBtn">
        <i class="layui-icon layui-icon-read"></i>查看
    </a>
    {{# if (d.dialysisStatus != -1){ }}
        <a class="layui-btn layui-btn-xs" lay-event="editBtn">
            <i class="iconfont icon-xinxibianji"></i>编辑
        </a>
    {{# } }}
</script>
that.table.on('tool(dataList)', function (obj) {
     var layEvent = obj.event,
     var data = obj.data;
     if (data && layEvent === 'cancelBtn') {) {
	      data.update({
	            dialysisStatus: -1, // 更新状态值
	            opTpl: '' // 必须要加(对应上面templet: '#opTpl')
	      });
	 }
 });
// 需要改动layui的table.js文件 找到以下代码块(可以全局搜索update) 在update函数中的判断添加 e == 'opTpl'(对应上面templet: '#opTpl') 去更新操作列
var v = function (e) {
    var l = t(this), n = l.parents("tr").eq(0).data("index"), o = a.layBody.find('tr[data-index="' + n + '"]'),
        r = d.cache[a.key][n];
    return t.extend({
        tr: o, data: d.clearCacheKey(r), del: function () {
            d.cache[a.key][n] = [], o.remove(), a.scrollPatch()
        }, update: function (e) {
            e = e || {}, layui.each(e, function (e, l) {
                if (e in r || e == 'opTpl') {
                    var n, d = o.children('td[data-field="' + e + '"]');
                    r[e] = l, a.eachCols(function (t, i) {
                        i.field == e && i.templet && (n = i.templet)
                    }), d.children(f).html(function () {
                        return n ? function () {
                            return "function" == typeof n ? n(r) : i(t(n).html() || l).render(r)
                        }() : l
                    }()), d.data("content", l)
                }
            })
        }
    }, e)
};