Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>First possibility</h2> <p>Do the same thing as you did with one word. Split user's input into separate words and test for each of them.</p> <h2>Second possibility - optimised filtering</h2> <p>Traversing the whole table seems very time consuming which could quite easily be optimised with a small overhead at the beginning. Since javascript objects are a kind of associative arrays you could do this:</p> <ol> <li>When page loads traverse table and parse texts of each cell. Parsing should be done using regular expressions, because it will make it simpler to exclude any punctuation and similar <code>var rx = /([a-z]+)/gi</code>. You can easily explude any single letter words here as well <code>var rx = /([a-z]{2,})/gi</code>.</li> <li>Store each word in associative object with relation to row.</li> <li>Filter using this object.</li> </ol> <p>How should your associative object look like:</p> <pre><code>var data = { "tomorrow": [1, 2, 3], "will": [3, 5, 6], "be": [78], "sunny": [3, 9, 19], ... }; </code></pre> <p>arrays of each word correspond to table row index. So when user enters whatever data in the textbox, you can easily split that into separate words and get all arrays:</p> <pre><code>var filter = $("#search_filter").val().split(" "); var firstWordRows = data[filter[0]]; </code></pre> <p>Then all you have to do is intersect those arrays and hide all rows except resulting ones.</p> <p>The good thing is you can define <em>stop</em> words that you don't filter (like <em>in, a, the, of, at,</em> etc.)...</p> <p><strong>Why even bother with the second one?</strong> If the page is more likely to be filtered several times as opposed to being loaded, then this overhead at page load will make your filtering snappy. Filtering will be very fast and less processor hungry.</p> <h2>Suggestion</h2> <p>I strongly suggest you do use some Javascript library that will make your life much simpler. Use jQuery for instance which doesn't have a long learning curve and works very well along with existing scripts you may have.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload