How to filter from centre part of column value
How to filter from centre part of column value
Link to test case: http://live.datatables.net/zafocaka/1/edit
Debugger code (debug.datatables.net):
Error messages shown: "Script error. (line 0)" ?
Description of problem: The Whyte notation for steam locomotives denotes leading wheels, coupled driving wheels and trailing wheels, hence a typical denomination of 0-6-0. If water is carried onboard, there is a suffix to indicate the type of tank, e.g. T, ST, or WT.
I have a list of locomotives and wheel arrangements, but would like to filter by driving wheel sets; i.e. 4 coupled, 6 coupled etc. so drawing together all arrangements that have the same number of driving wheels, irrespective of other wheels or tank type.
I have a table example that shows such data, although I'm sorry I couldn't make a 'rendered' column showing just the driving wheels (based on the wheelbase) physically display; adding a "data" : null column upset the table; although the logic would have been:
{
"data": null,
"render": function (data, type, row) {
if ((row.wheelbase.substr(2, 2) === "10") || (row.wheelbase.substr(2, 2) === "12")) {
return row.wheelbase.substring(4, 2);
}
else return row.wheelbase.substr(2, 1);
},
}
So instead, I have added a column to illustrate the filter values I am trying to apply.
I have tried adding .subst in the filter logic, but can't get anything to work.
For some reason the filter (across all the wheelbase entries) is not displaying on the example, although is showing up fine on my coding page.
Ideally, I would like to filter on 4-coupled, 6-coupled, 8-coupled etc. Is there a way I can achieve this ?
Here is my example. http://live.datatables.net/zafocaka/1/edit
Thanks, Rob.
This question has accepted answers - jump to:
Answers
Looking at the browser's console, not the JS BIN console tab, you will see these errors:
You have jquery.js loading after dattatable.js. It needs to load first.
Use orthogonal data to set the value for filtering. You can extract the middle number for the filter value. Then create a select list that contains the option text, 4-coupled, 6-coupled, 8-coupled etc, and the wheelbase values, 4, 6, 8, etc. Maybe something like this:
http://live.datatables.net/zafocaka/2/edit
Kevin
Kevin, thank you.
I had not realised the sequence of the jquery .src was sequence critical; I shall check through all my pages.
Wow, solved in one, and very tidy. How interesting that you have used a render function, but left the original column value intact. The extraction of the middle value is very clean. However, I don't see where the value of wb is being held prior to a filter action compare; is it in effect a hidden column ?
You have added logic to the existing filter, a few bits for me to comprehend here.
Thank you for helping. Rob
The use of orthogonal data allows for using different values, in the same column, for different operations like display, filter and sort. The
wb
value is simply stored in the wheelbase column for filtering. You can remove thecoupled
column if you don't need it.Load order of the .js libraries can be important. If one library, like datatables, requires another like jquery then it (jquery) needs to be loaded first. Use the Download Builder to get the correct files in the proper order for Datatables.
Kevin
Thanks Kevin.
Kevin, Just a further thought; there will be occasions when the wheelbase content is to be confirmed, so unknown. I found the datatable threw an error message "DataTables warning: table id=wheelbase - Requested unknown parameter 'wheelbase' for row 6, column 2." when the column value was blank.
So I have added a test in the columns render, to check 'wb' before it is returned (saved).
if (wb !== undefined) { // Required when no value available
return wb; // Use that for filtering
}
- and it now works even with multiple blanks, and the filter works with the data it finds. Really good. Cheers, Rob