记录一次有关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'
          });
        });
      });
    });
相关推荐
2401_885885045 小时前
国际语音php接口代码示例:PHP使用cURL快速调用语音发送API
android·开发语言·前端·人工智能·python·php·语音识别
leoZ2315 小时前
AI+前端提效-08 AI自动化文档:前端组件、接口、项目文档自动生成
前端·人工智能·深度学习·神经网络·目标检测·自然语言处理·自动化
lilian2335 小时前
HarmonyOS 7 新特性(五)|ContainerReader 容器断点与自适应布局
前端·华为·harmonyos
计算机魔术师5 小时前
Google DeepMind 将 AI 科学家 Co-Scientist 扩展为实验室集成研究伙伴
前端
2601_965798475 小时前
BrandBoost WordPress Theme Review: Fast Agency Web Architecture
前端
律宏阔5 小时前
Electron 集成 Drizzle + SQLite 踩坑笔记
前端
律宏阔5 小时前
Electron preload.ts 类型无法自动推导的解决方案
前端
名字还没想好☜5 小时前
kubectl 排障实战:jsonpath 精准取值、custom-columns、events 排序与 top 速查
运维·前端·chrome·docker·kubernetes
李昊哲小课5 小时前
Spring Boot 4 旅游主题实战教程 阶段二:Web 开发基础
前端·spring boot·旅游
ITmaster07316 小时前
从零到一!前端搭建本地轻量化 RAG 问答系统
前端