新增、编辑、详情用同一个页面实现

实现不同路由页面(如新增、编辑、详情)用同一个组件,减少代码冗余

有一些业务的页面大同小异,尤其是新增、编辑、详情,如果在router.js里面配置时不同的路由用不同的组件,会导致代码冗余,重复的代码复制来复制去,而且维护起来很不方便。

我后来想到一个方法,就是配置不同的路由,但是它们所引用的组件都一样,进入页面时通过路由来判断是哪个页面,从而控制页面元素的显隐,调不同的接口

以下举例:

1、不同的路由配置相同的组件

2、组件data()中定义一个标志,用于判断是该页面是属于哪个路由

3、定义一个getPageType()方法,页面初始化的时候调用,通过路由来判断当前页面是属于哪个(是新增,编辑,还是详情?)

 4、根据pageType标识控制页面元素的显隐、不同接口的调用

以上为本人拙见,如有小伙伴发现哪里有错误或者有更好的实现方式,欢迎大家指正交流~ 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ant Design Vue 的 Table 组件提供了可编辑、可新增、可删除等功能,可以很方便地实现一列多个值的表格。下面是实现步骤: 1. 在页面中引入 Table 组件并配置表头。在表头中定义需要展示的列,例如:字段1、字段2、字段3。 ``` <template> <a-table :columns="columns" :data-source="dataSource"></a-table> </template> <script> export default { data() { return { columns: [ { title: '字段1', dataIndex: 'field1', key: 'field1' }, { title: '字段2', dataIndex: 'field2', key: 'field2' }, { title: '字段3', dataIndex: 'field3', key: 'field3' } ], dataSource: [] } } } </script> ``` 2. 在 dataSource 中初始化一行空白数据,作为新增数据使用。 ``` data() { return { columns: [...], dataSource: [ { key: '0', field1: '', field2: '', field3: '' } ] } } ``` 3. 针对每一行数据,可以添加一个编辑按钮或者直接在单元格中添加编辑功能,让用户可以对数据进行修改。这里我们采用在单元格中添加编辑功能的方式,使用 Editable Cell 和 Editable Row 组件来实现。 ``` <template> <a-table :columns="columns" :data-source="dataSource" :components="components"> <template #body="tableProps"> <editable-row :record="tableProps.record" :index="tableProps.index"> <a-table-cell v-for="column in columns" :key="column.dataIndex" :dataIndex="column.dataIndex" :record="tableProps.record" :editable="column.editable" @update="handleUpdate" > {{ tableProps.record[column.dataIndex] }} </a-table-cell> </editable-row> </template> </a-table> </template> <script> import { EditableRow, EditableCell } from 'ant-design-vue'; export default { components: { EditableRow, EditableCell }, data() { return { columns: [...], dataSource: [ { key: '0', field1: '', field2: '', field3: '' } ] } }, methods: { handleUpdate(record, dataIndex, value) { const index = this.dataSource.findIndex(item => item.key === record.key); this.dataSource[index][dataIndex] = value; } } } </script> ``` 在这个例子中,我们使用了 Editable Cell 和 Editable Row 组件来实现单元格和行的编辑功能。其中,Editable Cell 组件用于渲染单元格内容,Editable Row 组件用于渲染整行数据。我们给每个需要编辑的单元格添加了 editable 属性以及 update 事件,当用户修改单元格内容时,会触发 update 事件,我们可以在这个事件中更新 dataSource 中对应的数据。 4. 当用户点击新增按钮时,可以在表格中添加一行空白行,并让用户输入新的数据。同时,也要提供一个保存按钮,让用户可以保存新增的数据。这里我们通过添加一个按钮来实现新增功能,并在按钮点击事件中向 dataSource 中添加一行空白数据。 ``` <template> <div> <a-button type="primary" @click="handleAdd">新增</a-button> <br /><br /> <a-table :columns="columns" :data-source="dataSource" :components="components"> <template #body="tableProps"> <editable-row :record="tableProps.record" :index="tableProps.index"> <a-table-cell v-for="column in columns" :key="column.dataIndex" :dataIndex="column.dataIndex" :record="tableProps.record" :editable="column.editable" @update="handleUpdate" > {{ tableProps.record[column.dataIndex] }} </a-table-cell> </editable-row> </template> </a-table> </div> </template> <script> import { EditableRow, EditableCell } from 'ant-design-vue'; export default { components: { EditableRow, EditableCell }, data() { return { columns: [...], dataSource: [ { key: '0', field1: '', field2: '', field3: '' } ] } }, methods: { handleAdd() { const newData = { key: `${this.dataSource.length}`, field1: '', field2: '', field3: '' }; this.dataSource.push(newData); }, handleUpdate(record, dataIndex, value) { const index = this.dataSource.findIndex(item => item.key === record.key); this.dataSource[index][dataIndex] = value; } } } </script> ``` 在这个例子中,我们添加了一个按钮来触发新增操作,并在按钮点击事件中向 dataSource 中添加了一行空白数据。注意,每行数据都必须有一个唯一的 key 属性,用于区分不同的数据行。 通过以上步骤,就可以实现 Ant Design Vue 的 Table 组件中的一列多个值可编辑新增的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值