国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

ant design vue 表格table 默認勾選幾項的操作

瀏覽:5日期:2022-11-08 08:02:20

為什么我同樣的功能要用react 、vue 都寫一遍 ?

啊我真是不是閑的蛋疼啊(~ o ~)~zZ

在 ant design vue 中,表格的第一列是聯(lián)動的選擇框

截一張官方文檔圖,圖示最后一排就是禁用狀態(tài)

ant design vue 表格table 默認勾選幾項的操作

點擊 checkbox 會觸發(fā)onChange , 從而得到selectedRowKeys,selectedRowKeys就是選中的 key 數(shù)組。

onChange: (selectedRowKeys, selectedRows) => { console.log(`selectedRowKeys: ${selectedRowKeys}`, ’selectedRows: ’, selectedRows); },

默認禁用disable 某項時,官方文檔給出了例子:

rowSelection() { const { selectedRowKeys } = this; return { onChange: (selectedRowKeys, selectedRows) => { console.log(`selectedRowKeys: ${selectedRowKeys}`, ’selectedRows: ’, selectedRows); }, getCheckboxProps: record => ({ props: { disabled: record.name === ’Disabled User’, // Column configuration not to be checked name: record.name, } }), } }

主要是getCheckboxProps 里面的disabled 屬性控制的。

默認選中某項時,需要 getCheckboxProps 里面的defaultChecked 屬性控制:

業(yè)務場景:勾選了幾項保存之后,下次進來編輯還是需要展示之前勾選的項,這時候就用到了默認勾選的屬性

之前只貼了核心邏輯,好多人好像沒看懂,我把整體的都貼上來了。

核心代碼defaultChecked: selectedRowKeys.includes(record.id) 的思路就是所有表格里所有包含已選中項的id,都給他默認選中

data () { return { // ... record: ’’, rowSelection: { selectedRowKeys: [], onChange: this.onSelectChange } }, methods: { handleEdit (record) { //...省略我的業(yè)務邏輯 if (record) { //...省略我的業(yè)務邏輯 let selectedRowKeys = (record.roleIdList.length > 0 && record.roleIdList.split(’,’)) || []; this.rowSelection = { selectedRowKeys: selectedRowKeys, onChange: this.onSelectChange, getCheckboxProps: record => { return { props: {defaultChecked: selectedRowKeys.includes(record.id) } }; } }; } else { this.record = ’’; this.rowSelection = { selectedRowKeys: [], onChange: this.onSelectChange } } }, onSelectChange (selectedRowKeys) { // 去重 Array.from(new Set(arr)) this.rowSelection.selectedRowKeys = Array.from(new Set(selectedRowKeys)); } }

ant design vue 版本和 react 版本寫法略有不同,disabled 和 defaultChecked 都掛在了props 屬性下。

補充知識:Ant-Design-Pro中Table組件rowSelection方法的一些坑

如下所示:

<Table rowSelection={rowSelection} columns={columns} dataSource={data} />

在 <Table/> 組件中有 rowSelection={rowSelection} 方法,可以讓Table的第一列成為聯(lián)動的選擇框。

API中說到通過 rowSelection.selectedRowKeys 來控制選中項。比較坑的是,selectedRowKeys 控制的只是dataSource當前的順序編號。

一定要加上rowKey='id'或者rowKey={record => record.id},后來經(jīng)過多次調(diào)試發(fā)現(xiàn)很多BUG都跟這個參數(shù)有關(guān),不然會導致聯(lián)動的選擇框狀態(tài)異常。id可以自定義為dataSource中的某個值。

以上這篇ant design vue 表格table 默認勾選幾項的操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 祁阳县| 万宁市| 台湾省| 商南县| 镇平县| 南昌市| 保靖县| 奈曼旗| 抚顺县| 淳化县| 武隆县| 长泰县| 南华县| 梁河县| 重庆市| 太和县| 富阳市| 贵溪市| 富川| 剑阁县| 西畴县| 沧源| 莆田市| 南京市| 东兰县| 抚州市| 红安县| 潞西市| 德昌县| 庆元县| 屏东县| 宜阳县| 桃园市| 新兴县| 紫金县| 土默特右旗| 奇台县| 都昌县| 平乐县| 民丰县| 和政县|