如何在 ant 的table中實(shí)現(xiàn)圖片的渲染操作
如何在 ant 的table中實(shí)現(xiàn)圖片的渲染
在使用react 的螞蟻金服的ui庫的時(shí)候,,平時(shí)用到的比較比較多的就是table組件,但是在ant官網(wǎng)上面,并沒有在后臺(tái)調(diào)取接口獲取數(shù)據(jù)后,,如何將后臺(tái)的http://lkjlkjlkj.jpg圖片渲染到每一行的例子。。只有一個(gè)render的方法。。。查閱了一些資料,作為一個(gè)不是很資深的前端來說,忙活了一上午,。實(shí)現(xiàn)了這個(gè)功能。。。記錄一下。。。
這里是table的使用
<Table selectHandle={false} onCtrlClick={ this.tableAction } header={this.tableHeader()} pagination={ true } scroll = {{y:450}} pageSize={10} getpage={this.getpage} currentPage={this.state.currentPage} data={this.state.dataSource} checkChang={this.checkChang} />
table中theader的渲染
tableHeader = () => { return [{ dataIndex: '', title: 'Logo', width: 150, key : 'image', render:(record) => { return <img src={record.image} alt="" style={{width:'5 0px',height:'50px',borderRadius:'50%'}}/> } },{ dataIndex: 'name', title: '名稱', width: 150, key : 'name' },{ dataIndex: 'label', title: '標(biāo)簽', width: 150, key : 'label' },{ dataIndex: 'collectCount', title: '關(guān)注數(shù)', width: 150, key : 'collectCount' }, { dataIndex: 'topicCount', title: '帖子數(shù)', width: 150, key : 'topicCount' },{ dataIndex: 'inTime', title: '創(chuàng)建時(shí)間', width: 150, key : 'inTime' }] }
利用table 中 render的屬性,,,將LOGO在tableHeader中render return一個(gè)img 標(biāo)簽,并將src={ record.image }
圖片就正確的渲染到你的table中了。。拿走 不謝~~~
補(bǔ)充知識(shí):ant design table 數(shù)據(jù)渲染,插槽使用
我就廢話不都說了,大家還是直接看代碼吧~
<a-table :columns="columns" :dataSource="dataList" :loading="loading" :pagination="false" :rowKey="(record,index) => index"> <template slot="duty" slot-scope="text, record, index"> <span v-if="text == 'general'">普通員工</span> <span v-if="text == 'expert'">專家</span> <span v-if="text == 'admin'">管理員</span> </template> <template slot="status" slot-scope="text, record, index"> <span v-if="text == '1'">正常</span> <span v-if="text == '0'">失效</span> </template> <template slot="action" slot-scope="text, record, index"> <a-button type="primary" size="small" @click="editUser(record)">編輯</a-button> </template> </a-table> // script 部分 data(){ return { columns:[ { title: '用戶賬號(hào)', dataIndex: 'username', }, { title: '姓名', dataIndex: 'name', }, { title: '角色', dataIndex: 'duty', scopedSlots: {customRender: 'duty'} }, { title: '狀態(tài)', dataIndex: 'status', scopedSlots: {customRender: 'status'} }, { title: '操作', dataIndex: 'action', scopedSlots: {customRender: 'action'}, }], dataList: [], loading: false, } }, created(){ this.getList() }, methods: { getList(){ this.loading = true; this.$http.get('/getUsers.do').then(res => { if(res){ this.dataList = res || [] } this.loading = false; }).catch(err => { console.log(err) }) }, editUser(record){ this.$refs.addModal.showModal(record) }, }
1.columns 定義table 表頭,以及和 dataList 的字段對(duì)應(yīng),
2. dataSource 為數(shù)據(jù)源,是一個(gè)數(shù)組,
3.loading 加載時(shí)loading,數(shù)據(jù)請(qǐng)求前設(shè)置 true,請(qǐng)求完成后設(shè)置 false,
4.插槽的使用
很多情況下,后端返回的數(shù)據(jù)是 數(shù)字,前端需要展示文字,這事使用插槽就會(huì)非常方便
1.首先,在 columns 中需要的部分添加 scopedSlots: {customRender: ‘status'}
2.table 中添加標(biāo)簽
<template slot="status" slot-scope="text, record, index"> <span v-if="text == '1'">正常</span> <span v-if="text == '0'">失效</span> </template>
customRender 的值和slot 的值相對(duì)應(yīng),slot-scope 中 text就是status的值(text可以自定義,key,item都可以), record 代表text所在的對(duì)象,可以通過 record 拿到該行的其他值. 比如
editUser(record){ this.$refs.addModal.showModal(record) },
把record作為參數(shù)傳遞,編輯改用戶信息.
以上這篇如何在 ant 的table中實(shí)現(xiàn)圖片的渲染操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用vue3實(shí)現(xiàn)一個(gè)人喵交流小程序
Vue3 在經(jīng)過多個(gè)開發(fā)版本的迭代后,終于迎來了它的正式版本,下面這篇文章主要給大家介紹了關(guān)于如何使用vue3實(shí)現(xiàn)一個(gè)人喵交流小程序的相關(guān)資料,需要的朋友可以參考下2021-11-11vue組件中節(jié)流函數(shù)的失效的原因和解決方法
這篇文章主要介紹了vue組件中節(jié)流函數(shù)的失效和解決方法,幫助大家更好的理解和學(xué)習(xí)vue框架,感興趣的朋友可以了解下2020-12-12關(guān)于Vue?CLI3中啟動(dòng)cli服務(wù)參數(shù)說明
這篇文章主要介紹了關(guān)于Vue?CLI3中啟動(dòng)cli服務(wù)參數(shù)說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue的url請(qǐng)求圖片的問題及請(qǐng)求失敗解決
這篇文章主要介紹了vue的url請(qǐng)求圖片的問題及請(qǐng)求失敗解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09ant design vue中table表格滾動(dòng)加載實(shí)現(xiàn)思路
在處理一寫數(shù)據(jù)量特別大的情況下,我們不能把后端的數(shù)據(jù)一次性全部拿到前端在table表格中展示,為了考慮性能優(yōu)化,使用了滾動(dòng)加載表格數(shù)據(jù),這篇文章主要介紹了ant design vue中table表格滾動(dòng)加載實(shí)現(xiàn)思路,需要的朋友可以參考下2024-07-07vue3.2自定義彈窗組件結(jié)合函數(shù)式調(diào)用示例詳解
這篇文章主要為大家介紹了vue3.2自定義彈窗組件結(jié)合函數(shù)式調(diào)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09vue實(shí)現(xiàn)簡易的雙向數(shù)據(jù)綁定
這篇文章主要介紹了vue如何實(shí)現(xiàn)簡易的雙向數(shù)據(jù)綁定,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12