antd-vue + a-table的select组件实现既可以输入添加,又可以下拉选择

antd-vue的select组件实现既可以输入添加,又可以下拉选择

最近,项目中要求a-table列表中单价字段可以从下拉框(后台返回的数据)中选择,也可以手动输入添加下拉框没有的内容,项目用的是antd-vue,所以最接近的组件就是a-select组件了,改造方法如下:
HTML 代码:

 <a-table
  :columns="columnsContract"
  :dataSource="clickPurContract"
  :pagination="false"
  :scroll="{ x: 'max-content' }"
  size="small"
  bordered
 >
   <template v-for="col1 in ['estimatePrice']" :slot="col1" slot-scope="text, record, index">
     <a-select
       @change="setEstimate($event, index, record.materialPrice)"
       @search="handleSearch($event, index, record.materialPrice)"
       @blur="handleBlur($event, index, record.materialPrice)"
       :filterOption="filterOption"
       defaultValue="--请选择--"
       showSearch
       :allowClear="true"
       v-model="clickPurContract[index].estimatePrice">
       <a-select-option v-for="item in record.materialPrice" :key="item.id">
         {{ item.TextEs }}
       </a-select-option>
     </a-select>
   </template>
 </a-table>

data定义:

data() {
  return {
   columnsContract: [{
       title: '单价',
       dataIndex: 'estimatePrice',
       className: 'columnsColor',
       scopedSlots: { customRender: 'estimatePrice' },
    }],
    clickPurContract:[{
      materialPrice:[{
      	id:"",
        TextEs:""
      }],
      estimatePrice:"",
    }],
  }
 }

methods方法:

getPrice(item, list, value) {
 let a = { Price: value }
 for (let i = 0; i < list.length; i++) {
   if (list[i].item == item) {
     let Price = list[i].Price == '' ? value : list[i].Price
     a.Price = Price
   }
 }
 return a
},
setEstimate(value, index, list) {
 this.clickPurContract[index].estimatePrice = value
 let a = this.getPrice(this.clickPurContract[index].estimatePrice, list, value)
},
handleSearch(value, index, list) {
 this.setEstimate(value, index, list)
},
handleBlur(value, index, list) {
 this.setEstimate(value, index, list)
},

效果图如下:
在这里插入图片描述

您可以在 Ant Design Vue 的弹窗中使用 Table 组件Select 组件实现行编辑中的下拉选择框。首先,您需要在 Table 组件中设置 editable 属性为 true,这样就可以开启行编辑功能。然后,您可以在需要进行下拉选择框编辑的列中,使用 scoped slot 的方式自定义单元格内容,并在该单元格中使用 Select 组件实现下拉选择框。具体实现方法可以参考以下代码示例: ```html <template> <div> <a-button type="primary" @click="showModal">打开弹窗</a-button> <a-modal v-model="visible" title="编辑表格" :width="800"> <a-form :form="form"> <a-table :columns="columns" :data-source="data" :row-key="record => record.id" :editable="true"> <template slot-scope="text, record, index"> <a-form-item> <a-select v-model="record.selectValue" style="width: 100%"> <a-select-option v-for="option in options" :key="option.value" :value="option.value">{{ option.label }}</a-select-option> </a-select> </a-form-item> </template> </a-table> </a-form> </a-modal> </div> </template> <script> import { Modal, Form, Button, Table, Select } from 'ant-design-vue' export default { components: { 'a-modal': Modal, 'a-form': Form, 'a-button': Button, 'a-table': Table, 'a-form-item': Form.Item, 'a-select': Select, 'a-select-option': Select.Option, }, data() { return { visible: false, form: this.$form.createForm(this), data: [ { id: 1, name: '张三', age: 18, selectValue: 'option1' }, { id: 2, name: '李四', age: 22, selectValue: 'option2' }, { id: 3, name: '王五', age: 25, selectValue: 'option3' }, ], options: [ { label: '选项1', value: 'option1' }, { label: '选项2', value: 'option2' }, { label: '选项3', value: 'option3' }, ], columns: [ { title: 'ID', key: 'id', dataIndex: 'id', editable: false }, { title: '姓名', key: 'name', dataIndex: 'name', editable: true }, { title: '年龄', key: 'age', dataIndex: 'age', editable: true }, { title: '下拉选择框', key: 'selectValue', dataIndex: 'selectValue', editable: true }, ], } }, methods: { showModal() { this.visible = true }, }, } </script> ``` 在上面的代码示例中,我们使用了 Ant Design Vue 的 Modal、Form、Button、TableSelect 组件实现弹窗、表单、按钮、表格和下拉选择框。在 Table 组件中,我们使用了 scoped slot 来自定义单元格内容,并在该单元格中使用 Select 组件实现下拉选择框。同时,我们还需要在该列的配置项中设置 editable 属性为 true,以开启该列的编辑功能。最终,我们将 Table 组件放置在 Modal 组件中,实现了在弹窗中编辑带有下拉选择框的表格的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值