本文章从以下图所示的最基本的table入手,分析table组件源代码。本人已经对table组件原来的源码进行削减,源码点击这里下载。本文只对重要的代码片断进行讲解,推荐下载代码把项目运行起来,跟着文章的思路阅读。vue
<div class="el-table"> <!-- 隐藏列: slot里容纳table-column --> <div class="hidden-columns" ref="hiddenColumns"> <slot></slot> </div> <div class="el-table__header-wrapper" ref="headerWrapper"> <table-header ref="tableHeader" :store="store"> </table-header> </div> <div class="el-table__body-wrapper" ref="bodyWrapper"> <table-body :context="context" :store="store"> </table-body> </div> </div> </template>
table、table-header、table-body、table-column之间经过table-store进行状态管理。table-header、table-body对table-store数据进行监听,每当table改变table-store数据时触发table-header、table-body从新渲染。git
table-column为列数据column绑定相应的renderCell函数,供table-body渲染时使用。table-column这个组件自身不作任何渲染。因此会看到模板将其隐藏。还有就是table-header、table-body经过render函数进行渲染。github
初始化storesegmentfault
``` data() { const store = new TableStore(this); return { store, }; } ```将store共享给table-header、table-body数组
``` <div class="el-table__header-wrapper" ref="headerWrapper"> <table-header :store="store"></table-header> </div> <div class="el-table__body-wrapper" ref="bodyWrapper"> <table-body :store="store"></table-body> </div> ```将数据存储到store,供table-body获取data将其渲染app
``` watch: { data: { immediate: true, handler(value) { // 供 table-body computed.data 使用 this.store.commit('setData', value); // ...... } }, }, ```设置tableId函数
``` created() { //..... this.tableId = `el-table_${tableIdSeed}`; //..... } ```调用 updateColumns 触发 table-header、table-body 二次render更新,标记mounted完成ui
``` mounted() { // ..... this.store.updateColumns(); // ..... this.$ready = true; } ```生成column,并为column绑定renderCell函数
供table-body使用this
给store.state._columns数组填充数据spa
``` mounted() { // ...... owner.store.commit('insertColumn', this.columnConfig, columnIndex, this.isSubColumn ? parent.columnConfig : null); } ```table-store有两个很重要的属性_columns、data,_columns保存列的相关信息,data则保存开发者传入的表格数据。还有两个重要的函数insertColumn与updateColumns。
insertColumn为_columns填充数据
``` TableStore.prototype.mutations = { insertColumn(states, column, index, parent) { let array = states._columns; // ...... if (typeof index !== 'undefined') { // 在index的位置插入column array.splice(index, 0, column); } else { array.push(column); } // ..... }, } ```updateColumns 对_columns进行过滤获得columns
``` TableStore.prototype.updateColumns = function() { const states = this.states; const _columns = states._columns || []; const notFixedColumns = _columns.filter(column => !column.fixed); // ..... const leafColumns = doFlattenColumns(notFixedColumns); // ..... states.columns = [].concat(leafColumns); // .... } ```table-header、table-body都拥有如下属性
props: { store: { required: true }, } computed: { columns() { return this.store.states.columns; }, }, render(){ // 渲染columns的数据 }
这两个组件的工做原理是监听columns数据变化以触发render渲染。在table组件的mounted阶段会调用 updateColumns 更新 columns,从而触发 table-header、table-body 从新渲染。
另外table-body还会监听data变化,触发render。例如当组件加载后发送请求,待请求响应赋值data,从新渲染table-body。
computed: { data() { // table.vue watch.data 中 调用 setData 在store 中存储 data return this.store.states.data; }, },