@ -1,4 +1,4 @@
import { VerticalLayout , Layout , Widget , shortcut , extend , isFunction , isNumber , PrefixIntervalTree , ResizeDetector } from "@/core" ;
import { VerticalLayout , Layout , Widget , shortcut , extend , isFunction , isNumber , sum , PrefixIntervalTree , ResizeDetector } from "@/core" ;
import { VirtualGroup } from "../combination" ;
/ * *
@ -17,7 +17,7 @@ export class VirtualGroupList extends Widget {
overscanHeight : 100 ,
blockSize : 10 ,
scrollTop : 0 ,
rowHeight : "auto" ,
rowHeight : "auto" , // 'auto' 或 数值 或function
items : [ ] ,
el : { } ,
itemFormatter : ( item , index ) => item ,
@ -97,7 +97,7 @@ export class VirtualGroupList extends Widget {
}
_isAutoHeight ( ) {
return ! isNumber ( this . options . rowHeight ) ;
return this . options . rowHeight === 'auto' ;
}
_renderMoreIf ( ) {
@ -163,7 +163,7 @@ export class VirtualGroupList extends Widget {
itemsArr . push ( items [ j ] ) ;
}
}
this . container . element . height ( rowHeight * items . length - topHeight ) ;
this . container . element . height ( this . summaryHeight - topHeight ) ;
this . container . populate (
itemsArr . map ( ( item , i ) => itemFormatter ( item , ( start < 0 ? 0 : start ) * blockSize + i ) )
) ;
@ -180,6 +180,16 @@ export class VirtualGroupList extends Widget {
Math . ceil ( this . options . items . length / blockSize ) ,
this . _isAutoHeight ( ) ? 0 : rowHeight * blockSize
) ;
if ( isFunction ( rowHeight ) ) {
for ( let i = 0 ; i < this . options . items . length / blockSize ; i ++ ) {
const index = i * blockSize ;
let summaryHeight = 0 ;
for ( let j = index ; j < index + blockSize && j < this . options . items . length ; j ++ ) {
summaryHeight += rowHeight ( j , this . options . items [ j ] ) ;
}
this . tree . set ( i , summaryHeight ) ;
}
}
this . _calculateBlocksToRender ( ) ;
try {
@ -188,7 +198,13 @@ export class VirtualGroupList extends Widget {
}
_restore ( ) {
const o = this . options ;
this . renderedIndex = - 1 ;
if ( isFunction ( o . rowHeight ) ) {
this . summaryHeight = sum ( o . items , o . rowHeight ) ;
} else {
this . summaryHeight = this . _isAutoHeight ( ) ? 0 : o . rowHeight * o . items . length ;
}
// 依赖于cache的占位元素也要初始化
this . topBlank . setHeight ( 0 ) ;
this . bottomBlank . setHeight ( 0 ) ;