实现功能:element-ui table表头开始单元格 添加元素。
直接上代码:
html
<el-table-column type="expand" width="64" label-class-name="use" >
//...这里面是其余的代码
</el-table>
js
mounted(){
this.setLabel();
},
setLabel() {
let _this = this;
this.$nextTick(() => {
let cellDiv = document.getElementsByClassName('cell use')[0]
cellDiv.setAttribute("style","cursor:pointer");
let node = document.createElement('i')
node.setAttribute('class', 'el-icon el-icon-arrow-right')
node.setAttribute('is-expand', 'no'); //设置属性is-expand的值
node.onclick = function () {
_this.cellDivClick.call(_this, node);
}
cellDiv.appendChild(node)
})
},
cellDivClick(ele) {
let state = ele.getAttribute('is-expand'); //获取属性is-expand的值
//关闭的情况点击
if (state === 'no') {
//展开
for(let i=0; i<this.systemInfoList.length; i++){
this.expands.push(this.systemInfoList[i].systemId)
}
ele.setAttribute('class', 'el-icon el-icon-arrow-down')
ele.setAttribute('is-expand', 'yes')
} else if (state === 'yes') {
//展开的情况点击关闭
this.expands = [];
ele.setAttribute('class', 'el-icon el-icon-arrow-right')
ele.setAttribute('is-expand', 'no')
}
},
总结 使用过directives 不好使,最后改成这种方式。虽然解决了,但是留有遗憾。因为指令不能成功实现。