fixed #1195 and abandon old Table render function
This commit is contained in:
Aresn 2017-06-30 16:26:31 +08:00
parent 4f5766117d
commit e40c5352a1
4 changed files with 45 additions and 105 deletions

View file

@ -11,6 +11,7 @@
{ {
type: 'expand', type: 'expand',
render: (h) => { render: (h) => {
console.log('______hover______');
return h(etable); return h(etable);
}, },
width: 50 width: 50

View file

@ -19,11 +19,9 @@
</div> </div>
</template> </template>
<script> <script>
import Vue from 'vue';
import Cell from './expand'; import Cell from './expand';
import Icon from '../icon/icon.vue'; import Icon from '../icon/icon.vue';
import Checkbox from '../checkbox/checkbox.vue'; import Checkbox from '../checkbox/checkbox.vue';
import { findComponentUpward } from '../../utils/assist';
export default { export default {
name: 'TableCell', name: 'TableCell',
@ -46,7 +44,7 @@
return { return {
renderType: '', renderType: '',
uid: -1, uid: -1,
context: this.$parent.$parent.currentContext context: this.$parent.$parent.$parent.currentContext
}; };
}, },
computed: { computed: {
@ -70,76 +68,11 @@
} }
}, },
methods: { methods: {
compile () {
if (this.column.render && this.renderType === 'render') {
// todo Render
let isRealRender = true;
const Table = findComponentUpward(this, 'Table');
if (Table.context) isRealRender = false;
if (isRealRender) {
// this.$el.innerHTML = '';
// const component = new Vue({
// functional: true,
// render: (h) => {
// return this.column.render(h, {
// row: this.row,
// column: this.column,
// index: this.index
// });
// }
// });
// const Cell = component.$mount();
// this.$refs.cell.appendChild(Cell.$el);
} else {
const $parent = this.context;
const template = this.column.render(this.row, this.column, this.index);
const cell = document.createElement('div');
cell.innerHTML = template;
this.$el.innerHTML = '';
let methods = {};
Object.keys($parent).forEach(key => {
const func = $parent[key];
if (typeof(func) === 'function' && (func.name === 'boundFn' || func.name === 'n')) {
methods[key] = func;
}
});
const res = Vue.compile(cell.outerHTML);
// 使 component
const components = {};
Object.getOwnPropertyNames($parent.$options.components).forEach(item => {
components[item] = $parent.$options.components[item];
});
const component = new Vue({
render: res.render,
staticRenderFns: res.staticRenderFns,
methods: methods,
data () {
return $parent._data;
},
components: components
});
if ($parent.$store != undefined) {
component.$store = $parent.$store;
}
component.row = this.row;
component.column = this.column;
const Cell = component.$mount();
this.$refs.cell.appendChild(Cell.$el);
}
}
},
destroy () {
},
toggleSelect () { toggleSelect () {
this.$parent.$parent.toggleSelect(this.index); this.$parent.$parent.$parent.toggleSelect(this.index);
}, },
toggleExpand () { toggleExpand () {
this.$parent.$parent.toggleExpand(this.index); this.$parent.$parent.$parent.toggleExpand(this.index);
} }
}, },
created () { created () {
@ -154,20 +87,6 @@
} else { } else {
this.renderType = 'normal'; this.renderType = 'normal';
} }
},
mounted () {
this.$nextTick(() => {
this.compile();
});
},
beforeDestroy () {
this.destroy();
},
watch: {
naturalIndex () {
this.destroy();
this.compile();
}
} }
}; };
</script> </script>

View file

