Table that is based off query with some variables

I am trying to have a table that is populated from a query but I would like to have two of the values in the rows to be based off variables. In other words, column one is a name, column two would depend on the item in the array. If field X has a number greater than 0, the table should display Y in col2, same for col3. Here is the code I have so far.

	$w("#table1").columns = [{
			"id": "col1",
			"dataPath": "exhibitorName",
			"label": "Exhibitor Name",
			"type": "string",
		},
		{
			"id": "col2",
			"dataPath": "TBD",
			"label": "Stall Type",
			"type": "number",
		},
		{
			"id": "col3",
			"dataPath": "TBD",
			"label": "# Purchased",
			"type": "number",
		},
		{
			"id": "col4",
			"dataPath": "_createdDate",
			"label": "Date Purchased",
			"type": "date",
		},
		{
			"id": "col5",
			"dataPath": "exhibitorPhone",
			"label": "Phone",
			"type": "number",
		}

	];

	let blockview = wixWindow.lightbox.getContext();
	$w("#text12").text = blockview.title;
	wixData.query("stallSales")
		.eq("trainerCellPhone", blockview.trainerCellPhone)
		.eq("horseShowId", "1001")
		.find()
		.then((results) => {
			console.log(results)
			let stallData = results.items;
			console.log(stallData)
			$w("#table1").rows = results.items;
			console.log("data should be in table")
			$w("#table1").expand();
			console.log("table should be expanded")
		});
});

The columns that are just strictly based on the fields are working great but I cant figure out how to say if X is true, display Y in the table.

Hi Doug,

You could do a for loop after the results have been obtained and apply the logic that you have in mind. In the results.items array you could create another field that you would then map to your table. This is a simplistic example to illustrate the point.

.then((results) => {
console.log(results);
let y = "Stall Type 1", x = 5;
for (var i = 0; i < results.items.length; i++) {
    let item = results.items[i];
    if (x > 0){
        // creates array column (field) called TBD that can display in table 
        item.TBD = y;
    } else {
        item.TBD = "";
    }
}
// Now, assign the results.items array with the newly added column(s)
$w("#table1").rows = results.items;
console.log("data should be in table")
$w("#table1").expand();
console.log("table should be expanded")
});

Thank you for this! I played with it but my problem is I need to figure out if any item in the array has a stalltype1field where there is a value greater than 0, not just the total count of the items in the array. So, the DB has 3 fields (type1, type2, type3) and the fields correlate to how many they purchased of each type (so type1:0, type2:3, type3:4, etc). I am trying to find a way to figure out if any of the items in the array have a type1 field that !== 0. I found the easiest way to use the info is to add a new column to the table which is working I jus can’t seem to figure out how to go through the array and figure out if any of the fields are !==0. Sorry if my original post wasn’t clear or if I’m reading the code wrong.

So, in the for loop you could apply your test. However, be aware of the following: if the actual value of the field is null, that also is not equal to 0. When viewing a numeric field in a collection, it will be blank if it’s null (nothing was written to it).

for (var i = 0; i < results.items.length; i++) {
    let item = results.items[i];
    if (item.type1 !== 0){
        // do something;
    }
    if (item.type2 !== 0){              
        // do something;     
    }
}