主表中的人员组件,开启了多选按钮,但是子表单中的人员组件,没有开启多选按钮。 想通过代码实现,主表中人员组件选几个,选完后,将这些人填充到子表单中的人员组件中,每选择一个人就站一行。
代码如下,已经实现获取主表中人员组件多选的信息获取,但是填充到子表单的人员组件时,只能填充第一个选择的人,后续选择的人不会在子表单中新增行。麻烦大神指导下!!!
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);
    
  }
}
                    版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
问题已解决,代码修改如下
  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);
根据您提供的代码,问题出在了循环中的语句 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);
}