Hello,
How can I filter using Select?
<input placeholder="Search" type="text" data-datatable-search="#kt_companies_table"/>
I use this successfully. But I also need the select option.
Can you help?
Hi Hüsame Özçelik,
You can use TypeScript or JavaScript code to apply a filter using a select dropdown. Here's how you can do it with the DataTables API:
this.dataTable
.setFilter({
column, // Specify the column index or name
type: "select", // Use "select" for dropdown filtering
value: "selected option value", // The value selected from the dropdown
})
.redraw();
<select >
<option value="">All</option>
<option value="Value1">Value1</option>
<option value="Value2">Value2</option>
</select>
document.getElementById("filter-select").addEventListener("change", function () {
const selectedValue = this.value;
this.dataTable
.setFilter({
column: 1, // Adjust the column index
type: "select",
value: selectedValue,
})
.redraw();
});
/metronic-tailwind-html/src/app/datatables/current-sessions.js
/metronic-tailwind-html/dist/html/demo1/account/security/current-sessions/index.html
Thanks so much for your help. I got this working. There's just one problem. If it does not find any results in the filter and I change the filter again, it does not make a request and the table is not redrawn. Do you have any knowledge on this subject? The event is listened to and the value is reflected in the console, but no drawing is made.
document.getElementById("status-filter").addEventListener("change", function () {
const selectedValue = this.value;
dataTable.setFilter({
column: "status",
type: "select",
value: selectedValue,
}).redraw();
});