日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

vue中用table_Ant-Design-Vue中关于Table组件的使用

發(fā)布時(shí)間:2025/3/13 58 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vue中用table_Ant-Design-Vue中关于Table组件的使用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 如何自定義表格列頭:

Name

const columns =[

{

dataIndex:'name',    // 自定義列表頭,則不能設(shè)置title屬性align:'left',

slots: { title:'customTitle'}  // 在這里定義一個(gè)slots屬性,并設(shè)置一個(gè)title屬性

}

]

頁(yè)面將會(huì)渲染為如下:

2.如何設(shè)置自定義單行樣式

// 這里傳入的值分別是:record:當(dāng)前行的原始數(shù)據(jù),index:當(dāng)前行的索引編輯

const columns =[

{

title:'菜單名稱'dataIndex:'name',  // dataIndex的值對(duì)應(yīng)的是,列數(shù)據(jù)在數(shù)據(jù)項(xiàng)中對(duì)應(yīng)的 keykey: 'name',     // 如果dataIndex屬性值唯一,則可以不設(shè)置key屬性

align:'left',

},

{

title:'操作',

key: 'action'

dataIndex:'action',

width:'30%',

scopedSlots: { customRender:'action' }, //這一行自定義渲染這一列

align: 'center'}

]

頁(yè)面展示如下:

3.如何設(shè)置表頭,頁(yè)腳和邊框線?

  // 這里添加bordered屬性,就可以添加上邊框線

{{text}}

  // slot="title"就可以設(shè)置頁(yè)頭了,'title'改為其他值則沒(méi)有頁(yè)頭Header--{{currentPageData}}    // 這里打印一下currentData,看下是啥值

Footer  // 跟上同理

const columns =[  // columns中并沒(méi)有定義頁(yè)頭和頁(yè)腳的相關(guān)代碼

{

title:'Name',

dataIndex:'name',

scopedSlots: { customRender:'name'},

},

{

title:'Cash Assets',

className:'column-money',

dataIndex:'money',

},

{

title:'Address',

dataIndex:'address',

},

];

頁(yè)面顯示就結(jié)果如下:

4.表格如何樹形展示數(shù)據(jù):

:rowSelection="rowSelection">// rowSelection是列表可選擇的時(shí)候的配置項(xiàng),后面介紹,帶有此選項(xiàng)表格前就會(huì)出現(xiàn)可選擇的checkbox Name

編輯

const columns =[

{

dataIndex:'name',

key:'name',

align:'left',

slots: { title:'customTitle'}

},

{

title:'操作',

dataIndex:'action',

width:'30%',

scopedSlots: { customRender:'action'},

align:'center'}

]

const dataSource=[

{

key:1,

name:'John Brown sr.',

age:60,

address:'New York No. 1 Lake Park',

qq: [//這里我把子節(jié)點(diǎn)的key,改為qq了

{

key:11,

name:'John Brown',

age:42,

address:'New York No. 2 Lake Park'}

]

},

{

key:2,

name:'Joe Black',

age:32,

address:'Sidney No. 1 Lake Park'}

]

頁(yè)面顯示效果如下:(顯示正確)

5.自定義篩選菜單:(下面的代碼都太多了,有必要在點(diǎn)開看吧,有詳細(xì)的注釋)

slot="filterDropdown"slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"style="padding: 8px"

>

v-ant-ref="c => searchInput = c":placeholder="`Search ${column.dataIndex}`":value="selectedKeys[0]"@change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"@pressEnter="() => handleSearch(selectedKeys, confirm)"style="width: 188px; margin-bottom: 8px; display: block;"

/>

type="primary"@click="() => handleSearch(selectedKeys, confirm)"icon="search"size="small"style="width: 90px; margin-right: 8px"

>Search

handleReset(clearFilters)" size="small" style="width: 90px"

>Reset

slot="filterIcon"slot-scope="filtered"type="search":style="{ color: filtered ? 'red' : undefined }"

/>

v-for="(fragment, i) in text.toString().split(new RegExp(`(?<=${searchText})|(?=${searchText})`, 'i'))"

>

v-if="fragment.toLowerCase() === searchText.toLowerCase()":key="i"class="highlight"

>{{fragment}}

{{fragment}}

{{text}}

exportdefault{

data () {return{

data,

searchText:'',

searchInput:null,

columns: [

{

title:'Name',

dataIndex:'name',

key:'name',//這里的三個(gè)插槽,分別是搜索按鈕插槽,定義搜索按鈕樣式插槽,和搜索之后的數(shù)據(jù)插槽

scopedSlots: {

filterDropdown:'filterDropdown',

filterIcon:'filterIcon',

customRender:'customRender'},//這里才是確定篩選的運(yùn)行函數(shù)

onFilter: (value, record) =>record.name.toString().toLowerCase().includes(value.toLowerCase()),//自定義篩選菜單可見(jiàn)變化時(shí)調(diào)用

onFilterDropdownVisibleChange: visible =>{if(visible) {

setTimeout(()=>{this.searchInput.focus()

},0)

}

}

},{......}//省略了部分配置

]

}

},

methods: {

handleSearch (selectedKeys, confirm) {

confirm();//confirm會(huì)關(guān)閉搜索框

console.log(selectedKeys) //會(huì)打印出你在搜索框中輸入的值

this.searchText = selectedKeys[0]

},

handleReset (clearFilters) {

clearFilters();//=> 這里面也有調(diào)用confirm方法關(guān)閉對(duì)話框

this.searchText = ''}

}

}

