@@ -28,6 +28,37 @@ const toggleRowExpansion = function(state, row, expanded) {
2828 return changed ;
2929} ;
3030
31+ const toggleRowSelection = function ( state , row , selected ) {
32+ let changed = false ;
33+ const selectRows = state . selectRows ;
34+
35+ if ( typeof selected !== "undefined" ) {
36+ const index = selectRows . indexOf ( row ) ;
37+ if ( selected ) {
38+ if ( index === - 1 ) {
39+ selectRows . push ( row ) ;
40+ changed = true ;
41+ }
42+ } else {
43+ if ( index !== - 1 ) {
44+ selectRows . splice ( index , 1 ) ;
45+ changed = true ;
46+ }
47+ }
48+ } else {
49+ const index = selectRows . indexOf ( row ) ;
50+ if ( index === - 1 ) {
51+ selectRows . push ( row ) ;
52+ changed = true ;
53+ } else {
54+ selectRows . splice ( index , 1 ) ;
55+ changed = true ;
56+ }
57+ }
58+
59+ return changed ;
60+ } ;
61+
3162const getKeysMap = function ( array , rowKey ) {
3263 const arrayMap = { } ;
3364 ( array || [ ] ) . forEach ( ( row , index ) => {
@@ -36,7 +67,7 @@ const getKeysMap = function(array, rowKey) {
3667 return arrayMap ;
3768} ;
3869
39- const getRowIdentity = ( row , rowKey ) => {
70+ const getRowIdentity = function ( row , rowKey ) {
4071 if ( ! row ) throw new Error ( "row is required when get row identity" ) ;
4172 if ( typeof rowKey === "string" ) {
4273 if ( rowKey . indexOf ( "." ) < 0 ) {
@@ -53,18 +84,43 @@ const getRowIdentity = (row, rowKey) => {
5384 }
5485} ;
5586
87+ export const geneFlattenArrayFromTreeByDepth = (
88+ tree ,
89+ maxDepth ,
90+ currDepth = 0 ,
91+ res = [ ]
92+ ) => {
93+ let i = 0 ;
94+ let item = null ;
95+ const l = tree . length ;
96+
97+ if ( currDepth >= maxDepth ) return ( currDepth = 0 ) || [ ]
98+
99+ for ( ; i < l ; i ++ ) {
100+ item = tree [ i ]
101+ res [ res . length ] = item
102+
103+ if ( item && item . children && item . children . length ) {
104+ geneFlattenArrayFromTreeByDepth ( item . children , maxDepth , currDepth + 1 , res )
105+ }
106+ }
107+
108+ return res ;
109+ } ;
56110
57111export default class TableState {
58- constructor ( initialState ) {
112+ constructor ( table , initialState ) {
113+ this . table = table ;
59114 this . states = {
60115 data : null ,
61116 columns : "" ,
62117 rowKey : "" ,
63- selectedRowsKeys : [ ] ,
118+ selectRows : [ ] ,
64119 expandRows : [ ] ,
65- expandRowKeys : [ ] ,
120+ expandDepth : 0 ,
66121 defaultExpandAll : false ,
67122 } ;
123+ // console.log(this.states)
68124 this . setState ( initialState ) ;
69125 }
70126
@@ -83,11 +139,28 @@ export default class TableState {
83139 /* 切换当前行展开收起状态 */
84140 toggleRowExpansion ( row , expanded ) {
85141 const changed = toggleRowExpansion ( this . states , row , expanded ) ;
142+
86143 if ( changed ) {
87- // eslint-disable-next-line
88- console . log ( 'toggleRowExpansion has changed' )
89- // this.table.$emit("expand-change", row, this.states.expandRows);
90- // this.scheduleLayout();
144+ this . table . $emit ( "expand-change" , row , this . states . expandRows ) ;
145+ }
146+ }
147+
148+ isRowSelected ( row ) {
149+ const { selectRows = [ ] , rowKey } = this . states ;
150+
151+ if ( rowKey ) {
152+ const expandMap = getKeysMap ( selectRows , rowKey ) ;
153+ return ! ! expandMap [ getRowIdentity ( row , rowKey ) ] ;
154+ }
155+
156+ return selectRows . indexOf ( row ) !== - 1 ;
157+ }
158+
159+ toggleRowSelection ( row , selected ) {
160+ const changed = toggleRowSelection ( this . states , row , selected ) ;
161+
162+ if ( changed ) {
163+ this . table . $emit ( "select-change" , row , this . states . selectRows ) ;
91164 }
92165 }
93166
@@ -106,21 +179,29 @@ export default class TableState {
106179 }
107180 }
108181
109- _setData ( state , data ) {
182+ _setData ( states , data ) {
110183 // eslint-disable-next-line
111- const dataInstanceChanged = state . _data !== data ;
112- state . _data = data ;
113- state . data = data ;
114-
115- const rowKey = state . rowKey ;
116- const defaultExpandAll = state . defaultExpandAll ;
117- if ( defaultExpandAll ) {
118- this . states . expandRows = ( state . data || [ ] ) . slice ( 0 ) ;
184+ const dataInstanceChanged = states . _data !== data ;
185+ states . _data = data ;
186+ states . data = data ;
187+
188+ const rowKey = states . rowKey ;
189+ const expandDepth = states . expandDepth ;
190+ const defaultExpandAll = states . defaultExpandAll ;
191+
192+ // keep expand rows
193+ if ( defaultExpandAll ) { /* Does not support this attribute */
194+ this . states . expandRows = ( states . data || [ ] ) . slice ( 0 ) ;
195+ } else if ( expandDepth ) {
196+ this . states . expandRows = geneFlattenArrayFromTreeByDepth (
197+ ( states . data || [ ] ) ,
198+ expandDepth ,
199+ ) . slice ( 0 )
119200 } else if ( rowKey ) {
120201 // update expandRows to new rows according to rowKey
121202 const ids = getKeysMap ( this . states . expandRows , rowKey ) ;
122203 let expandRows = [ ] ;
123- for ( const row of state . data ) {
204+ for ( const row of states . data ) {
124205 const rowId = getRowIdentity ( row , rowKey ) ;
125206
126207 if ( ids [ rowId ] ) {
@@ -133,7 +214,23 @@ export default class TableState {
133214 // clear the old rows
134215 this . states . expandRows = [ ] ;
135216 }
217+ /*
218+ // const selectRows = states.selectRows
219+ if (this.selectRows && this.selectRows.length) {
220+ const ids = getKeysMap(this.states.selectRows, rowKey);
221+ let selectRows = []
222+ for (const row of states.data) {
223+ const rowId = getRowIdentity(row, rowKey);
136224
225+ if (ids[rowId]) {
226+ expandRows.push(row);
227+ }
228+ }
229+
230+ // The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included).
231+ this.states.selectRows = selectRows
232+ }
233+ */
137234 // Vue.nextTick(() => this.table.updateScrollY());
138235 }
139236}
0 commit comments