记录一次有关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'
          });
        });
      });
    });
相关推荐
tryCbest3 小时前
Vue3——第 22 篇:Pinia——新一代状态管理(上)
前端·javascript·vue.js
bai2733 小时前
"👨‍👩‍👧‍👦" 的 length 是 11:我把自己写的字数统计脚本拖出来审了一遍
前端·javascript
zhanlang02174 小时前
HTML 零基础入门到企业实战【第八章 8.5】画布 canvas、SVG 矢量图形基础入门
前端·html
星云_byto5 小时前
CSS面试题
前端·css
zhanlang02175 小时前
HTML 零基础入门到企业实战全套教程【连载 8.4】HTML5 新增表单属性与表单控件
前端·html·html5
科研邪修法14 小时前
PDF新增页面文字与文本批注怎么区分?以双版本培训讲义为例
前端·css·pdf
Lanwarf-前端开发15 小时前
Vue3 + Canvas/WebGL 雷达扫描可视化实战:PPI、RHI、VOL、SVOL 四种模式原理与实现
前端·javascript·vue.js·webgl
FRing0316 小时前
vue3干货总结2(vue3生命周期、父子通信、模版引用、provide和inject)
开发语言·前端·vue.js·vue3
锋行天下17 小时前
langgraph 全栈项目(三)
前端·人工智能·后端
锋行天下18 小时前
langgraph 全栈项目(二)
前端·人工智能·后端