Saturday 15 January 2011

javascript - Custom filtering knockout js observableArray -


I have a searchable array that wants to filter by text in the field of research. Filtering updates after every important stroke, so I'm searching for the most efficient method.

See this JS-viddle for a simplified version of my problem:

Below, you can see a snippet from Bella so far filtering search field Takes a complete lesson in and examines the "name" field of each test site against it.

What do I want to filter the text into words and search for each word in every search field in the search field.

I type in "Search for user" in the search field for example, I want to return it only to testuite, which puts these words in any field (here in the name of two checksets "Users "And" qualified "in the description).

  self.filteredTestsuites = ko.computed (function () {// If there are many white spaces in the line, replace only one white space with fText = self.filterText (). ( / \ S + / G, ''); // If there is anything in the search box, filter for this // Now from now this filter does not divide the text and only the name field var filterteredCollection = ko.utils. Search for arrayFilter (self.testsuites) (), Task (test) {if (fText.length) returns (test.name.toUpperCase). IndexOf (fText.toUpperCase ()) gt; = 0); Return 1;}); Filtered filtered;}, to the left);  

My question is How can I find the most efficient? One potential solution is that for every word in the search field, I search for each field in the current test list. Although I would like a more general solution, where I do not have to specify the fields (such as name, description, etc.), and I am also unsure about the efficiency of this method.

Suggestions?

A simple solution that has been used earlier to do a long search for all the searchable key / text key To merge into the appropriate text and use it to do all your searches.

A slightly simplified version is below

  function view modal () {var self = this, x, i, suit; Self.filterText = ko.observable (""); // Text from the search field // test collections self.testsuites = ko.observableArray ([{name: "register the user", description: "blah blah blah", etc.: "many more fields ..."}, { Name: "many more details", etc.: "many more fields"}, {name: "send money", description: "na-na-no-bat man", etc.: "many more fields"}]); Suit = self.testsuites (); (I = 0; i & lt; suite.label; i ++) for {suit [i] ["search_content"] = "& gt;"; (Suite (i.e. [i]. (If (suit! I) .hasOwnProperty (x) || x == "search_content" type suit [i] [x]! == "string") {continue ;} Suit [i] ["search_content"] + = Suite [i] [x] .toUpperCase ();}} // Self collection of tests after going through the search filter. Filteredstustsites = co.cospid (function ( ) {Var reg; // If there are many white spaces in the row, replace only one white space with fText = self.filterText (). (/ \ S + / gi, '|'); fText = fText Replace (/ \ | \ * $ / gi, ''); console.log ("regex:", fText); reg = new RegExp (FText, "gi"); // if in the search box If there is anything, then filter it for // Now this filter does not divide the text and only the name field var filterteredCollection = ko.utils.arrayFilter (auto.php), function (test) {if (fText) .length) Return test.search_content.match (reg); and return1;}); filename filtering;}, self);} $ (document) .ready (function () {var vm = New ViewModel (); ApplyBindings (vm);});  

No comments:

Post a Comment