I'm using a ng-repeat and filter in angularJS like the phones tutorial but I'd like to highlight the search results in the page. With basic jQuery I would have simply parsed the page on key up on the input, but I'm trying to do it the angular way. Any ideas ?
My code :
<input id="search" type="text" placeholder="Recherche DCI" ng-model="search_query" autofocus>
<tr ng-repeat="dci in dcis | filter:search_query">
<td class='marque'>{{dci.marque}} ®</td>
<td class="dci">{{dci.dci}}</td>
</tr>
Use ng-class that is applied when the search term is related to the data the element contains.
So on your ng-repeated elements, you'd have
ng-class="{ className: search_query==elementRelatedValue}"
which would apply class "className" to elements dynamically when the condition is met.
About the problems with special caracter, I think just escaping you might lose regex search.
What about this:
An invalid regexp could be user just typing the text:
What do you think @Mik Cox?
There is standart Highlight filter in the angular-bootstrap:
typeaheadHighlight
Usage
With scope
{text:"Hello world", query:"world"}
renders inThanks for asking this as it was something I was dealing with as well.
Two things though:
First, The top answer is great but the comment on it is accurate that highlight() has problem with special characters. That comment suggests using an escaping chain which will work but they suggest using unescape() which is being phased out. What I ended up with:
Second, I was trying to do this in a data bound list of URLs. While in the highlight() string, you don't need to data bind.
Example:
Became:
Was running into problems with leaving them in {{ }} and getting all sorts of errors.
Hope this helps anybody running into the same problems.
If you are using the angular material library there is a built in directive called
md-highlight-text
From the documentation:
Link to docs: https://material.angularjs.org/latest/api/directive/mdHighlightText
My solution for highlight, used this with angular-ui-tree element: https://codepen.io/shnigi/pen/jKeaYG
Add css:
HTML:
And to add search input: