|
| 1 | +--- |
| 2 | +title: Highlight a Search Entry in the Editor Content |
| 3 | +description: How can I highlight a search entry in the content of the Telerik UI for {{ site.framework }} Editor? |
| 4 | +type: how-to |
| 5 | +page_title: Highlight a Search Entry in the Editor Content |
| 6 | +slug: editor-highlight-search-entry |
| 7 | +tags: editor, highlight, search, entry, grid, window |
| 8 | +res_type: kb |
| 9 | +component: editor |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | +<table> |
| 14 | + <tbody> |
| 15 | + <tr> |
| 16 | + <td>Product Version</td> |
| 17 | + <td>2024.1.130</td> |
| 18 | + </tr> |
| 19 | + <tr> |
| 20 | + <td>Product</td> |
| 21 | + <td>Editor for Progress® Telerik® {{ site.product_short }}</td> |
| 22 | + </tr> |
| 23 | + </tbody> |
| 24 | +</table> |
| 25 | + |
| 26 | +## Description |
| 27 | + |
| 28 | +How can I highlight all occurrences of a search entry into the Editor's content? |
| 29 | + |
| 30 | +This article uses the following use case to demonstrate this functionality—A [Grid](https://demos.telerik.com/{{ site.platform }}/grid/search-panel) component contains a custom command that opens a [Window](https://demos.telerik.com/{{ site.platform }}/window) with an [Editor](https://demos.telerik.com/{{ site.platform }}/editor) that binds to a specified Grid property. When the user searches through the Grid's data and opens the Window that holds the Editor, all occurrences of the search entry must be highlighted in the Editor. |
| 31 | + |
| 32 | +## Solution |
| 33 | + |
| 34 | +The example below relies on the following key steps: |
| 35 | + |
| 36 | +1. Select the search entry entered in the search panel. |
| 37 | +1. Access the Editor's HTML content and split it by empty space to create an array of substrings. |
| 38 | +1. Loop through the array and check if any of its elements contains the search entry. |
| 39 | +1. Wrap each occurrence in a `span` element with a class `highlight` and store the new content in a string variable. |
| 40 | +1. Update the Editor content based on the string variable. |
| 41 | +1. Set a background color to the `highlight` class. |
| 42 | + |
| 43 | +```HtmlHelper |
| 44 | + @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.EmployeeViewModel>() |
| 45 | + .Name("NotesAudit") |
| 46 | + .Columns(columns => { |
| 47 | + columns.Bound(e => e.FirstName); |
| 48 | + columns.Bound(e => e.LastName); |
| 49 | + columns.Bound(e => e.Title); |
| 50 | + columns.Command(command => command.Custom("Notes").Click("showDetails")).Title("Notes").Width(70); |
| 51 | + }) |
| 52 | + .ToolBar(toolbar => |
| 53 | + { |
| 54 | + toolbar.Search().Text("Search Notes..."); |
| 55 | + }) |
| 56 | + .Search(s => |
| 57 | + { |
| 58 | + s.Field(f => f.Title, "contains"); |
| 59 | + }) |
| 60 | + .DataSource(dataSource => dataSource |
| 61 | + .Ajax() |
| 62 | + .Read(read => read.Action("CustomCommand_Read", "Grid")) |
| 63 | + ) |
| 64 | + ) |
| 65 | +
|
| 66 | + @(Html.Kendo().Window() |
| 67 | + .Name("Notes") |
| 68 | + .Title("Notes") |
| 69 | + .Visible(false) |
| 70 | + .Modal(true) |
| 71 | + .Draggable(true) |
| 72 | + .Content(@<text> |
| 73 | + @(Html.Kendo().Editor() |
| 74 | + .Name("NotesEditor") |
| 75 | + .Encoded(true) |
| 76 | + .HtmlAttributes(new { data_bind = "value: Title", style = "width:100%; height:720px" }) |
| 77 | + ) |
| 78 | + </text>) |
| 79 | + .Width(1300) |
| 80 | + .Height(900) |
| 81 | + ) |
| 82 | +``` |
| 83 | +{% if site.core %} |
| 84 | +```TagHelper |
| 85 | + @addTagHelper *, Kendo.Mvc |
| 86 | +
|
| 87 | + <kendo-grid name="NotesAudit"> |
| 88 | + <datasource type="DataSourceTagHelperType.Ajax"> |
| 89 | + <transport> |
| 90 | + <read url="@Url.Action("CustomCommand_Read", "Grid")"/> |
| 91 | + </transport> |
| 92 | + </datasource> |
| 93 | + <columns> |
| 94 | + <column field="FirstName"/> |
| 95 | + <column field="LastName"/> |
| 96 | + <column field="Title"/> |
| 97 | + <column width="70" title="Notes"> |
| 98 | + <commands> |
| 99 | + <column-command text="Notes" click="showDetails"/> |
| 100 | + </commands> |
| 101 | + </column> |
| 102 | + </columns> |
| 103 | + <toolbar> |
| 104 | + <toolbar-button name="search"></toolbar-button> |
| 105 | + </toolbar> |
| 106 | + <search fields-extended="@(new object[] { new { Name = "Title", Operator = "contains"}})"></search> |
| 107 | + </kendo-grid> |
| 108 | +
|
| 109 | + <kendo-window name="Notes" |
| 110 | + title="Notes" |
| 111 | + visible="false" |
| 112 | + modal="true" |
| 113 | + draggable="true" |
| 114 | + width="1300" |
| 115 | + height="900"> |
| 116 | + <content> |
| 117 | + <kendo-editor name="NotesEditor" encoded="true" data-bind="value: Title" style="width:100%; height:720px"> |
| 118 | + </kendo-editor> |
| 119 | + </content> |
| 120 | + </kendo-window> |
| 121 | +``` |
| 122 | +{% endif %} |
| 123 | +```Scripts |
| 124 | +<script type="text/javascript"> |
| 125 | + function showDetails(event) { |
| 126 | + var grid = $("#NotesAudit").getKendoGrid(); // Get a reference to the Grid. |
| 127 | + let dataItem = grid.dataItem($(event.target).closest("tr")); //Get the data item of the respective row. |
| 128 | + var window = $("#Notes").data("kendoWindow"); // Get a reference to the Window. |
| 129 | + var editor = $("#NotesEditor").data("kendoEditor"); // Get a reference to the Editor. |
| 130 | + kendo.bind(editor.element, dataItem); // Bind the Editor to the data item. |
| 131 | + let searchedText = $(".k-searchbox input").val().toLowerCase(); // Access the search panel entry. |
| 132 | + var editorBody = $(editor.body); // Get the Editor's body element. |
| 133 | +
|
| 134 | + window.open().center(); // Open the Window. |
| 135 | + highlight(searchedText, editorBody); // Call the "highlight" function. |
| 136 | + } |
| 137 | +
|
| 138 | + function highlight(text, editorBody) { |
| 139 | + var editorBodyHTML= $(editorBody).html(); |
| 140 | + var splittedContent = $(editorBody).html().split(" "); // Convert the Editor's content into an array of substrings. |
| 141 | + var newContent = ""; |
| 142 | + for(var i = 0; i < splittedContent.length; i++){ // Loop through the array. |
| 143 | + var index = splittedContent[i].toLowerCase().indexOf(text); |
| 144 | + if(index >= 0) { // Check for an occurrence. |
| 145 | + splittedContent[i] = splittedContent[i].substring(0,index) + "<span class='highlight'>" + splittedContent[i].substring(index,index+text.length) + "</span>" + splittedContent[i].substring(index + text.length); // Wrap each occurrence in a "span" element. |
| 146 | + } |
| 147 | + newContent += (" " + splittedContent[i]); |
| 148 | + } |
| 149 | +
|
| 150 | + $(editorBody).html(newContent); // Update the Editor's content. |
| 151 | + $(editorBody).find('.highlight').css('background-color', 'yellow'); // Highlight the occurrences. |
| 152 | + } |
| 153 | +</script> |
| 154 | +``` |
| 155 | + |
| 156 | +{% if site.core %} |
| 157 | +For a runnable example based on the code above, refer to the following REPL samples: |
| 158 | + |
| 159 | +* [Sample code with the Editor HtmlHelper](https://netcorerepl.telerik.com/meOHnFvd41L6YZkY45) |
| 160 | +* [Sample code with the Editor TagHelper](https://netcorerepl.telerik.com/QIanHFbH44hh6jUB16) |
| 161 | +{% else %} |
| 162 | +For a runnable example based on the code above, refer to the [REPL example on highlighting the search entry within Editor's content](https://netcorerepl.telerik.com/meOHnFvd41L6YZkY45). |
| 163 | +{% endif %} |
| 164 | + |
| 165 | +## More {{ site.framework }} Editor Resources |
| 166 | + |
| 167 | +* [{{ site.framework }} Editor Documentation]({%slug htmlhelpers_editor_aspnetcore%}) |
| 168 | + |
| 169 | +* [{{ site.framework }} Editor Demos](https://demos.telerik.com/{{ site.platform }}/editor) |
| 170 | + |
| 171 | +{% if site.core %} |
| 172 | +* [{{ site.framework }} Editor Product Page](https://www.telerik.com/aspnet-core-ui/editor) |
| 173 | + |
| 174 | +* [Telerik UI for {{ site.framework }} Video Onboarding Course (Free for trial users and license holders)]({%slug virtualclass_uiforcore%}) |
| 175 | + |
| 176 | +* [Telerik UI for {{ site.framework }} Forums](https://www.telerik.com/forums/aspnet-core-ui) |
| 177 | + |
| 178 | +{% else %} |
| 179 | +* [{{ site.framework }} Editor Product Page](https://www.telerik.com/aspnet-mvc/editor) |
| 180 | + |
| 181 | +* [Telerik UI for {{ site.framework }} Video Onboarding Course (Free for trial users and license holders)]({%slug virtualclass_uiformvc%}) |
| 182 | + |
| 183 | +* [Telerik UI for {{ site.framework }} Forums](https://www.telerik.com/forums/aspnet-mvc) |
| 184 | +{% endif %} |
| 185 | + |
| 186 | +## See Also |
| 187 | + |
| 188 | +* [Client-Side API Reference of the Editor for {{ site.framework }}](https://docs.telerik.com/kendo-ui/api/javascript/ui/editor) |
| 189 | +* [Server-Side API Reference of the Editor for {{ site.framework }}](https://docs.telerik.com/{{ site.platform }}/api/editor) |
| 190 | +{% if site.core %} |
| 191 | +* [Server-Side TagHelper API Reference of the Editor for {{ site.framework }}](https://docs.telerik.com/{{ site.platform }}/api/taghelpers/editor) |
| 192 | +{% endif %} |
| 193 | +* [Telerik UI for {{ site.framework }} Breaking Changes]({%slug breakingchanges_2023%}) |
| 194 | +* [Telerik UI for {{ site.framework }} Knowledge Base](https://docs.telerik.com/{{ site.platform }}/knowledge-base) |
0 commit comments