Hi Wix Coders,
I really hope one of you can help me as I believe the solution is very simple but as I am a beginner to code I do not understand it.
I have a database that you can filter based on dropdowns, but, I also want to add an ascending/descending option for age / price
I am struggling to combine my code for the dropdowns with my code for the ascending / descending options.
Please can someone tell me how I can combine both code. I currently have the below which works fine for filtering the database based on my dropdowns, however, I want users to be able to sort my age and price columns.
import wixData from ‘wix-data’;
$w.onReady(() => {
$w(’ #dataset1 ').onReady(() => {
count();
$w(’ #dropdown1 , #Positiondropdown , #Teamdropdown , #PBdropdown , #Countrydropdown ‘).onChange(() => {
search();
})
$w(’ #clear ‘).onClick(() => {
$w(’#dropdown1, #Positiondropdown, #Teamdropdown, #PBdropdown, #Countrydropdown’).value = “”;
$w(‘#dataset1’).setFilter(wixData.filter())
.then(count);
});
function search() {
let filter = wixData.filter();
let player = $w(“#dropdown1”).value;
let position = $w(“#Positiondropdown”).value;
let team = $w(“#Teamdropdown”).value;
let pbLeague = $w(“#PBdropdown”).value;
let country = $w(“#Countrydropdown”).value;
if (player && player !== ‘all’) {
filter = filter.eq(“player”, player);
}
if (position && position !== ‘all’) {
filter = filter.eq(“position”, position);
}
if (team && team !== ‘all’) {
filter = filter.eq(“team”, team);
}
if (pbLeague && pbLeague !== ‘all’) {
filter = filter.eq(“pbLeague”, pbLeague);
}
if (country && country !== ‘all’) {
filter = filter.eq(“country”, country);
}
$w(‘#dataset1’).setFilter(filter)
.then(count);
} function count() {
let total = $w(‘#dataset1’).getTotalCount();
if (total > 0) {
$w(’ #textCount ‘).text = ${total} result has found.
;
} else {
$w(’#textCount’).text = “No result found!”;
}
}
});
});
Below is the code that I can use to srot my age / price columns which works but I do not know how to integrate it into the above code to make both sets of code work simultaneously:
$w.onReady( function() {
$w(" #button10 “).onClick( (event, $w) => {
$w(” #dataset1 ").setSort( wixData.sort()
.descending(“age”) );
$w(" #button9 “).onClick( (event, $w) => {
$w(”#dataset1").setSort( wixData.sort()
.ascending(“age”) );
$w(" #button11 “).onClick( (event, $w) => {
$w(”#dataset1").setSort( wixData.sort()
.descending(“ppriceOn200819”) );
$w(" #button12 “).onClick( (event, $w) => {
$w(”#dataset1").setSort( wixData.sort()
.ascending(“priceOn200819”) );
Thanks!