@ -5,12 +5,13 @@
</colgroup> </colgroup>
<tbody :class="[prefixCls + '-tbody']"> <tbody :class="[prefixCls + '-tbody']">
<template v-for="(row, index) in data"> <template v-for="(row, index) in data">
<tr <table-tr
:class="rowClasses(row._index)" :row="row"
@mouseenter.stop="handleMouseIn(row._index)" :prefix-cls="prefixCls"
@mouseleave.stop="handleMouseOut(row._index)" @mouseenter.native.stop="handleMouseIn(row._index)"
@click.stop="clickCurrentRow(row._index)" @mouseleave.native.stop="handleMouseOut(row._index)"
@dblclick.stop="dblclickCurrentRow(row._index)"> @click.native.stop="clickCurrentRow(row._index)"
@dblclick.native.stop="dblclickCurrentRow(row._index)">
<td v-for="column in columns" :class="alignCls(column, row)"> <td v-for="column in columns" :class="alignCls(column, row)">
<Cell <Cell
:fixed="fixed" :fixed="fixed"
@ -25,7 +26,7 @@
:expanded="rowExpanded(row._index)" :expanded="rowExpanded(row._index)"
></Cell> ></Cell>
</td> </td>
</tr> </table-tr>
<tr v-if="rowExpanded(row._index)"> <tr v-if="rowExpanded(row._index)">
<td :colspan="columns.length" :class="prefixCls + '-expanded-cell'"> <td :colspan="columns.length" :class="prefixCls + '-expanded-cell'">
<Expand :key="row" :row="row" :render="expandRender" :index="row._index"></Expand> <Expand :key="row" :row="row" :render="expandRender" :index="row._index"></Expand>
@ -37,6 +38,7 @@
</template> </template>
<script> <script>
// todo :key="row" // todo :key="row"
import TableTr from './table-tr.vue';
import Cell from './cell.vue'; import Cell from './cell.vue';
import Expand from './expand.js'; import Expand from './expand.js';
import Mixin from './mixin'; import Mixin from './mixin';
@ -44,7 +46,7 @@
export default { export default {
name: 'TableBody', name: 'TableBody',
mixins: [ Mixin ], mixins: [ Mixin ],
components: { Cell, Expand }, components: { Cell, Expand, TableTr },
props: { props: {
prefixCls: String, prefixCls: String,
styleObject: Object, styleObject: Object,
@ -72,16 +74,6 @@
} }
}, },
methods: { methods: {
rowClasses (_index) {
return [
`${this.prefixCls}-row`,
this.rowClsName(_index),
{
[`${this.prefixCls}-row-highlight`]: this.objData[_index] && this.objData[_index]._isHighlight,
[`${this.prefixCls}-row-hover`]: this.objData[_index] && this.objData[_index]._isHover
}
];
},
rowChecked (_index) { rowChecked (_index) {
return this.objData[_index] && this.objData[_index]._isChecked; return this.objData[_index] && this.objData[_index]._isChecked;
}, },
@ -91,9 +83,6 @@
rowExpanded(_index){ rowExpanded(_index){
return this.objData[_index] && this.objData[_index]._isExpanded; return this.objData[_index] && this.objData[_index]._isExpanded;
}, },
rowClsName (_index) {
return this.$parent.rowClassName(this.objData[_index], _index);
},
handleMouseIn (_index) { handleMouseIn (_index) {
this.$parent.handleMouseIn(_index); this.$parent.handleMouseIn(_index);
}, },

View file

@ -0,0 +1,31 @@
<template>
<tr :class="rowClasses(row._index)"><slot></slot></tr>
</template>
<script>
export default {
props: {
row: Object,
prefixCls: String
},
computed: {
objData () {
return this.$parent.objData;
}
},
methods: {
rowClasses (_index) {
return [
`${this.prefixCls}-row`,
this.rowClsName(_index),
{
[`${this.prefixCls}-row-highlight`]: this.objData[_index] && this.objData[_index]._isHighlight,
[`${this.prefixCls}-row-hover`]: this.objData[_index] && this.objData[_index]._isHover
}
];
},
rowClsName (_index) {
return this.$parent.$parent.rowClassName(this.objData[_index], _index);
},
}
};
</script>