ant design 给表格添加可编辑列,无法取到组件的this值

问题:想为表格实现一列编辑按钮,并且点击按钮会触发相应的事件

先写了以下的代码,发现this为undefined,所以无法调用this,也就没法去触发组件下的方法

{
        title: '操作',
        dataIndex: '',
        key: 'x',
        render: (record, row) =>
            (<span>
                <a href="#" onClick={() => this.handleEdit(true)}>编辑</a>
            </span>)
    }]

解决办法,通过props传值的方法,由父组件将方法传递给子组件

<Table dataSource={data}
    columns={columns({
       handleEdit: this.setModal1Visible
    })}
    pagination={{total, onChange: this.handleSearch}}>
</Table>
const columns = (props) => {
    const { handleEdit } = props;
    return [{
        title: '操作',
        dataIndex: '',
        key: 'x',
        render: (record, row) =>
            (<span>
                <a href="#" onClick={() => handleEdit(true)}>编辑</a>
            </span>)
    }]
};

这样就可以访问到父组件的setModal1Visible方法了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值