15
15
</ style >
16
16
</ head >
17
17
18
- < body >
19
- <!-- toolbar with ui5-bar -->
20
- < ui5-bar design ="Header " accessible-name-ref ="title " style ="position: sticky; top: 0; z-index: 2; height: 50px; ">
21
- < ui5-title tabindex ="0 " level ="H3 " id ="title " slot ="startContent "> My Selectable Products (1000)</ ui5-title >
22
- < ui5-slider id ="slider " min ="0 " max ="100 " step ="1 " value ="100 "
23
- label-interval ="0 "> </ ui5-slider >
24
- </ ui5-bar >
18
+ < body style ="background-color: var(--sapBackgroundColor) " class ="ui5-content-density-compact ">
19
+ < div class ="section ">
20
+ < ui5-table id ="table " loading-delay ="100 " style ="height: 150px; ">
21
+ < ui5-table-virtualizer id ="virtualizer " slot ="features " row-count ="1000 " row-height ="32 "> </ ui5-table-virtualizer >
22
+ < ui5-table-selection slot ="features "> </ ui5-table-selection >
23
+ < ui5-table-header-row slot ="headerRow " sticky >
24
+ < ui5-table-header-cell width ="300px "> Product Name</ ui5-table-header-cell >
25
+ < ui5-table-header-cell > Dimensions</ ui5-table-header-cell >
26
+ < ui5-table-header-cell > Weight</ ui5-table-header-cell >
27
+ < ui5-table-header-cell horizontal-align ="Right "> Price</ ui5-table-header-cell >
28
+ </ ui5-table-header-row >
29
+ </ ui5-table >
30
+ < template id ="rowTemplate ">
31
+ < ui5-table-row position ="-1 " row-key ="-1 ">
32
+ < ui5-table-cell data ="name "> </ ui5-table-cell >
33
+ < ui5-table-cell data ="height "> </ ui5-table-cell >
34
+ < ui5-table-cell data ="weight "> </ ui5-table-cell >
35
+ < ui5-table-cell data ="price "> </ ui5-table-cell >
36
+ </ ui5-table-row >
37
+ </ template >
38
+ </ div >
25
39
26
- < ui5-table id ="table " loading-delay ="0 " accessible-name-ref ="title " no-data-text ="No data found " overflow-mode ="Scroll " style ="height: 375px; ">
27
- < ui5-table-virtualizer id ="virtualizer " slot ="features " row-height ="51 " row-count ="10 "> </ ui5-table-virtualizer >
28
- < ui5-table-selection id ="selection " selected ="2 5 " slot ="features "> </ ui5-table-selection >
29
- < ui5-table-header-row slot ="headerRow " sticky >
30
- < ui5-table-header-cell width ="200px " id ="produtCol "> Product</ ui5-table-header-cell >
31
- < ui5-table-header-cell width ="max-content " id ="supplierCol "> Supplier</ ui5-table-header-cell >
32
- < ui5-table-header-cell width ="max-content " id ="dimensionsCol "> Dimensions</ ui5-table-header-cell >
33
- < ui5-table-header-cell width ="max-content " id ="weightCol "> Weight</ ui5-table-header-cell >
34
- < ui5-table-header-cell width ="100px " id ="priceCol "> Price</ ui5-table-header-cell >
35
- </ ui5-table-he πader-row>
36
- </ ui5-table >
37
-
38
- < ui5-input value ="after table " data-sap-ui-fastnavgroup ="true "> </ ui5-input >
39
40
< script >
40
- const slider = document . getElementById ( "slider" ) ;
41
- const table = document . getElementById ( "table" ) ;
42
- let timer = 0 ;
43
- slider . addEventListener ( "input" , ( e ) => {
44
- table . style . width = `${ e . target . value } %` ;
45
- } ) ;
46
- table . addEventListener ( "row-click" , ( e ) => {
47
- console . log ( `${ Date . now ( ) } : Row with the row-key ${ e . detail . row . row - key } is clicked` ) ;
48
- } ) ;
49
41
50
- const virtualizerData = [ ] ;
51
- for ( let i = 0 ; i <= 1000 ; i ++ ) {
52
- virtualizerData . push ( Math . random ( ) * 200 + 32 ) ;
53
- }
42
+ class ProductStore {
43
+ constructor ( ) {
44
+ this . products = [ ] ;
45
+ }
54
46
55
- const virtualizer = document . getElementById ( "virtualizer" ) ;
56
- virtualizer . addEventListener ( "range-change" , ( e ) => {
47
+ async fetchProducts ( first , last ) {
48
+ const products = [ ] ;
49
+ for ( let i = first ; i < last ; i ++ ) {
50
+ this . products [ i ] ??= await this . fetchProduct ( i ) ;
51
+ products . push ( this . products [ i ] ) ;
52
+ }
53
+ return products ;
54
+ }
57
55
58
- if ( table . rows . length ) {
59
- clearTimeout ( timer ) ;
60
- table . loading = true ;
61
- timer = setTimeout ( ( ) => {
62
- for ( let i = e . detail . first ; i < e . detail . last ; i ++ ) {
63
- const index = i - e . detail . first ;
64
- const content = table . rows [ index ] . cells [ 0 ] ;
65
- table . rows [ index ] . rowKey = i + "" ;
66
- table . rows [ index ] . position = i ;
67
- content . querySelector ( "b" ) . firstChild . nodeValue = `Notebook Basic ${ i } ` ;
68
- content . querySelector ( "a" ) . firstChild . nodeValue = `HT-100${ i } ` ;
69
- }
70
- table . loading = false ;
71
- } , 500 ) ;
72
- return ;
56
+ async fetchProduct ( index ) {
57
+ return new Promise ( resolve => {
58
+ setTimeout ( ( ) => {
59
+ resolve ( {
60
+ key : `P${ index } ` ,
61
+ name : `Product ${ index } ` ,
62
+ height : `${ ( Math . random ( ) * 100 ) . toFixed ( 0 ) } cm` ,
63
+ weight : `${ ( Math . random ( ) * 100 ) . toFixed ( 1 ) } KG` ,
64
+ price : `${ ( Math . random ( ) * 1000 ) . toFixed ( 2 ) } EUR`
65
+ } ) ;
66
+ } , Math . random ( ) * 10 ) ; // Simulate network delay
67
+ } ) ;
73
68
}
69
+ }
74
70
75
- table . rows . forEach ( row => row . remove ( ) ) ;
71
+ const productStore = new ProductStore ( ) ;
72
+ const table = document . getElementById ( "table" ) ;
73
+ const rowTemplate = document . getElementById ( "rowTemplate" ) ;
74
+ const virtualizer = document . getElementById ( "virtualizer" ) ;
76
75
77
- for ( let i = e . detail . first ; i < e . detail . last ; i ++ ) {
78
- const newRow = document . createElement ( "ui5-table-row" ) ;
79
- newRow . setAttribute ( "row-key" , i . toString ( ) ) ;
80
- newRow . position = i ;
81
- newRow . innerHTML = `
82
- <ui5-table-cell><ui5-label><b>Notebook Basic ${ i } </b><br><a href="#">HT-100${ i } </a></ui5-label></ui5-table-cell>
83
- <ui5-table-cell><ui5-label>Technocom</ui5-label></ui5-table-cell>
84
- <ui5-table-cell><ui5-input></ui5-input></ui5-table-cell>
85
- <ui5-table-cell><ui5-label style="color: #2b7c2b"><b>3.7</b> KG</ui5-label></ui5-table-cell>
86
- <ui5-table-cell><ui5-label><b>29</b> EUR</ui5-label></ui5-table-cell>
87
- ` ;
88
- table . appendChild ( newRow ) ;
76
+ virtualizer . addEventListener ( "range-change" , async ( e ) => {
77
+ const { first, last } = e . detail ;
78
+ const products = await productStore . fetchProducts ( first , last ) ;
79
+ for ( let i = first ; i < last ; i ++ ) {
80
+ const rowIndex = i - first ;
81
+ const product = products [ rowIndex ] ;
82
+ const row = table . rows [ rowIndex ] || table . appendChild ( rowTemplate . content . firstElementChild . cloneNode ( true ) ) ;
83
+ row . setAttribute ( "position" , i ) ;
84
+ row . setAttribute ( "row-key" , product . key ) ;
85
+ row . querySelectorAll ( "[data]" ) . forEach ( el => {
86
+ el . textContent = product [ el . getAttribute ( "data" ) ] ;
87
+ } ) ;
88
+ }
89
+ for ( let i = last ; i < table . rows . length ; i ++ ) {
90
+ table . rows [ i ] . remove ( ) ;
89
91
}
90
92
} ) ;
91
93
92
- const selection = document . getElementById ( "selection" ) ;
93
- selection . addEventListener ( "change" , ( e ) => {
94
- console . log ( e . target . selected ) ;
95
- } ) ;
96
94
</ script >
95
+
97
96
</ body >
98
97
99
98
</ html >
0 commit comments