vxe-table 复选框--分页选择及回显数据

该文章已生成可运行项目,

解决vxe-table复选框跨页选中和数据回显
根据vxe-table官方文档,想要保留选中的数据,需要用到配置项 “ checkbox-configrow-config” 
Vxe Table v4官网



html代码

<vxe-table
        ref="tableRef"
        :header-cell-style="{
          'text-align': 'center',
          backgroundColor: '#F2F2F2',
          color: '#000000'
        }"
        :cell-style="{ 'text-align': 'center', color: '#000000' }"
        height="500"
        min-height="48px"
        :data="data"
        border
        stripe
        resizable
        @checkbox-change="handleSelectionChange"
        @checkbox-all="selectAllCheckboxChange"
        style="width: 97%; margin: 0 auto"
        @cell-click="rowClick"
        :seq-config="{ seqMethod }"
        :row-config="{ keyField: 'id' }"
        :checkbox-config="{ reserve: true }"
      >
    <vxe-column type="checkbox" width="55" />
    <vxe-column title="序号" type="seq" align="center" width="60" />
</vxe-table>

因为需求要单击行也要选中checkbox所以上述代码写了3个方法,下面是获取选中行的数据

//单行选中checkbox
const rowClick = ({ row }) => {
  const b = tableRef.value.getCheckboxRecords() //当前页点击选中的数据
  const a = tableRef.value.getCheckboxReserveRecords() //当前页之前点击选中的数据
  state.multipleSelection = [...a, ...b] 
}

//
const handleSelectionChange = ({ checked, records, reserves, row }) => {
  const b = tableRef.value.getCheckboxRecords() //当前页点击选中的数据
  const a = tableRef.value.getCheckboxReserveRecords() //当前页之前点击选中的数据
  state.multipleSelection = [...a, ...b]
}

//全选
const selectAllCheckboxChange = (checked) => {
  const b = tableRef.value.getCheckboxRecords() //当前页点击选中的数据
  const a = tableRef.value.getCheckboxReserveRecords() //当前页之前点击选中的数据
  state.multipleSelection = [...a, ...b]
}

state.multipleSelection //自定义数组存选中的所有数据(包括翻页的数据)
tableRef.value  //表格实例
getCheckboxRecords() getCheckboxReserveRecords() //vxe-table 提供的方法

因为用户可能会点击行选中数据 或者点击checkBox选中数据,所以上述3个方法最好都写上,以防万一。(如果需求没有单击行选中数据,第一个方法可不写

本文章已经生成可运行项目
在 `vxe-table` 组件中添加复选框列是一个常见的需求,通常用于实现数据的多选操作。`vxe-table` 提供了便捷的方式来实现该功能,主要通过配置 `type="checkbox"` 的列来实现。 以下是一个基本的实现方式: ### 添加复选框列 ```html <template> <vxe-table :data="tableData"> <vxe-column type="checkbox" width="60"></vxe-column> <vxe-column field="name" title="Name"></vxe-column> <vxe-column field="age" title="Age"></vxe-column> <vxe-column field="address" title="Address"></vxe-column> </vxe-table> </template> <script> export default { data() { return { tableData: [ { id: 1, name: 'Alice', age: 25, address: 'New York' }, { id: 2, name: 'Bob', age: 30, address: 'San Francisco' }, { id: 3, name: 'Charlie', age: 28, address: 'Los Angeles' } ] }; } }; </script> ``` 上述代码中,`<vxe-column type="checkbox"` 会自动为每一行生成一个复选框,用户可以通过勾选复选框选择对应的行数据。 ### 获取选中行数据 若需要获取当前选中的行数据,可以使用 `ref` 来调用 `vxe-table` 提供的方法: ```html <template> <div> <vxe-table ref="xTable" :data="tableData"> <vxe-column type="checkbox" width="60"></vxe-column> <vxe-column field="name" title="Name"></vxe-column> <vxe-column field="age" title="Age"></vxe-column> <vxe-column field="address" title="Address"></vxe-column> </vxe-table> <button @click="getSelectedRows">获取选中行</button> </div> </template> <script> export default { methods: { getSelectedRows() { const selectedRows = this.$refs.xTable.getCheckboxRecords(); console.log('选中的行数据:', selectedRows); } } }; </script> ``` 在该示例中,`getCheckboxRecords()` 方法用于获取当前选中的行数据,并输出到控制台。该方法可以结合按钮点击事件来触发,适用于批量操作、数据导出等场景。 ### 自定义复选框列行为 如果需要对复选框列的行为进行自定义,例如控制某些行不可选,可以通过 `checkbox-config` 属性进行设置: ```html <vxe-table :data="tableData" :checkbox-config="{ checkField: 'checked', trigger: 'cell' }" > <vxe-column type="checkbox" width="60"></vxe-column> <vxe-column field="name" title="Name"></vxe-column> <vxe-column field="age" title="Age"></vxe-column> <vxe-column field="address" title="Address"></vxe-column> </vxe-table> ``` 通过 `checkbox-config` 可以指定选中状态的字段名(`checkField`)以及触发选中行为的方式(`trigger`),如点击单元格(`cell`)或点击复选框(`default`)。 ### 样式与布局优化 为了提升用户体验,可以为复选框列设置固定宽度,防止布局错乱: ```html <vxe-column type="checkbox" width="60" align="center"></vxe-column> ``` 设置 `align="center"` 可以让复选框在列中居中显示,使界面更加美观。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值