Vue
by deniro
HTML
<div id="app" v-cloak>
<v-table :data="data" :columns="columns"></v-table>
<button @click="add">新增</button>
</div>
CSS
[v-cloak] {
display: none;
}
table {
width: 100%;
margin-bottom: 24px;
/*合并边框模型*/
border-collapse: collapse;
border-spacing: 0;
/*在空单元格周围绘制边框*/
empty-cells: show;
border: 1px solid #e9e9e9;
}
table th {
font: bold 14px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
background: #CAE8EA;
color: #5c6b77;
/*设置文本粗细*/
font-weight: 600;
/*段落中的文本不进行换行*/
white-space: nowrap;
border-top: 1px solid #C1DAD7;
}
table td, table th {
padding: 8px 16px;
text-align: left;
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
}
table th a {
/*不独占一行的块级元素*/
display: inline-block;
margin: 0 4px;
cursor: pointer;
}
table th a.on {
color: #3399ff;
}
table th a:hover {
color: #3399ff;
}
Vue
Vue.component('vTable', {
props: {
//表头列名称
columns: {
type: Array,
default: function () {
return [];
}
},
//数据
data: {
type: Array,
default: function () {
return [];
}
}
},
//为了不影响原始数据,这里定义了相应的需要操作的数据对象
data: function () {
return {
currentColumns: [],
currentData: []
}
},
//render 实现方式
render: function (h) {
var that = this;
/**
* 创建列样式与表头
*/
var ths = [];//<th> 标签数组
var cols = [];//<cols> 标签数组
this.currentColumns.forEach(function (col, index) {
if (col.width) {//创建列样式
cols.push(h('col', {
style: {
width: col.width
}
}))
}
if (col.sortable) {
ths.push(h('th', [
h('span', col.title),
//升序
h('a', {
class: {
on: col.sortType === 'asc'
},
on: {
click: function () {
that.sortByAsc(index)
}
}
}, '↑'),
//降序
h('a', {
class: {
on: col.sortType === 'desc'
},
on: {
click: function () {
that.sortByDesc(index);
}
}
}, '↓')
]));
} else {
ths.push(h('th', col.title));
}
});
/**
* 创建内容
*/
var trs =...