Hi there!
I need help in this code. How do I add more columns to search?
This is my code below :
import {getAllRecords} from ‘backend/podPlatform’;
// Backend module named dataService.jsw under backend folder
let unfilteredRecords = ; // This will hold all records
$w.onReady( function () {
$w(“#repeater1”).data = ; // Clear repeater from data
getAllRecords().then((allObjects) => {
unfilteredRecords = allObjects;
// Store all records in array
$w(“#repeater1”).data = unfilteredRecords;
// Set all records to repeater
})
});
// Search button onClick event
export async function searchButton_click(event) {
let searchWord = $w(“#searchInput”).value;
$w(“#repeater1”).data = unfilteredRecords.filter(obj =>
obj.platformName.toLowerCase().includes(searchWord.toLowerCase())); // name is fieldKey is Data Collection
}
// I believe it should be added in this part
$w(" #repeater1 ").data = unfilteredRecords.filter(obj => obj.platformName.toLowerCase().includes(searchWord.toLowerCase())); // name is fieldKey is Data Collection }
How do I do that?
Here you go! The first code will search for two fieldKeys as you can see and by using the && it means AND so both of them need to match.
$w("#repeater1").data = unfilteredRecords.filter(obj => obj.platformName.toLowerCase().includes(searchWord.toLowerCase()) && obj.fieldKey.toLowerCase().includes(searchWord.toLowerCase()));
In the below I use || which is the same as OR so any of them needs to match.
$w("#repeater1").data = unfilteredRecords.filter(obj => obj.platformName.toLowerCase().includes(searchWord.toLowerCase()) || obj.fieldKey.toLowerCase().includes(searchWord.toLowerCase()));
You can chain as many conditions as you would like to using this way.
@andreas-kviby When I use || and &&, the search will not work anymore even if the fieldkeys are correct. It only works when I have one condition.
@andreas-kviby Do I need to have the data available in the window or I can search through behind? For example, in the repeater, I don’t have the shipping location so only the platformName is in the repeater. Is that the reason why the code below won’t work anymore? It doesn’t work really anymore, the console shows nothing at the verbose.
$w(" #repeater1 ").data = unfilteredRecords.filter(obj => obj.platformName.toLowerCase().includes(searchWord.toLowerCase()) || obj.shippinglocation.toLowerCase().includes(searchWord.toLowerCase()));
@freedfoxworldwide You need to have all data that you want to filter on in the array.
1 Like
@andreas-kviby May I ask how do I do that?
It seems I have been baffled by code. I am not really a coder 