I'm trying to figure out how to load data (lazy load). First, loads the data lets say 5 records and then when the user clicks the button second time then load another 5 records so it will be 10 records and so on and so forth till the it ends.
Peter Seliger was kind enough to answer the question and I need other half so I decided to create a new question instead of confusing him more with my original question.
const data = [
{
"color": "purple",
"type": "minivan",
"registration": new Date('2017-01-03'),
"capacity": 7
},
{
"color": "red",
"type": "station wagon",
"registration": new Date('2018-03-03'),
"capacity": 5
},
{
"color": "red2",
"type": "station wagon",
"registration": new Date('2018-03-03'),
"capacity": 5
},
{
"color": "red3",
"type": "station wagon",
"registration": new Date('2018-03-03'),
"capacity": 5
},
{
"color": "red4",
"type": "station wagon",
"registration": new Date('2018-03-03'),
"capacity": 5
},
{
"color": "red5",
"type": "station wagon",
"registration": new Date('2018-03-03'),
"capacity": 5
},
{
"color": "red6",
"type": "station wagon",
"registration": new Date('2018-03-03'),
"capacity": 5
},
{
"color": "red7",
"type": "station wagon",
"registration": new Date('2018-03-03'),
"capacity": 5
},
{
"color": "red8",
"type": "station wagon",
"registration": new Date('2018-03-03'),
"capacity": 5
},
{
"color": "red9",
"type": "station wagon",
"registration": new Date('2018-03-03'),
"capacity": 5
},
{
"color": "red10",
"type": "station wagon",
"registration": new Date('2018-03-03'),
"capacity": 5
},
{
"color": "red11",
"type": "station wagon",
"registration": new Date('2018-03-03'),
"capacity": 5
},
{
"color": "red12",
"type": "station wagon",
"registration": new Date('2018-03-03'),
"capacity": 5
},
{
"color": "red13",
"type": "station wagon",
"registration": new Date('2018-03-03'),
"capacity": 5
},
{
"color": "red14",
"type": "station wagon",
"registration": new Date('2018-03-03'),
"capacity": 5
}
];
data2 = {};
function* createChunkGenerator(
itemList = [], chunkSize = 1, chunkCount = 0
) {
chunkSize = Math.max(chunkSize, 1);
while (itemList.length >= 1) {
++chunkCount;
yield { chunkCount, itemList: itemList.splice(0, chunkSize), };
}
}
let chunkGenerator = createChunkGenerator( data, 5 );
console.log('...automatically tiggered `next` based iteration...');
data2 = chunkGenerator.next();
console.log( data2 );
Here is the codepen I have created: https://codepen.io/threeonethree/pen/YzYgMYL
'click'handler with the bound generator and element nodes. Every time one clicks the button, a new chunk gets presented. It is working code. Thus one just needs to adapt the code which is already there, and instead of rendering the pure chunk data as text, one creates the correct element nodes and fills them from/with the data of each of the chunk'sitemListitems and appends the created nodes to the lazy-load root-node in the DOM.