Hi there
I’m struggling with a FAQ page I’m building. First I need a solution that shows questions and answers. But in the first view it hides the answers and when you click on the question it shows it. I found the code online, and it worked if I uses it without my following code that I have added.
I want to filter the questions depending on the choices of the user. so if they are a starter or an expert it shows questions that are for them. to do this, I worked with a database and 3 column tags. User type, Category or the Type of the question. (If possible, it would also be nice if there was a search bar, no idea how I have to code that)
When I added the second code, the first code only works when you do not filter it, as soon as you start sorting the questions (it seems that the filtering code works). Then the first code does not work anymore.
Check the website below
the current page:
https://dennis1864.editorx.io/io2web/blank-9-1-1-1
import wixAnimations from 'wix-animations';
import wixData from 'wix-data';
const ACTIVE_COLOR = '#207272';
const DEFAULT_COLOR = '#001E1E';
//$w.onReady(function () {
//});
function initFAQrepeater() {
$w('#FAQrepeater' ).forEachItem(( $item, itemData, index) => {
$item('#FAQbutton').onClick(() => {
openRelevantContent(index);
});
});
}
function openRelevantContent(targetIndex) {
$w('#FAQrepeater').forEachItem(($item, itemData, index) => {
const contentBox = $item('#FAQcontent');
const button = $item('#FAQbutton');
const arrow = $item('#FAQarrow');
if (targetIndex === index) {
contentBox.collapsed ? expandFAQcontent(contentBox, button, arrow) : collapseFAQcontent(contentBox, button, arrow);
} else {
collapseFAQcontent(contentBox, button, arrow);
}
});
}
function expandFAQcontent(contentBox, button, arrow) {
contentBox.expand();
button.style.color= ACTIVE_COLOR;
wixAnimations.timeline()
.add(arrow, { rotate: 270, duration: 300 })
.play();
}
function collapseFAQcontent(contentBox, button, arrow) {
contentBox.collapse();
button.style.color= DEFAULT_COLOR;
wixAnimations.timeline()
.add(arrow, { rotate: 90, duration: 300 })
.play();
}
/**
* Adds an event handler that runs when the dataset is ready.
*/
export function FAQdataset_ready() {
initFAQrepeater()
// This function was added from the Properties & Events panel. To learn more, visit http://wix.to/UcBnC-4
// Add your code for this event here:
}
/// Second CODE for sorting
$w.onReady(function () {
$w("#TagCategory, #TagUser, #TagType").onChange(function () {
search();
initFAQrepeater();
});
function search() {
let filter = wixData.filter();
let catIdx = $w("#TagCategory").selectedIndices;
let userIdx = $w("#TagUser").selectedIndices;
let typeIdx = $w("#TagType").selectedIndices;
let catVal = $w("#TagCategory").value;
let userVal = $w("#TagUser").value;
let typeVal = $w("#TagType").value;
if (catIdx.length > 0 || userIdx.length > 0 || typeIdx.length > 0) {
filter = filter.hasAll("category", catVal)
.and(filter = filter.hasAll("userType", userVal))
.and(filter = filter.hasAll("type", typeVal))
$w("#FAQdataset").setFilter(filter)
.then(count)
} else {
$w("#FAQdataset").setFilter(filter)
.then(count)
}
$w("#Clearbutton").onClick(function () {
$w("#TagCategory, #TagUser, #TagType").value = undefined;
$w("#FAQdataset").setFilter(wixData.filter()).then(count);
});
}
//COUNT ITEM
function count() {
let count = $w("#FAQdataset").getTotalCount();
if (count > 0) {
$w("#countText").text = `${count} items found`;
} else { $w("#countText").text = `No item found`; }
return count;
}
$w("#FAQdataset").onReady(function () {
count();
});
});