vbenjs / vue-vben-admin

A modern vue admin panel built with Vue3, Shadcn UI, Vite, TypeScript, and Monorepo. It's fast!
https://www.vben.pro
MIT License
25.21k stars 6.87k forks source link

appendSchemaByField 添加表单默认值丢失 #2600

Closed nejinn closed 1 year ago

nejinn commented 1 year ago

⚠️ 重要 ⚠️ 在进一步操作之前,请检查下列选项。如果您忽视此模板或者没有提供关键信息,您的 Issue 将直接被关闭

描述 Bug

appendSchemaByField 添加表单默认值丢失

Snipaste_2023-03-05_14-47-56

复现 Bug

      function add() {
        appendSchemaByField(
          [
            {
              field: `divider-spec${n.value}`,
              component: 'Divider',
              label: `品规信息`,
            },
            {
              field: `specName${n.value}`,
              component: 'Input',
              label: '规格名称',
              required: true,
              colProps: {
                span: 8,
              },
              componentProps: {
                placeholder: '请输入规格名称',
              },
              helpMessage: '最长输入20个字',
              dynamicRules: () => {
                return [
                  {
                    required: true,
                    validator: (_, value) => {
                      if (!value) {
                        return Promise.reject('请输入规格名称');
                      }
                      if (value.length > 20) {
                        return Promise.reject('最长输入20个字');
                      }
                      return Promise.resolve();
                    },
                  },
                ];
              },
            },
            {
              field: `specUnit${n.value}`,
              component: 'ApiSelect',
              required: true,
              label: '规格单位',
              colProps: {
                span: 8,
              },
              helpMessage: '请选择单位',
              componentProps: {
                // more details see /src/components/Form/src/components/ApiSelect.vue
                api: selectBoxApi,
                resultField: 'unit',
                // use name as label
                labelField: 'name',
                // use id as value
                valueField: 'value',
                showSearch: true,
                optionFilterProp: 'label',
                params: {
                  key: 'unit',
                },
              },
            },
            {
              field: `specMinNum${n.value}`,
              component: 'InputNumber',
              label: '最小服务数量',
              required: true,
              defaultValue: 1,
              helpMessage:
                '此品规每次服务可以提供的最小服务数量,比如单位为平米,最小服务数量60,即购买一次服务最少可以获得60平米服务,最小服务数量必须是整数',
              colProps: {
                span: 8,
              },
              componentProps: {
                placeholder: '请输入最小服务数量',
              },
              dynamicRules: ({ values }) => {
                return [
                  {
                    required: true,
                    validator: (_, value) => {
                      if (value <= 0) {
                        return Promise.reject('最小服务数量必须大于0');
                      }
                      if (value > values.specMaxNum) {
                        return Promise.reject('最小服务数量不能大于最大服务数量');
                      }

                      if (value % 1 !== 0) {
                        return Promise.reject('最小服务数量只能是整数');
                      }
                      return Promise.resolve();
                    },
                  },
                ];
              },
            },
            {
              field: `specMaxNum${n.value}`,
              component: 'InputNumber',
              label: '最大服务数量',
              required: true,
              defaultValue: 1,
              colProps: {
                span: 8,
              },
              componentProps: {
                placeholder: '请输入最大服务数量',
              },
              helpMessage:
                '此品规每次服务可以提供的最大服务数量,比如单位为平米,最大服务数量60,即购买一次服务最多可以获得60平米服务,最大服务数量必须是整数',
              dynamicRules: ({ values }) => {
                return [
                  {
                    required: true,
                    validator: (_, value) => {
                      if (value <= 0) {
                        return Promise.reject('最大服务数量必须大于0');
                      }
                      if (value > values.specMinUum) {
                        return Promise.reject('最大服务数量不能小于最小服务数量');
                      }

                      if (value % 1 !== 0) {
                        return Promise.reject('最大服务数量只能是整数');
                      }
                      return Promise.resolve();
                    },
                  },
                ];
              },
            },
            {
              field: `specPrice${n.value}`,
              component: 'InputNumber',
              label: '单价',
              required: true,
              defaultValue: 1,
              colProps: {
                span: 8,
              },
              componentProps: {
                placeholder: '请输入单价',
              },
              helpMessage:
                '此品规1一个服务数量的价格,即单价,比如单价5,最小服务数为50,那么总价就最少为250',
              dynamicRules: () => {
                return [
                  {
                    required: true,
                    validator: (_, value) => {
                      if (value < 0) {
                        return Promise.reject('单价必须大于等于0');
                      }
                      return Promise.resolve();
                    },
                  },
                ];
              },
            },
            {
              field: `specSvNum${n.value}`,
              component: 'InputNumber',
              label: '服务次数',
              required: true,
              defaultValue: 1,
              colProps: {
                span: 8,
              },
              componentProps: {
                placeholder: '请输入服务次数',
              },
              helpMessage: '服务次数代表可以提供几次服务,服务次数必须是整数',
              dynamicRules: () => {
                return [
                  {
                    required: true,
                    validator: (_, value) => {
                      if (value < 0) {
                        return Promise.reject('服务次数必须大于等于0');
                      }
                      if (value % 1 !== 0) {
                        return Promise.reject('服务次数只能是整数');
                      }
                      return Promise.resolve();
                    },
                  },
                ];
              },
            },
            {
              field: `specPremiumRate${n.value}`,
              component: 'InputNumber',
              label: '溢价系数',
              required: true,
              defaultValue: 100,
              colProps: {
                span: 8,
              },
              componentProps: {
                placeholder: '请输入溢价系数',
              },
              helpMessage:
                '溢价系数为品规总价乘数。比如总价100,溢价系数是130,则品规实际总价为 100 * 130 / 100,溢价系数必须是整数',
              dynamicRules: () => {
                return [
                  {
                    required: true,
                    validator: (_, value) => {
                      if (value < 90) {
                        return Promise.reject('溢价系数必须大于等于90');
                      }
                      if (value % 1 !== 0) {
                        return Promise.reject('溢价系数只能是整数');
                      }
                      return Promise.resolve();
                    },
                  },
                ];
              },
            },
            {
              field: `specDiscountRate${n.value}`,
              component: 'InputNumber',
              label: '折扣系数',
              required: true,
              defaultValue: 100,
              colProps: {
                span: 8,
              },
              componentProps: {
                placeholder: '请输入折扣系数',
              },
              helpMessage:
                '折扣系数是打折,比如折扣系数围为90,即打九折,总价为100,则优惠价格为 90 * 100 = 90,折扣系数必须是整数',
              dynamicRules: () => {
                return [
                  {
                    required: true,
                    validator: (_, value) => {
                      if (value < 0) {
                        return Promise.reject('折扣系数必须大于等于0');
                      }
                      if (value % 1 !== 0) {
                        return Promise.reject('折扣系数只能是整数');
                      }
                      return Promise.resolve();
                    },
                  },
                ];
              },
            },
            {
              field: `specPointRate${n.value}`,
              component: 'InputNumber',
              label: '积分系数',
              required: true,
              defaultValue: 1,
              colProps: {
                span: 8,
              },
              componentProps: {
                placeholder: '请输入积分系数',
              },
              helpMessage:
                '积分系数购买此品规可获得的积分,比如总价100,积分系数为0.7,则可获取70积分',
              dynamicRules: () => {
                return [
                  {
                    required: true,
                    validator: (_, value) => {
                      if (value < 0) {
                        return Promise.reject('积分次数必须大于等于0');
                      }
                      return Promise.resolve();
                    },
                  },
                ];
              },
            },
            {
              field: `specDesc${n.value}`,
              component: 'InputTextArea',
              label: '规格描述',
              required: true,
              colProps: {
                span: 16,
              },
              componentProps: {
                placeholder: '请输入描述',
              },
            },
            {
              field: `specIsDel${n.value}`,
              label: '状态',
              component: 'RadioButtonGroup',
              defaultValue: 1,
              required: true,
              colProps: {
                span: 8,
              },
              componentProps: {
                options: [
                  { label: '启用', value: 1 },
                  { label: '停用', value: 2 },
                ],
              },
            },
            {
              field: `${n.value}`,
              component: 'Input',
              label: ' ',
              colProps: {
                span: 8,
              },
              slot: 'add',
            },
          ],
          `${n.value - 1}`,
        );
        nArray.value.push(n.value);
        n.value++;
      }

系统信息

luocong2016 commented 1 year ago

看案例没问题

这个代码问题,找不到这个插入位置

${n.value - 1} 
nejinn commented 1 year ago

Hi,this is Lerity      邮件已经收到      辛苦了

zhangyue-mars commented 5 months ago

请问这个问题怎么解决呢?我的也是看不到默认值,appendSchemaByField函数,里面写了defaultValue: 1,代码如下: appendSchemaByField( { field: hyperParamTrainPrefix + value, label: '超参数:' + value, // subLabel:'test', required: true, defaultValue: 1, component: 'InputNumber', show: true, componentProps: { placeholder: '请输入对应超参', }, colProps: { span: 20 }, dynamicRules: ({}) => { return [ { required: true, validator: (_, value) => { if (value <= 0) { return Promise.reject('超参数必须大于0'); } return Promise.resolve(); }, }, ]; }, }, undefined, undefined, );

image 界面上不显示默认值