antd+vue table表格 默认勾选 编辑状态

antd+vue table表格 默认勾选 编辑状态

小功能记录一下:
table编辑时要展示已勾选的数据,折腾好久,终于找到解决办法。太开心了!
重点是selectedRowKeys
通过赋值selectedRowKeys来控制显示勾选框。
![效果图1](https://img-blog.csdnimg.cn/20210128102330172.png

这里展示的是部分代码

<template>
  <div>
    <a-table
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :columns="columns"
      :data-source="data"
    />
  </div>
</template>
<script>
export default {
  data() {
    return {
      data,
      columns,
      selectedRowKeys: [], // Check here to configure the default column
      loading: false,
    };
  },
  async created: {
    var result = await findUser()
    this.data = result.content //获取整个表格数据
    this.getById()
  },
  methods: {
    //获取已选中的数据
    async getById(e){
      this.selectData = await getUserByGroupId({id:this.id})
      const dataSource = [...this.selectData]
      this.selectedRowKeys = dataSource.map(o=>{return[o.id].toString()})//重点是这里
      this.addData()
    },
    onSelectChange(selectedRowKeys) {
      console.log('selectedRowKeys changed: ', selectedRowKeys);
      this.selectedRowKeys = selectedRowKeys;
    },
  },
};
</script>

selectedRowKeys 值是字符串,不是对象,记得不要传一整个对象给它
例如{id:‘123’}这是识别不了的

还有一个翻页数据丢失的问题还未处理,有小伙伴已经解决的话快来指导我一下吧

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
要使用 Ant Design Vue表格树组件,您需要先安装和导入 Table 和 TreeSelect 组件。 在表格中,您需要使用 `customExpandIcon` 属性来自定义展开/折叠图标,并使用 `customRow` 属性来自定义行的渲染。 以下是一个示例代码: ```vue <template> <a-table :columns="columns" :data-source="data" :custom-row="customRow" :custom-expand-icon="customExpandIcon"> <template slot="name" slot-scope="{ record }"> {{ record.name }} </template> </a-table> </template> <script> import { Table, TreeSelect } from 'ant-design-vue'; export default { components: { Table, TreeSelect, }, data() { return { columns: [ { title: 'Name', dataIndex: 'name', key: 'name', scopedSlots: { customRender: 'name' }, }, { title: 'Age', dataIndex: 'age', key: 'age', }, { title: 'Address', dataIndex: 'address', key: 'address', }, ], data: [ { key: '1', name: 'Parent 1', age: 30, address: 'New York No. 1 Lake Park', children: [ { key: '1-1', name: 'Child 1-1', age: 12, address: 'New York No. 2 Lake Park', }, { key: '1-2', name: 'Child 1-2', age: 13, address: 'New York No. 3 Lake Park', }, ], }, { key: '2', name: 'Parent 2', age: 32, address: 'London No. 1 Lake Park', children: [ { key: '2-1', name: 'Child 2-1', age: 11, address: 'London No. 2 Lake Park', }, { key: '2-2', name: 'Child 2-2', age: 16, address: 'London No. 3 Lake Park', }, ], }, ], }; }, methods: { customExpandIcon({ record, onExpand }) { if (record.children) { return ( <a onClick={onExpand}> {record._expanded ? <a-icon type="minus-square" /> : <a-icon type="plus-square" />} </a> ); } return null; }, customRow(record, index, indent, expanded) { return { class: `custom-row-${record.key}`, on: { click: () => { console.log(record); }, }, }; }, }, }; </script> ``` 在上面的代码中,您可以看到我们自定义了展开/折叠图标和行的渲染,以及使用了插槽 `scopedSlots` 来渲染名称列。您还可以根据您的需求来添加其他自定义的组件或插槽。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值