Webix DataTable无列ID时如何实现列筛选功能?
Hey there, let's tackle this Webix datatable filtering issue. The core problem here is that Webix's default filtering relies on matching column field attributes to data keys, but your dataset is a plain 2D array with no object keys—so the table doesn't know which array element corresponds to which column.
Luckily, you have two solid solutions that don't require modifying your original dataset at all:
Solution 1: Map Columns to Array Indices (Simplest Approach)
You don't need to change your dataset; just update your column definitions to include field properties that point to the index of each element in your data arrays. This tells Webix exactly which part of the array to filter against.
Here's how to adjust your columns:
columns: [ { id:"col1", header:"ID", width:100, field:0, filter:"text" }, { id:"col2", header:"Name", width:150, field:1, filter:"text" }, { id:"col3", header:"Age", width:100, field:2, filter:"text" } ]
The field:0, field:1, etc., map each column to the first, second, third elements of your data rows respectively. Webix's built-in filtering will work automatically after this change—no extra event handlers needed.
Solution 2: Manual Filtering with onAfterFilter (For Custom Logic)
If you need more control over the filtering behavior, you can use the onAfterFilter event to manually process the filter values and update the table data. Just make sure to keep a copy of your original data so you don't lose it when applying filters.
Here's a complete code example:
// Store original data to reset from after filters const originalData = [ ["1", "Alex", "30"], ["2", "Bob", "25"], ["3", "Charlie", "35"] ]; webix.ui({ view: "datatable", id: "table", columns: [ { id:"col1", header:"ID", width:100, filter:"text" }, { id:"col2", header:"Name", width:150, filter:"text" }, { id:"col3", header:"Age", width:100, filter:"text" } ], data: originalData, on: { onAfterFilter: function() { // Get current filter values from the table const filters = this.getFilter(); // Filter the original data based on active filters const filteredData = originalData.filter(row => { // Check each column's filter condition for (const colId in filters) { const filterValue = filters[colId].value.toLowerCase(); // Map column ID to array index (col1 → 0, col2 →1, etc.) const rowIndex = parseInt(colId.replace("col", "")) - 1; const rowValue = row[rowIndex].toLowerCase(); // If the row doesn't match the filter, exclude it if (!rowValue.includes(filterValue)) { return false; } } // If all filters are matched, keep the row return true; }); // Update the table with filtered data this.clearAll(); this.parse(filteredData); } } });
This approach gives you full control—you can tweak the matching logic (like using exact matches instead of substring checks) or add custom rules as needed.
Which to Choose?
- Use Solution 1 if you just need basic filtering to work with minimal code changes.
- Use Solution 2 if you require custom filtering behavior that Webix's built-in tools don't support.
内容的提问来源于stack exchange,提问作者A.G.Progm.Enthusiast

