开发者社区 问答 正文

主表中人员组件开启多选按钮,如何通过代码填充到子表的人员组件中(未开启多选,一人占一行)

主表中的人员组件,开启了多选按钮,但是子表单中的人员组件,没有开启多选按钮。 想通过代码实现,主表中人员组件选几个,选完后,将这些人填充到子表单中的人员组件中,每选择一个人就站一行。

代码如下,已经实现获取主表中人员组件多选的信息获取,但是填充到子表单的人员组件时,只能填充第一个选择的人,后续选择的人不会在子表单中新增行。麻烦大神指导下!!!

export function onChange({ value }) {
  console.log('onChange', value);
  var newValue=[]
  value.forEach((item)=>{
    newValue.push({
      value:item.value,
      label:item.label
    })
  })
  console.log('newValue', newValue);

  //填充子表单
  for(let i = 0;i<newValue.length;i++){
    const tableValue = this.$('tableField_lila8ke3').getValue();
    console.log('tableValue',tableValue);
    const newTableValue = tableValue.map((item,index)=>{
      item['employeeField_lila8ke4'] = newValue[index];
      return item;
    })
    this.$('tableField_lila8ke3').setValue(newTableValue);
    
  }
}

展开
收起
游客pfw6zgtugzckw 2023-06-08 15:16:13 254 分享 版权
来自: 钉钉宜搭
2 条回答
写回答
取消 提交回答
  • 问题已解决,代码修改如下

      export function onChange({ value }) {
      console.log('onChange', value);
      var newValue = []
      value.forEach((item) => {
        newValue.push({
          value: item.value,
          label: item.label
        })
      })
      console.log('newValue', newValue);
    
      //填充子表单
      const arr = newValue.map((item, index) => {
        const a = [
          { value: newValue[index].value, label: newValue[index].label }
        ]
        console.log('a', a);
        return {
          employeeField_lila8ke4: a
        }
      })
      this.$('tableField_lila8ke3').setValue(arr);
    
    2023-06-09 10:10:39
    赞同 3 展开评论
  • 根据您提供的代码,问题出在了循环中的语句 item['employeeField_lila8ke4'] = newValue[index]; 上。

    这句代码只会将 newValue 数组中的第 i 个元素赋值给子表单中的第 i 行,而不是每次都新增一行。因此,您需要在循环中添加一个判断,如果当前行已经存在值,就新增一行,否则就更新当前行的值。

    以下是修改后的代码,供您参考:

    export function onChange({ value }) {
      console.log('onChange', value);
      var newValue=[]
      value.forEach((item)=>{
        newValue.push({
          value:item.value,
          label:item.label
        })
      })
      console.log('newValue', newValue);
    
      //填充子表单
      const tableValue = this.$('tableField_lila8ke3').getValue();
      const newTableValue = tableValue.map((item,index)=>{
        if (item['employeeField_lila8ke4']) {
          // 如果当前行已经存在值,就新增一行
          const newRow = { ...item };
          newRow['employeeField_lila8ke4'] = newValue[index];
          return newRow;
        } else {
          // 否则就更新当前行的值
          item['employeeField_lila8ke4'] = newValue[index];
          return item;
        }
      })
      this.$('tableField_lila8ke3').setValue(newTableValue);
    }
    
    2023-06-08 17:51:14
    赞同 4 展开评论
问答分类:
问答地址: