[SOLVED] Populate Table row using forLoop?

Hello, is there a sample code on how to populate table row using for loop statement?


$w.onReady(function () {

    $w("#table1").columns = [
        {
 		"id": "col1",
 		"label": "Full Name",
 		"dataPath": "name",
 		"visible": true,
 		"width": 150,
 		"type": "string",
        },  
    ];

});
////////////////////////////////Here is my code i want to display on the table:

.then( (items) => {
 let count = items.length;

     for (var e = 0; e<count; e++) {
         let name = items[e].name;
         console.log(e);
         //insert table row value then loop again
    }

I want to display my console log results into a table.
Can anyone help me? Thank you.

Hi Jefferson,

You don’t need a loop at all . Corvid provides built-in functionality to update your table from the query results. Use this code:

wixData.query( “myCollection” )
.find()
.then((res) => {
$w( “#table1” ).rows = res.items;
});

Just make sure the dataPath property in your $w ( “#table1” ). columns definition matches the field id in the collection. In your case “dataPath” : “name” so the field id in the collection should be called “name”.

Good Luck,
Asaf

Hello sir @asafr , Thank you for the response, for this matter I am not storing the data resulted from my forLoop, although i have managed to display in on textbox but after the desired result of the loop will I save the data to my collection.
Here is my forum post related to this matter.
https://www.wix.com/corvid/forum/community-discussion/how-can-we-display-console-log-result-to-an-element

Finally I got it!!! T POPULATE TABLE ROW USING FOR LOOP STATEMENT :smiley:

$w.onReady(function () {

    $w("#table1").columns = [
        {
           "id": "col1",
           "label": "Names",
           "dataPath": "names",
           "visible": true,
           "width": 400,
           "type": "string",
        }
    ];

});

some event here:

 .then( (items) => {
 let count = items.length;
 let objROW= [];

 for (var e = 0; e<count; e++) {
     let name = items[e].name;
     //THE MOST IMPORTANT, convert your result to an Object.
     objROW[e] = { names: name };
}
$w("#table1").rows = objROW;