11/*!
2- * jQuery Bootgrid v1.4.2 - 01/20 /2017
2+ * jQuery Bootgrid v1.4.2 - 02/10 /2017
33 * Copyright (c) 2014-2017 Rafael Staib (http://www.jquery-bootgrid.com)
44 * Licensed under MIT http://www.opensource.org/licenses/MIT
55 */
@@ -87,6 +87,45 @@ function highlightAppendedRows(rows) {
8787 }
8888}
8989
90+ // Replaces all occurrences of the word in the given html
91+ // Original source: http://stackoverflow.com/questions/8503121/replace-words-in-a-string-but-ignore-html
92+ function highlightResults ( html ) {
93+ var that = this ,
94+ word = that . searchPhrase ,
95+ tpl = this . options . templates ,
96+ css = this . options . css ,
97+ container = document . createElement ( "div" ) ,
98+ regexFlag = that . options . caseSensitive ? 'g' : 'gi' ,
99+ regex = new RegExp ( '(' + word + ')' , regexFlag ) ;
100+
101+ container . innerHTML = html ;
102+
103+ // Traverses the given element and apply the text replacement function with the given regex
104+ function traverseElement ( el , regex , textReplacerFunc ) {
105+ var child = el . lastChild ;
106+ while ( child ) {
107+ if ( child . nodeType === 1 ) {
108+ traverseElement ( child , regex , textReplacerFunc ) ;
109+ } else if ( child . nodeType === 3 ) {
110+ textReplacerFunc ( child , regex ) ;
111+ }
112+ child = child . previousSibling ;
113+ }
114+ }
115+
116+ traverseElement ( container , regex , function ( textNode , regex ) {
117+ // need this as the rendered cell will encode any html tags which will not render
118+ textNode . data = textNode . data . replace ( regex , "{{$1}}" ) ;
119+ } ) ;
120+
121+ var reg = new RegExp ( "(?:{{)(.*?)(?:}})" , "g" ) ;
122+ return container . innerHTML . replace ( reg , function ( str , el ) {
123+ return tpl . highlightResults . resolve ( getParams . call ( that , {
124+ content : el
125+ } ) ) ;
126+ } ) ;
127+ }
128+
90129function isVisible ( column ) {
91130 return column . visible ;
92131}
@@ -162,7 +201,7 @@ function loadData() {
162201
163202 for ( var i = 0 ; i < that . columns . length ; i ++ ) {
164203 column = that . columns [ i ] ;
165- if ( column . searchable && ( column . visible || that . options . searchSettings . includeHidden ) &&
204+ if ( column . searchable && ( column . visible || that . options . searchSettings . includeHidden ) &&
166205 column . converter . to ( row [ column . id ] , row , column , that ) . toString ( ) . search ( searchPattern ) > - 1 ) {
167206 return true ;
168207 }
@@ -609,6 +648,12 @@ function renderRows(rows) {
609648 column . formatter . call ( that , column , row ) :
610649 column . converter . to ( row [ column . id ] , row , column , that ) ,
611650 cssClass = ( column . cssClass . length > 0 ) ? " " + column . cssClass : "" ;
651+
652+ // Highlight search phrase if available
653+ if ( that . searchPhrase !== '' && that . options . searchSettings . highlightResults ) {
654+ value = highlightResults . call ( that , value ) ;
655+ }
656+
612657 cells += tpl . cell . resolve ( getParams . call ( that , {
613658 content : ( value == null || value === "" ) ? " " : value ,
614659 css : ( ( column . align === "right" ) ? css . right : ( column . align === "center" ) ?
@@ -1093,14 +1138,24 @@ Grid.defaults = {
10931138 characters : 1 ,
10941139
10951140 /**
1096- * Option if search should ignore hidden columns
1141+ * Option if search should include hidden columns
10971142 *
10981143 * @property includeHidden
10991144 * @type Boolean
11001145 * @default false
11011146 * @for searchSettings
11021147 **/
1103- includeHidden : false
1148+ includeHidden : false ,
1149+
1150+ /**
1151+ * Option if search term in results should be highlighted
1152+ *
1153+ * @property highlightResults
1154+ * @type Boolean
1155+ * @default false
1156+ * @for searchSettings
1157+ **/
1158+ highlightResults : true
11041159 } ,
11051160
11061161 /**
@@ -1266,6 +1321,7 @@ Grid.defaults = {
12661321 dropDownMenuText : "dropdown-text" , // must be a unique class name or constellation of class names within the actionDropDown
12671322 footer : "bootgrid-footer container-fluid" ,
12681323 header : "bootgrid-header container-fluid" ,
1324+ highlightResults : "bootgrid-search-highlight" ,
12691325 icon : "icon glyphicon" ,
12701326 iconColumns : "glyphicon-th-list" ,
12711327 iconDown : "glyphicon-chevron-down" ,
@@ -1276,7 +1332,6 @@ Grid.defaults = {
12761332 left : "text-left" ,
12771333 pagination : "pagination" , // must be a unique class name or constellation of class names within the header and footer
12781334 paginationButton : "button" , // must be a unique class name or constellation of class names within the pagination
1279-
12801335 /**
12811336 * CSS class to select the parent div which activates responsive mode.
12821337 *
@@ -1423,6 +1478,7 @@ Grid.defaults = {
14231478 footer : "<div id=\"{{ctx.id}}\" class=\"{{css.footer}}\"><div class=\"row\"><div class=\"col-sm-6\"><p class=\"{{css.pagination}}\"></p></div><div class=\"col-sm-6 infoBar\"><p class=\"{{css.infos}}\"></p></div></div></div>" ,
14241479 header : "<div id=\"{{ctx.id}}\" class=\"{{css.header}}\"><div class=\"row\"><div class=\"col-sm-12 actionBar\"><p class=\"{{css.search}}\"></p><p class=\"{{css.actions}}\"></p></div></div></div>" ,
14251480 headerCell : "<th data-column-id=\"{{ctx.column.id}}\" class=\"{{ctx.css}}\" style=\"{{ctx.style}}\"><a href=\"javascript:void(0);\" class=\"{{css.columnHeaderAnchor}} {{ctx.sortable}}\"><span class=\"{{css.columnHeaderText}}\">{{ctx.column.text}}</span>{{ctx.icon}}</a></th>" ,
1481+ highlightResults : "<span class=\"{{css.highlightResults}}\">{{ctx.content}}</span>" ,
14261482 icon : "<span class=\"{{css.icon}} {{ctx.iconCss}}\"></span>" ,
14271483 infos : "<div class=\"{{css.infos}}\">{{lbl.infos}}</div>" ,
14281484 loading : "<tr><td colspan=\"{{ctx.columns}}\" class=\"loading\">{{lbl.loading}}</td></tr>" ,
0 commit comments