I would like to make a filter using List.js. I have two simple buttons: "BUY" and "SELL" Ideally they should be used as reference to search into the value of a specific cell but I cannot make it work.
Here it is one of the objects:
"trade": "<span class=\"badge rounded-pill bg-danger text-white\">SELL</span>",
"date": "18/01/2022",
"price": "60",
"quantity": "10"
}
This is the code of the filter
$('.filter').click(function () {
var search = $(this).text().toUpperCase(); // val = SELL
featureList.filter(function (item) {
return item.values().trade.includes(search);
});
});
If I manually replace search with the value "SELL" in the includes() function, I get the result I'm expecting.. using the variable no..
Moreover is there a way to automatically select the column where to search? I've tried to add a data-filter attribute to the button and I managed to retreive the value but if I try the following code doesn't work
$('.filter').click(function () {
var col = $(this).data('filter'); // val = trade
featureList.filter(function (item) {
return item.values().col.includes('SELL');
});
});
Thank you for helping me to understand what I clearly do wrong
You were missing class="list" for the tbody element. This is necessary for list.js. Also your search variable had white spaces on each end due to the span element in the trade cells. By trimming the whitespaces your search variable can used in the includes() method.