记录一次有关layui的bug修复,监听table内的input的动态变化并且修改表格

1.场景:

今天发现一个离职同事留下的bug,table的删除逻辑有问题。仔细一看,同事是通过获取table的行data的name数据的来配合删除的。但由于他在table内实现的input是动态变化的且没有实时修改表格的data,在此记录一下修改方案。

2.code:

typescript 复制代码
<table id="attributeTable" lay-filter="attributeTable">
      
</table>
-------------------------------------------------------------------------------------
<script type="text/html" id="inputNameTpl">
  <div class="layui-input-inline" style="width: 100%;">
    <input type="text" name="inputName" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input table-input" value="{{d.name || ''}}">
  </div>
</script>
<script type="text/html" id="inputValueTpl">
  <div class="layui-input-inline" style="width: 100%;">
    <input type="text"  name="inputValue" placeholder="多属性用英文逗号隔开" autocomplete="off" class="layui-input table-input" value="{{d.nameCode || ''}}">
  </div>
</script>
-------------------------------------------------------------------------------------
    layui.use(['table', 'form'], function(){
      var tableHeight = $(document).height() > 800 ? $(document).height() - 575 : 430 ;
      layui.table.render({
        elem: '#attributeTable'
        ,id: 'attributeTable'
        ,height:tableHeight
        ,limit:100000
        ,data:App.addDttributeData
        ,cols:  [[
          {checkbox: true}
          ,{field: 'name', title: '属性名称',align:'center',templet: '#inputNameTpl'}
          ,{field: 'nameCode', title: '属性值',align:'center',templet: '#inputValueTpl'}

        ]]
      });
      // 监听表格单击事件
      layui.table.on('row(attributeTable)', function(obj){
        // 监听表格内input的修改事件
        $('.table-input').on('change',  function(){
          var value = $(this).val(); // 获取input的值
          var field = $(this).attr("name"); // 获取input的name值
          // 根据field更新原始数据
          if (field == "inputName"){
            obj.update({
              name: value,
            });
          } else if (field == "inputValue"){
            obj.update({
              nameCode: value,
            });
          }
          layui.table.reload("attributeTable", {
            scrollPos: 'fixed'
          });
        });
      });
    });
相关推荐
小林攻城狮28 分钟前
使用 Transport 节流解决 Vercel AI SDK 流式渲染卡死问题
前端·react.js
前端缘梦33 分钟前
告别 TS 运行时类型漏洞!Zod 完整入门实战教程(前端 / 全栈必备)
前端·react.js·全栈
the_answer1 小时前
Webpack vs Vite 深度对比分析
前端·webpack
转转技术团队1 小时前
验证码识别实战:前端不写页面,改训模型了?
前端
MomentYY1 小时前
Temperature:AI 的“脑洞旋钮”
前端·llm·ai编程
远航_1 小时前
OpenSpec 完整详细介绍
前端·后端
召钱熏2 小时前
状态枚举正确≠渲染正确:一个语音按钮的状态机边界修复实录
android·前端
SkyWalking中文站2 小时前
认识 Horizon UI · 1/17:SkyWalking 新一代可观测性控制台
运维·前端·监控
cidy_982 小时前
Dify 操作教程:工作流编排 & Chat 对话编排
前端·工作流引擎
tangdou3690986552 小时前
AI真好玩系列-2分钟快速了解DeepAgents | Quick Guide to DeepAgents in 2 Minutes
前端·javascript·后端