I want to select only two ‘Country’, ‘Articles Title’ of the four available columns from the Wix database collection ‘Articles’ and connect them to the ‘table1’ on wix page. I do not want to use Dataset connection instead want to use wixdata.query function. Not sure which option to use to select few columns and code to connect them to respective table columns. Please help.
Greetings,
This code probably needs to be tweaked a little for field names. Make sure you’re using the field key that doesn’t have spaces and is case-sensitive. Also, the field name of each column needs to be set to the field key. You could also set the columns up in code if you wanted.
import wixData from 'wix-data';
$w.onReady(function () {
PopulateTable();
});
export function PopulateTable(){
wixData.query("Articles")
.find()
.then( (results) => {
if(results.items.length > 0) {
let TableData = []; TableRowData = {}, item;
// Add one row at a time to the TableData array by looping through the query results.
for (var i = 0; i < results.items.length; i++) {
item = results.items[i];
TableRowData = {"country": item.country,"articlesTitle": item.articlesTitle};
TableData.push(TableRowData);
$w('#table1').rows = TableData;
}
} else {
// no data found
}
} )
.catch( (err) => {
let errorMsg = err;
} );
}
Thanks a lot Anthony it worked. But I think I am curious whether wix has option to select only few columns from the tables like in any database table. For example I can write SQL as below other than Wix Query.
Select article_title, country from Articles.
This SQL gives me only two columns that I need instead of total table. Is there any option like that in Wix Query?
@greenmango318 Wix’s databases are noSQL-based, and on top of that tables are basically just arrays with a little flair, so anthony’s answer is what you’re looking for.
For people who don’t use/like arrays, repeaters are the easier option.
@skmedia Thanks David.