pro-components icon indicating copy to clipboard operation
pro-components copied to clipboard

🧐[问题]我使用了EditableProTable, 其中有一行是选择部门,valueType我选择了treeSelect,想问一下treeSelect的options怎么设置,在valueEnum里面设置吗?还是说直接自定义一个新组件呢?

Open akira0912 opened this issue 3 years ago • 8 comments

valueEnum 可以和options相同吗?

{ title: '责任部门', dataIndex: 'dept', key: 'dept', width: '10%', valueType: 'treeSelect', formItemProps: { rules: [{ required: true, message: '请输入责任部门' }], }, valueEnum: [ { value: 'parent 1', title: 'parent 1', children: [ { value: 'parent 1-0', title: 'parent 1-0', children: [ { value: 'leaf1', title: 'leaf1', }, { value: 'leaf2', title: 'leaf2', }, ], }, ], }, ], align: 'center', editable: (text, record) => record.age >= 18, },

akira0912 avatar Mar 29 '23 03:03 akira0912

fieldProps: { options: [] }

cheneyg916 avatar Mar 29 '23 10:03 cheneyg916

TAutomatically replying with ChatGPT can be attempted, but it cannot be guaranteed to be completely accurate and may not fully address all issues. Please feel free to reply if you have any further questions or concerns. 此回复基于 ChatGPT 自动生成,可以尝试下方案,官方人员会在一定时间后继续继续处理。

您好,关于EditableProTable中treeSelect的options设置,您可以在formItemProps中使用fieldProps来设置treeSelect的属性。具体可以参考以下代码示例:

{
  title: '责任部门',
  dataIndex: 'dept',
  key: 'dept',
  width: '10%',
  valueType: 'treeSelect',
  formItemProps: {
    rules: [{ required: true, message: '请输入责任部门' }],
    fieldProps: {
      options: [
        {
          label: 'parent 1',
          value: 'parent 1',
          children: [
            {
              label: 'parent 1-0',
              value: 'parent 1-0',
              children: [
                {
                  label: 'leaf1',
                  value: 'leaf1',
                },
                {
                  label: 'leaf2',
                  value: 'leaf2',
                },
              ],
            },
          ],
        }
      ]
    }
  },
  align: 'center',
  editable: (text, record) => record.age >= 18,
}

在fieldProps中,可以设置treeSelect的所有属性,例如options、onChange等。options是treeSelect的数据源,使用label、value、children来描述。可以根据需要来设置options的内容。

希望以上信息能对您有所帮助。感谢您的提问,如果还有其它问题,请随时提出。

chenshuai2144 avatar Apr 01 '23 08:04 chenshuai2144

设置 fieldProps: { options: [] } 有ts报错,但使用时正常的

luzhuokun avatar Apr 03 '23 06:04 luzhuokun

TAutomatically replying with ChatGPT can be attempted, but it cannot be guaranteed to be completely accurate and may not fully address all issues. Please feel free to reply if you have any further questions or concerns. 此回复基于 ChatGPT 自动生成,可以尝试下方案,官方人员会在一定时间后继续继续处理。

如果使用 fieldProps: {options: []} 时出现 TypeScript 报错,这可能是因为 treeSelect 组件的 options类型不被 TypeScript 自动推断。 您可以这样解决 TypeScript 报错:

fieldProps: {
  options: [] as {
    label: string;
    value: string;
    children?: any[];
  }[]
}

在这个示例中,我们强制将空数组转换为具有必需属性的类型的数组。 这使 TypeScript 能够正确识别 valueType: 'treeSelect' 的 options 属性,并消除报错。

chenshuai2144 avatar Apr 10 '23 13:04 chenshuai2144

非常感谢!已解决,另外还有一个问题,如果onChange也写在fieldProps是不是就获取不到这行的index了?我想做的是两个联动的select,option都是从接口获取的、

akira0912 avatar Apr 13 '23 01:04 akira0912

    {
      title: '责任部门',
      dataIndex: 'responsibleDeptId',
      key: 'responsibleDeptId',
      width: '18%',
      align: 'center',
      //valueType:'treeSelect',
      valueEnum: Object.fromEntries(
        [...deptName.current].map(([key, value]) => [key, { text: value }]),
      ),
      editable: () => flowActivityId == 'task_qlty_error_ycgs' && flowInProcess,
      // fieldProps: {
      //   options: treeSelect
      // },
      renderFormItem: (a) => {
        const x = a.index ?? 0;
        return (
          <ProFormTreeSelect
            rules={[{ required: true }]}
            fieldProps={{
              treeData: treeSelect,
              treeExpandedKeys: expandKeys,
              onTreeExpand: (expandedKeys) => {
                setExpandKeys(expandedKeys);
              },
              onChange: (value) => {
                selectPerson(value, x);
              },
              treeLine: true,
            }}
          />
        );
      },
    },

akira0912 avatar Apr 13 '23 01:04 akira0912

TAutomatically replying with ChatGPT can be attempted, but it cannot be guaranteed to be completely accurate and may not fully address all issues. Please feel free to reply if you have any further questions or concerns. 此回复基于 ChatGPT 自动生成,可以尝试下方案,官方人员会在一定时间后继续继续处理。

如果使用 fieldProps: {options: []} 时出现 TypeScript 报错,这可能是因为 treeSelect 组件的 options类型不被 TypeScript 自动推断。 您可以这样解决 TypeScript 报错:

fieldProps: {
  options: [] as {
    label: string;
    value: string;
    children?: any[];
  }[]
}

在这个示例中,我们强制将空数组转换为具有必需属性的类型的数组。 这使 TypeScript 能够正确识别 valueType: 'treeSelect' 的 options 属性,并消除报错。

@chenshuai2144 给 options 设置类型还是有报错的,要给fieldProps设置类型才行

luzhuokun avatar Apr 13 '23 01:04 luzhuokun

fieldProps: (_, { rowIndex }) => {
        return {
          options: treeSelect,
          treeExpandedKeys: expandKeys,
          onTreeExpand: (expandedKeys) => {
            setExpandKeys(expandedKeys);
          },
          onSelect: (value: number) => {
            editorFormRef.current?.setRowData?.(rowIndex, { responsibleId: '0' });
            selectPerson(value, rowIndex);
          },
          treeLine: true,
        };
      },

抱歉 好好看完文档之后发现可以这样写。还有一点bug没改完

akira0912 avatar Apr 13 '23 09:04 akira0912