I recently wrote about Filtering Table Data with Knockout Computeds. I'm not sure I shared anything earth shattering as others have already written about that topic. My real motivation for writing that post was to provide a foundation to compare/contrast filtering a plain HTML table against an Oracle JET ojTable
that is based on an oj.ArrayTableDataSource (not to be confused with oj.CollectionTableDataSource, which wraps an oj.Collection and has its own where
method and cookbook example).
Let's jettify the Filtering Table Data with Knockout Computeds example. Instead of <input type="text">
, we'll use ojInputText
. Instead of a Bootstrap table, we'll use ojTable
. Here is the jsFiddle:
As you can see, the code is fairly similar. A couple of key differences to note are the rawValue
parameter and the lack of a computed. Instead of using the value
and valueUpdate
parameters, the ojInputText
component uses value
and rawValue
. The ojInputText
value
parameter acts just like the standard knockout parameter, tracking the final updated value after losing focus. The rawValue
parameter tracks changes as you type, which is similar to the standard input
with the valueUpdate
parameter. The other difference between these examples is that this example didn't use a computed observable, but rather the oj.ArrayTableDataSource.reset() method to replace the table's array when filtering.
As with the Knockout computed example, we can debounce this example so that the array filter code doesn't run with every key press, but only after a predetermined pause. I included the rate limiting extension in the JavaScript, but commented it out for example purposes. When filtering large data sets, it might make sense to switch from observing rawValue
to observing the value
parameter. Another option is to include a Search button that triggers the filter code on click. You can find an example of this here.
Note: Filtering arrays requires an ES6 compatible browser or some type of polyfill library. The examples include Lazy.js as a polyfill so you can run the jsFiddle example in a wider range of web browsers.
This looks very promising. Do you know how to preserve the sort order if an ojTable is sorted? The call to datasource.reset() filters the table but loses the sort order and the sort indicators unfortunately remain visible in the table header.
ReplyDeleteExcellent Work Jim.....
ReplyDeleteOne question though, this current search implimentation is case sensitive
How can we make this as caseinsensitive