优化重构

This commit is contained in:
xuliangzhan
2020-03-15 13:07:25 +08:00
parent f048b349f3
commit 30bb14c26e
2 changed files with 26 additions and 18 deletions

View File

@@ -1254,7 +1254,7 @@ const apis = [
},
{
name: 'loadMethod',
desc: '该方法 Function({row}) 用于异步加载展开后的内容(必须返回 Promise<any[]> 对象)',
desc: '该方法 Function({row, row, rowIndex?, $rowIndex?}) 用于异步加载展开后的内容(必须返回 Promise<any[]> 对象)',
version: '',
type: 'Function',
enum: '',
@@ -1263,7 +1263,7 @@ const apis = [
},
{
name: 'toggleMethod',
desc: '该方法 Function({expanded,row}) 在展开或关闭触发之前调用,可以通过返回值来决定是否允许继续执行',
desc: '该方法 Function({expanded, column, columnIndex, $columnIndex, row, rowIndex?, $rowIndex?}) 在展开或关闭触发之前调用,可以通过返回值来决定是否允许继续执行',
version: '',
type: 'Function',
enum: '',
@@ -1272,7 +1272,7 @@ const apis = [
},
{
name: 'visibleMethod',
desc: '该函数 Function({row, rowIndex, $rowIndex, column, columnIndex, $columnIndex}) 的返回值用来决定是否允许显示展开按钮',
desc: '该函数 Function({column, columnIndex, $columnIndex, row, rowIndex?, $rowIndex?}) 的返回值用来决定是否允许显示展开按钮',
version: '',
type: 'Function',
enum: '',
@@ -1408,7 +1408,7 @@ const apis = [
},
{
name: 'toggleMethod',
desc: '该方法 Function({expanded,row}) 在展开或关闭触发之前调用,可以通过返回值来决定是否允许继续执行',
desc: '该方法 Function({expanded, row, column, columnIndex, $columnIndex}) 在展开或关闭触发之前调用,可以通过返回值来决定是否允许继续执行',
version: '',
type: 'Function',
enum: '',
@@ -2264,7 +2264,7 @@ const apis = [
version: '',
type: '',
enum: '',
defVal: '{expanded,row},event',
defVal: '{expanded,row,rowIndex,$rowIndex,column,columnIndex,$columnIndex},event',
list: []
},
{
@@ -2273,7 +2273,7 @@ const apis = [
version: '',
type: '',
enum: '',
defVal: '{expanded,row},event',
defVal: '{expanded, row, column, columnIndex, $columnIndex},event',
list: []
},
{

View File

@@ -232,7 +232,7 @@ const Methods = {
if (isLazy && row[treeOpts.hasChild] && XEUtils.isUndefined(row[treeOpts.children])) {
row[treeOpts.children] = null
}
const rest = { row, rowid, index }
const rest = { row, rowid, index: treeConfig ? -1 : index }
if (source) {
fullDataRowIdData[rowid] = rest
fullDataRowMap.set(row, rest)
@@ -2356,13 +2356,15 @@ const Methods = {
* 展开行事件
*/
triggerRowExpandEvent (evnt, params) {
const { expandOpts, expandLazyLoadeds } = this
const { expandOpts, expandLazyLoadeds, expandColumn: column } = this
const { row } = params
const { lazy } = expandOpts
if (!lazy || expandLazyLoadeds.indexOf(row) === -1) {
const expanded = !this.isExpandByRow(row)
const columnIndex = this.getColumnIndex(column)
const $columnIndex = this.$getColumnIndex(column)
this.setRowExpansion(row, expanded)
this.$emit('toggle-row-expand', { expanded, row, $table: this }, evnt)
this.$emit('toggle-row-expand', { expanded, column, columnIndex, $columnIndex, row, rowIndex: this.getRowIndex(row), $rowIndex: this.$getRowIndex(row), $table: this }, evnt)
}
},
/**
@@ -2400,7 +2402,7 @@ const Methods = {
const rest = this.fullAllDataRowMap.get(row)
return new Promise(resolve => {
this.expandLazyLoadeds.push(row)
this.expandOpts.loadMethod({ $table: this, row }).catch(e => e).then(() => {
this.expandOpts.loadMethod({ $table: this, row, rowIndex: this.getRowIndex(row), $rowIndex: this.$getRowIndex(row) }).catch(e => e).then(() => {
rest.expandLoaded = true
XEUtils.remove(this.expandLazyLoadeds, item => item === row)
this.rowExpandeds.push(row)
@@ -2416,10 +2418,12 @@ const Methods = {
* @param {Boolean} expanded 是否展开
*/
setRowExpansion (rows, expanded) {
const { fullAllDataRowMap, expandLazyLoadeds, expandOpts } = this
const { fullAllDataRowMap, expandLazyLoadeds, expandOpts, expandColumn: column } = this
let { rowExpandeds } = this
const { lazy, accordion, toggleMethod } = expandOpts
const lazyRests = []
const columnIndex = this.getColumnIndex(column)
const $columnIndex = this.$getColumnIndex(column)
if (rows) {
if (!XEUtils.isArray(rows)) {
rows = [rows]
@@ -2431,7 +2435,7 @@ const Methods = {
}
if (expanded) {
rows.forEach(row => {
if ((!toggleMethod || toggleMethod({ expanded, row })) && rowExpandeds.indexOf(row) === -1) {
if ((!toggleMethod || toggleMethod({ expanded, column, columnIndex, $columnIndex, row, rowIndex: this.getRowIndex(row), $rowIndex: this.$getRowIndex(row) })) && rowExpandeds.indexOf(row) === -1) {
const rest = fullAllDataRowMap.get(row)
const isLoad = lazy && !rest.expandLoaded && expandLazyLoadeds.indexOf(row) === -1
if (isLoad) {
@@ -2442,7 +2446,7 @@ const Methods = {
}
})
} else {
XEUtils.remove(rowExpandeds, row => (!toggleMethod || toggleMethod({ expanded, row })) && rows.indexOf(row) > -1)
XEUtils.remove(rowExpandeds, row => (!toggleMethod || toggleMethod({ expanded, column, columnIndex, $columnIndex, row, rowIndex: this.getRowIndex(row), $rowIndex: this.$getRowIndex(row) })) && rows.indexOf(row) > -1)
}
}
this.rowExpandeds = rowExpandeds
@@ -2516,13 +2520,15 @@ const Methods = {
* 展开树节点事件
*/
triggerTreeExpandEvent (evnt, params) {
const { treeOpts, treeLazyLoadeds } = this
const { treeOpts, treeLazyLoadeds, expandColumn: column } = this
const { row } = params
const { lazy } = treeOpts
if (!lazy || treeLazyLoadeds.indexOf(row) === -1) {
const expanded = !this.isTreeExpandByRow(row)
const columnIndex = this.getColumnIndex(column)
const $columnIndex = this.$getColumnIndex(column)
this.setTreeExpansion(row, expanded)
this.$emit('toggle-tree-expand', { expanded, row, $table: this }, evnt)
this.$emit('toggle-tree-expand', { expanded, column, columnIndex, $columnIndex, row, $table: this }, evnt)
}
},
/**
@@ -2604,9 +2610,11 @@ const Methods = {
* @param {Boolean} expanded 是否展开
*/
setTreeExpansion (rows, expanded) {
const { fullAllDataRowMap, tableFullData, treeExpandeds, treeOpts, treeLazyLoadeds } = this
const { fullAllDataRowMap, tableFullData, treeExpandeds, treeOpts, treeLazyLoadeds, expandColumn: column } = this
const { lazy, hasChild, children, accordion, toggleMethod } = treeOpts
const result = []
const columnIndex = this.getColumnIndex(column)
const $columnIndex = this.$getColumnIndex(column)
if (rows) {
if (!XEUtils.isArray(rows)) {
rows = [rows]
@@ -2620,7 +2628,7 @@ const Methods = {
}
if (expanded) {
rows.forEach(row => {
if ((!toggleMethod || toggleMethod({ expanded, row })) && treeExpandeds.indexOf(row) === -1) {
if ((!toggleMethod || toggleMethod({ expanded, column, columnIndex, $columnIndex, row })) && treeExpandeds.indexOf(row) === -1) {
const rest = fullAllDataRowMap.get(row)
const isLoad = lazy && row[hasChild] && !rest.treeLoaded && treeLazyLoadeds.indexOf(row) === -1
// 是否使用懒加载
@@ -2634,7 +2642,7 @@ const Methods = {
}
})
} else {
XEUtils.remove(treeExpandeds, row => (!toggleMethod || toggleMethod({ expanded, row })) && rows.indexOf(row) > -1)
XEUtils.remove(treeExpandeds, row => (!toggleMethod || toggleMethod({ expanded, column, columnIndex, $columnIndex, row })) && rows.indexOf(row) > -1)
}
return Promise.all(result).then(this.recalculate)
}