View Code

6.如何自定義可以編輯單行的表格?

v-for="col in ['name', 'age', 'address']":slot="col"slot-scope="text, record, index"

>

v-if="record.editable"style="margin: -5px 0":value="text"@change="e => handleChange(e.target.value, record.key, col)"

/>

{{text}}

save(record.key)">Save

cancel(record.key)">

Cancel

edit(record.key)">Edit

{title:'name',dataIndex: 'name',width: '25%',scopedSlots: { customRender: 'name'}},

{title:'age',dataIndex: 'age',width: '15%',scopedSlots: { customRender: 'age'}},

{title:'address',dataIndex: 'address',width: '40%',scopedSlots: { customRender: 'address'}},

{title:'operation',dataIndex: 'operation',scopedSlots: { customRender: 'operation'}}

];

const data=[];for (let i = 0; i < 100; i++) {

data.push({

key: i.toString(),

name: `Edrward ${i}`,

age:32,

address: `London Park no. ${i}`,

});

}

exportdefault{

data() {this.cacheData = data.map(item => ({ ...item })); //緩存所有數(shù)據(jù)

return{

data,

columns,

};

},

methods: {/**

* input的change的回調(diào)方法

* @param value input框中你輸入的值

* @param key 當(dāng)前行對(duì)應(yīng)的key值

* @param column 當(dāng)前列的dataIndex對(duì)應(yīng)的名稱,有['name','age','address']*/handleChange(value, key, column) {

const newData= [...this.data];

const target= newData.filter(item => key === item.key)[0];

console.log(column);if(target) {

target[column]=value;this.data =newData;

}

},/**

* 點(diǎn)擊操作欄中修改的回調(diào)方法

* @param key 當(dāng)前行對(duì)應(yīng)的key值*/edit(key) {

const newData= [...this.data];//直接獲取了所有數(shù)據(jù)

const target = newData.filter(item => key === item.key)[0]; //在篩選出key值相同的那一條數(shù)據(jù)

if (target) { //如果數(shù)據(jù)存在,則給這條數(shù)據(jù)新增一個(gè)屬性為editable屬性為true => 代表為正在更改中

target.editable = true;this.data =newData;

}

},/**

* 修改完成之后點(diǎn)擊保存的回調(diào)方法

* @param key 當(dāng)前行對(duì)應(yīng)的key值*/save(key) {

const newData= [...this.data];

const newCacheData= [...this.cacheData];

const target= newData.filter(item => key === item.key)[0];

const targetCache= newCacheData.filter(item => key === item.key)[0];if (target &&targetCache) {delete target.editable; //刪除editable屬性

this.data =newData;

Object.assign(

targetCache,

target

);this.cacheData =newCacheData;

}

},/**

* 點(diǎn)擊取消編輯的回調(diào)方法

* @param key 當(dāng)前行對(duì)應(yīng)的key值*/cancel(key) {

const newData= [...this.data];

const target= newData.filter(item => key === item.key)[0];if (target) { //將緩存的值重新復(fù)制給原先的數(shù)據(jù),并刪除editable屬性

Object.assign(target, this.cacheData.filter(item => key === item.key)[0]);deletetarget.editable;this.data =newData;

}

},

},

};

margin-right: 8px;

}

View Code

7.如何定義可展開的table?

Delete

{{index}}

{{record}}--{{index}}--{{indent}}--{{expanded}}

{ title:'Name', dataIndex: 'name', key: 'name'},

{ title:'Age', dataIndex: 'age', key: 'age'},

{ title:'Address', dataIndex: 'address', key: 'address'},

{ title:'Action', dataIndex: '', key: 'x', scopedSlots: { customRender: 'action'} },

];

const data=[

{

key:1,

name:'John Brown',

age:32,

address:'New York No. 1 Lake Park',

description:'My name is John Brown, I am 32 years old, living in New York No. 1 Lake Park.',

}

];

exportdefault{

data() {return{data,columns,};

},

};

View Code

8.最后來(lái)一個(gè)帶分頁(yè)的表格

{itle:'Name',dataIndex:'name'},

{title:'Age',dataIndex:'age'},

{title:'Address',dataIndex:'address'}

]

const data=[]for(let i= 0; i< 46; i++) {

data.push({

key: i,

name: `Edward King ${i}`,

age:32,

address: `London, Park Lane no. ${i}`

})

}

exportdefault{

data () {return{

data,

columns

ipagination: {

current:1,

pageSize:10,

total: data.length,

showSizeChanger:true,

showQuickJumper:true,

pageSizeOptions: ['10','20','30'],//這里注意只能是字符串,不能是數(shù)字

showTotal: (total, range)=>`顯示${range[0]}-${range[1]}條,共有 ${total}條`

}

}

}

}

9.建議看官方組件案列中的,自定義選擇項(xiàng)案例,看完弄懂,表格的基本使用沒(méi)有問(wèn)題了。大家使用的時(shí)候遇到了什么問(wèn)題可以來(lái)溝通一下啊。。。

總結(jié)

以上是生活随笔為你收集整理的vue中用table_Ant-Design-Vue中关于Table组件的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。