layui 对 table 中的数据进行转义

本文介绍两种在前端开发中实现表格数据动态渲染的方法:一是利用done回调遍历并修改表格单元格内容;二是采用templet模板直接定义单元格展示形式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法一:通过 done 回调进行遍历:

		// 方法级渲染
        table.render({
            id: 'demoList',
            elem: '#demoList'
            , url: 'showDemoList'
            , cols: [[
                {checkbox: true, fixed: true, width: '10%'}
                , {field: 'id', title: '编号', width: '15%', sort: true}
                , {field: 'pipe_id', title: '机构ID', width: '15%'}
                , {field: 'bank_id', title: '银行ID', width: '10%'}
                , {field: 'pipe_cost', title: '成本', width: '10%'}
                , {field: 'status', title: '状态', width: '20%'}
                , {field: 'remark', title: '操作', width: '20%', toolbar: '#toolBar'}
            ]],
            done: function (res, curr, count) {
                // 如果是异步请求数据方式,res即为你接口返回的信息。
                // 如果是直接赋值的方式,res即为:{data: [], count: 99} data为当前页数据、count为数据总长度
                $("[data-field='status']").children().each(function () {
                    if ($(this).text() == '0') {
                        $(this).text('有效');
                    } else if ($(this).text() == '1') {
                        $(this).text('无效');
                    }
                });
            }
            , page: true
            , height: 'full-83'
        });

方法二:通过 templet 模板实现

{field: 'status', title: '状态', width: '20%',templet:'#status'}
 
<script type="text/html" id="status">
    {{#  if(d.status=='0'){ }}
   	有效
    {{#  }else{ }}
        无效
    {{# } }}
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值