前端 js实现 选中数据 动态 添加在表格中

如下图展示,表格上方有属性内容,下拉选中后,根据选中的内容,添加在下方的表格中。



实现方式,(要和后端约定,因为这些动态添加的字段都是后端返回的,后端自己会做处理,我们前端要做的是,就是根据后端的返回下拉数据,映射到表格上,并实现增删改查。)

一般可能会 用 ``模板字符串 方法,但是有时候可以直接map赋值。

如下 。

复制代码
			<>
            销售属性:
            <Select
              style={{ width: 260, marginRight: 10 }}
              size="small"
              mode="multiple"
              maxTagCount="responsive"
              value={this.props.salesAttrCodes}
              options={this.props.selsetList} // 展示的数据,后端返回,自己调取接口获取后赋值
              onChange={(value) => {
                this.handleChangeSalesAttr(value);
              }}
            />
          </>

	  handleChangeSalesAttr = (value) => {
	    this.props.changeSalesAttrCodes(value);
	    const addrowsData = this.props?.selsetListAll?.filter((item) => {
	      return value.includes(item.catePropCode);
	    });
	    const addrows =
	      addrowsData.length &&
	      addrowsData.map((item) => {
	        let them;
	        if (item?.isHand) {
	          them = { // 表格定义的规则 。
	            title: item.catePropName,
	            width: 180,
	            dataIndex: item.catePropCode,
	            align: 'left',
	            editable: true,
	            rules: [{ required: item.isMust, message: '必填' }],
	            field: () => {
	              return {
	                formOption: {
	                  type: '$textArea',
	                  props: {
	                    showCount: true,
	                    maxLength: 200,
	                    placeholder: '请输入且最多200个字',
	                  },
	                },
	                name: item.catePropCode,
	              };
	            },
	          };
	        } else {
	          them = {
	            title: item.catePropName,
	            width: 180,
	            dataIndex: item.catePropCode,
	            align: 'left',
	            editable: true,
	            rules: [{ required: item.isMust, message: '必填' }],
	            field: () => {
	              return {
	                formOption: {
	                  type: '$select',
	                  props: {
	                    placeholder: '请选择',
	                    options: item.valueNames.split(';').map((item) => {
	                      return {
	                        label: item,
	                        value: item,
	                      };
	                    }),
	                  },
	                },
	                name: item.catePropCode,
	              };
	            },
	          };
	        }
	        return them;
	      });
	      // 重新触发更新表格
	    this.setState({
	      AddTableColumns: addrows,
	    });
	  };
相关推荐
玄同7658 分钟前
我的 Trae Skill 实践|使用 UV 工具一键搭建 Python 项目开发环境
开发语言·人工智能·python·langchain·uv·trae·vibe coding
Yorlen_Zhang19 分钟前
Python Tkinter Text 控件完全指南:从基础编辑器到富文本应用
开发语言·python·c#
lxl130720 分钟前
C++算法(1)双指针
开发语言·c++
不绝19131 分钟前
C#进阶:预处理指令/反射,Gettype,Typeof/关键类
开发语言·c#
前端大卫34 分钟前
Vue3 + Element-Plus 自定义虚拟表格滚动实现方案【附源码】
前端
无小道37 分钟前
Qt-qrc机制简单介绍
开发语言·qt
zhooyu44 分钟前
C++和OpenGL手搓3D游戏编程(20160207进展和效果)
开发语言·c++·游戏·3d·opengl
HAPPY酷1 小时前
C++ 和 Python 的“容器”对决:从万金油到核武器
开发语言·c++·python
大鹏说大话1 小时前
告别 MSBuild 脚本混乱:用 C# 和 Nuke 构建清晰、可维护的现代化构建系统
开发语言·c#
却尘1 小时前
Next.js 请求最佳实践 - vercel 2026一月发布指南
前端·react.js·next.js