I have a dataset of products that I want users to be able to search. I only want those products that I have marked as ‘true’ in a Boolean field called ‘Display’ on the table to have subsequent user filters applied to it. Originally, I added this top-most level filter via the Dataset Settings filter settings, but it doesn’t work. Instead, it retrieves every record in the table, whether that field is true or false.
How can I setup this top-most filter on my code? Please note, it should be the default upon entry to page and whenever a user “resets” the search.
All other filters/code work as expected. It’s just that it always starts the sub-filters by first displaying everything in the collection.
import wixData from 'wix-data';
import { local } from 'wix-storage';
$w.onReady(function () {
//notes not code
$w('#filterDataset').onReady(() => {
beginSearch();
});
$w("#searchButton").onClick(() => {
beginSearch();
$w('#noResults').collapse();
});
$w("#resetButton").onClick(() => {
$w('#filterDataset').onReady(() => {
$w("#filterDataset").setFilter(wixData.filter());
$w("#phaseSearch").selectedIndices = [0, 1];
$w("#flangeSearch").selectedIndices = [0, 1, 2];
$w("#wordSearch").value = "";
$w("#wordSearch").placeholder = "Search";
$w("#prodGroup, #rpmSearch, #voltageSearch, #frameSearch, #hpSearchBox, #kwSearch, #materialSearch").selectedIndex = null;
$w('#searchingText').text = "Showing all items.";
});
$w('#noResults').collapse();
});
$w("#repeater2").onItemReady(($item, itemData, index) => {
checkUnix();
let visitor = local.getItem("visitor");
let id = itemData._id;
wixData.query("CompareMotorList")
.eq("motorReference", itemData._id)
.eq("title", visitor)
.find()
.then((results) => {
if (results.items.length > 0) {
$item("#checkbox1").checked = true;
} else {
$item("#checkbox1").checked = false;
}
})
.catch((err) => {
let errorMsg = err;
});
$item("#checkbox1").onChange((event) => {
let isTrue = $item("#checkbox1").checked;
$item("#errorMessage").hide();
if (isTrue) {
let toInsert = {
"motorReference": id,
"title": id,
"_owner": visitor,
};
wixData.insert("analyticsCompare", toInsert)
.then((results) => {
let toInsert2 = {
"motorReference": id,
"title": visitor
};
wixData.query("CompareMotorList")
.eq("title", visitor)
.find()
.then((results) => {
if (results.items.length <= 2) {
$item("#checkbox1").checked = true;
wixData.insert("CompareMotorList", toInsert2)
.then((results) => {
//
})
.catch((err) => {
let errorMsg = err;
});
} else {
$item("#checkbox1").checked = false;
$item("#errorMessage").show();
}
})
.catch((err) => {
let errorMsg = err;
});
})
.catch((err) => {
let errorMsg = err;
});
} else {
wixData.query("CompareMotorList")
.eq("motorReference", itemData._id)
.eq("title", visitor)
.find()
.then((results) => {
if (results.items.length > 0) {
let resItem = results.items[0]._id;
console.log(resItem)
wixData.remove("CompareMotorList", resItem)
.then((res) => {
let item = results;
})
.catch((err) => {
let errorMsg = err;
console.log("Cannot delete")
});
} else {
console.log("Unable to remove item")
}
})
.catch((err) => {
let errorMsg = err;
});
}
});
});
});
async function beginSearch() {
$w('#searchingText').text = "Searching...";
let word = $w('#wordSearch').value;
let group = $w('#prodGroup').value;
let hp = $w('#hpSearchBox').value;
let kw = $w('#kwSearch').value;
let phase = Number($w('#phaseSearch').value);
let sync = Number($w('#rpmSearch').value);
let voltage = $w('#voltageSearch').value;
let frame = $w('#frameSearch').value;
let material = $w('#materialSearch').value;
let flange = $w('#flangeSearch').value;
let filter = wixData.filter();
if (word) {
filter = filter.contains("parent_item_id", word);
}
if (group) {
filter = filter.contains("filter_productLines", group);
}
if (phase) {
filter = filter.hasSome("phase", phase);
}
if (sync) {
filter = filter.eq("sync_speed", sync);
}
if (voltage) {
filter = filter.hasSome("voltage_tags", voltage);
}
if (hp) {
filter = filter.eq("hp1", hp);
}
if (kw) {
filter = filter.eq("kw1", kw);
}
if (frame) {
filter = filter.hasSome("frame_tags", frame);
}
if (material) {
filter = filter.contains("frame_material", material);
}
if (flange) {
filter = filter.hasSome("flange", flange);
}
console.log(filter);
await $w('#filterDataset').setFilter(filter);
let count = $w('#filterDataset').getTotalCount();
$w('#searchingText').text = `Results found: ${count}`;
if (count < 1) {
$w('#noResults').expand();
}
}
function checkUnix() {
let visitor = local.getItem("visitor");
var timeStamp = Math.floor(Date.now() / 1000);
if (visitor) {
console.log("Visitor " + visitor + " is online right now.");
} else {
local.setItem("visitor", timeStamp.toString());
}
}