antd vue table跨行合并單元格,并且自定義內(nèi)容實(shí)例
ant-design-vue版本:~1.3.8
需求:表格實(shí)現(xiàn)跨行合并,并且在合并完的單元格中顯示圖片
效果圖:

源碼:
export default {
data() {
return {
pic95: require('@/assets/produit/95.png'),
pic99: require('@/assets/produit/99.png'),
varTable: {
cloumns: [
{
title: '置信度',
dataIndex: 'confidence ',
class: 'confidence',
customRender: (value, row, index) => {
let obj = {
children: '',
attrs: {}
}
if (index === 0) {
obj = {
children: <div class="risk-pic"><img src={this.pic95} /></div>,
attrs: { rowSpan: 4 }
}
}
if (index === 4) {
obj = {
children: <div class="risk-pic"><img src={this.pic99} /></div>,
attrs: { rowSpan: 4 }
}
}
if ([1, 2, 3, 5, 6, 7].indexOf(index) !== -1) {
obj.attrs.colSpan = 0
}
return obj
}
},
{
title: '天數(shù)',
dataIndex: 'window_length',
width: '25%',
customRender: (text) => text + '日'
},
{
title: 'VaR(萬元)',
dataIndex: 'var',
width: '25%'
},
{
title: 'VaR/凈資產(chǎn)',
dataIndex: 'var_rate',
width: '25%',
customRender: (text) => fmtRatio(text, 2)
}
],
data: [
{window_length: 1, var: 151.69, var_rate: 0.01858},
{window_length: 5, var: 298.94, var_rate: 0.03661},
{window_length: 10, var: 416.70, var_rate: 0.05104},
{window_length: 20, var: 576.04, var_rate: 0.07055},
{window_length: 1, var: 370.64, var_rate: 0.045398},
{window_length: 5, var: 463.33, var_rate: 0.056751},
{window_length: 10, var: 632.91, var_rate: 0.077523},
{window_length: 20, var: 1233.95, var_rate: 0.15114}
]
}
}
},
methods:{
// 百分?jǐn)?shù)設(shè)置
fmtRatio(val, index, def) {
// index默認(rèn)值為2
var index = arguments[1] ? arguments[1] : 2
if (val == null || isNaN(val) || typeof (val) === 'string' && val === '') {
return def || '--'
}
var ratio = (val * 100).toFixed(index)
return ratio + '%'
}
}
}
導(dǎo)入圖片的方式還有
import pic95 from '@/assets/produit/95.png'
import pic99 from '@/assets/produit/99.png'
如果有問題,歡迎提出,一起交流~~!
補(bǔ)充知識(shí):ant-design vue table 可選列、自定義列實(shí)現(xiàn)
實(shí)現(xiàn)ant-design for vue 自定義列實(shí)現(xiàn)。點(diǎn)擊按鈕,彈窗顯示所有列的checkbox,選擇checkbox,確定即可實(shí)現(xiàn)自定義列。先上代碼
<script>
/**
* 該組件為實(shí)現(xiàn)table可選列。
* 具體操作見下方注釋。
* 全部集成原a-table功能,使用方式與原a-table完全相同,擴(kuò)展增加了可選列功能
* 該組件已注冊(cè)至全局,使用方式只需將a-table變?yōu)閦yx-table即可,等等一系列原寫法不變,即可增加該功能.
* 采用rander函數(shù)模式寫,為了實(shí)現(xiàn)a-table中slot可動(dòng)態(tài)。
*/
export default {
name: 'Table',
data () {
return {
modalVisible: false, // 彈窗
columns: [], // 表格的列,根據(jù)條件來操作該字段
selectList: [], // 已選擇的列
temporarySelectData: [], // 暫時(shí)選擇的列,點(diǎn)擊checkbox暫存到該字段,點(diǎn)確定同步到selectList
checkboxList: []// checkbox的list,也做總數(shù)據(jù)來使用
}
},
mounted () {
/**
* 掛載后,將原columns復(fù)制到本頁columns,checkboxList
* 將selectList賦值全選狀態(tài)
*/
this.columns = this.deepClone(this.$attrs.columns)
this.checkboxList = this.deepClone(this.$attrs.columns)
this.selectList = this.columns.map(ele => ele.dataIndex)
},
methods: {
/**
* 打開modal,將checkbox默認(rèn)值或者是選擇值(暫存)重新賦值
*/
handelOpenSelect () {
this.temporarySelectData = this.deepClone(this.selectList)
this.modalVisible = true
},
/**
* 點(diǎn)擊確定,將暫存值賦值(temporarySelectData)給已選擇的列(selectList)
* 將列(columns)根據(jù)已選擇的列(selectList)篩選出來
*/
handleOk () {
this.selectList = this.deepClone(this.temporarySelectData)
this.modalVisible = false
this.columns = this.checkboxList.filter(ele => this.selectList.includes(ele.dataIndex))
},
handleCancel () {
this.modalVisible = false
},
handelChange (e) {
this.temporarySelectData = this.deepClone(e)
},
deepClone (target) {
let result
if (typeof target === 'object') {
if (Array.isArray(target)) {
result = []
for (const i in target) {
result.push(this.deepClone(target[i]))
}
} else if (target === null) {
result = null
} else if (target.constructor === RegExp) {
result = target
} else {
result = {}
for (const i in target) {
result[i] = this.deepClone(target[i])
}
}
} else {
result = target
}
return result
}
},
render () {
const props = { ...this.$attrs, ...this.$props, ...{ columns: this.columns } }
const on = { ...this.$listeners }
const slots = Object.keys(this.$slots).map(slot => {
return (
<template slot={slot}>{ this.$slots[slot] }</template>
)
})
const table = (
<a-table props={props} scopedSlots={ this.$scopedSlots } on={on} ref="zyxTable">
{slots}
</a-table>
)
const changeDiv = (
<a-button class="select-columns" size="small" onClick={this.handelOpenSelect}>列</a-button>
)
const checkboxArr = []
for (let i = 0; i < this.checkboxList.length; i++) {
checkboxArr.push(<a-col span={8}><a-checkbox value={this.checkboxList[i].dataIndex}>{this.checkboxList[i].title}</a-checkbox></a-col>)
}
const modal = (
<a-modal
title="設(shè)置列"
visible={this.modalVisible}
onOk={this.handleOk}
onCancel={this.handleCancel}>
<a-checkbox-group value={this.temporarySelectData} onChange={this.handelChange}>
<a-row>
{checkboxArr}
</a-row>
</a-checkbox-group>
</a-modal>
)
return (
<div class="zyx-table">
{ table }
{ changeDiv }
{ modal }
</div>
)
}
}
</script>
<style lang="less" scoped>
.zyx-table{
position: relative;
margin-top: 20px;
.select-columns{
position: absolute;
right: 0;
top: -30px;
}
}
.ant-row{
width: 100%;
.ant-col-8{
margin-bottom: 10px;
}
}
.ant-checkbox-group{
width: 100%;
}
</style>
該組件二次封裝了a-table,集成原a-table所有方法
使用方法,在全局注冊(cè)該組件,將原a-table替換為zyx-table即可實(shí)現(xiàn)。
將原標(biāo)簽替換為rander函數(shù),是為了實(shí)現(xiàn)slot動(dòng)態(tài)傳入的效果。
有疑問或者更好的建議,歡迎光臨思密達(dá)。github傳送門
以上這篇antd vue table跨行合并單元格,并且自定義內(nèi)容實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3在table里使用elementUI的form表單驗(yàn)證的示例代碼
這篇文章主要介紹了vue3在table里使用elementUI的form表單驗(yàn)證的示例代碼,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-12-12
vue使用JSON編輯器:vue-json-editor詳解
文章介紹了如何在Vue項(xiàng)目中使用JSON編輯器插件`vue-json-editor`,包括安裝、引入、注冊(cè)和使用示例,通過這些步驟,用戶可以在Vue應(yīng)用中輕松實(shí)現(xiàn)JSON數(shù)據(jù)的編輯功能,文章最后呼吁大家支持腳本之家2025-01-01
vue頁面引入three.js實(shí)現(xiàn)3d動(dòng)畫場景操作
這篇文章主要介紹了vue頁面引入three.js實(shí)現(xiàn)3d動(dòng)畫場景操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
使用vue-router beforEach實(shí)現(xiàn)判斷用戶登錄跳轉(zhuǎn)路由篩選功能
這篇文章主要介紹了vue使用vue-router beforEach實(shí)現(xiàn)判斷用戶登錄跳轉(zhuǎn)路由篩選,本篇文章默認(rèn)您已經(jīng)會(huì)使用 webpack 或者 vue-cli 來進(jìn)行環(huán)境的搭建,并且具有一定的vue基礎(chǔ)。需要的朋友可以參考下2018-06-06
Vue.js 2.0 移動(dòng)端拍照壓縮圖片預(yù)覽及上傳實(shí)例
這篇文章主要介紹了Vue.js 2.0 移動(dòng)端拍照壓縮圖片預(yù)覽及上傳實(shí)例,本來移動(dòng)端開發(fā)H5應(yīng)用,準(zhǔn)備將mui框架和Vue.js+vue-router+vuex 全家桶結(jié)合起來使用2017-04-04

