Kendo TreeView搜索突出显示

编程入门 行业动态 更新时间:2024-10-25 21:19:54
本文介绍了Kendo TreeView搜索突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带spriteclass的KendoTreeview。我想用我的搜索词突出显示节点(根节点和子节点)。我已经实现了搜索功能。但是当我搜索它时的问题是突出显示节点中的术语但在第一次搜索后缺少节点中的SpriteClass。有什么想法?

I have a KendoTreeview with spriteclass. I want to highlight the nodes (root as well as child nodes) with my search term. I have implemented the search functionality. But the issue when i search it is highlighting the term in the nodes but missing the SpriteClass in the nodes after first search. Any idea ?

jsFiddle code

$('#search-term').on('keyup', function () { $('span.k-in > span.highlight').each(function () { $(this).parent().text($(this).parent().text()); }); // ignore if no search term if ($.trim($(this).val()) == '') { return; } var term = this.value.toUpperCase(); var tlen = term.length; $('#treeview-sprites span.k-in').each(function (index) { var text = $(this).text(); var html = ''; var q = 0; while ((p = text.toUpperCase().indexOf(term, q)) >= 0) { html += text.substring(q, p) + '<span class="highlight">' + text.substr(p, tlen) + '</span>'; q = p + tlen; } if (q > 0) { html += text.substring(q); $(this).html(html); $(this).parentsUntil('.k-treeview').filter('.k-item').each( function (index, element) { $('#treeview-sprites').data('kendoTreeView').expand($(this)); $(this).data('search-term', term); }); } }); $("#treeview-sprites").kendoTreeView({ dataSource: [{ text: "My Documents", expanded: true, spriteCssClass: "rootfolder", items: [{ text: "Kendo UI Project", expanded: true, spriteCssClass: "folder", items: [{ text: "about.html", spriteCssClass: "html" }, { text: "index.html", spriteCssClass: "html" }, { text: "logo.png", spriteCssClass: "image" }] }, { text: "New Web Site", expanded: true, spriteCssClass: "folder", items: [{ text: "mockup.jpg", spriteCssClass: "image" }, { text: "Research.pdf", spriteCssClass: "pdf" }, ] }, { text: "Reports", expanded: true, spriteCssClass: "folder", items: [{ text: "February.pdf", spriteCssClass: "pdf" }, { text: "March.pdf", spriteCssClass: "pdf" }, { text: "April.pdf", spriteCssClass: "pdf" }] }] }] })

推荐答案

Kendo的树视图小部件不喜欢它,如果你在它的HTML中,所以我建议修改数据源(这将需要编码 DS中所有项目的选项。)

Kendo's tree view widget doesn't like it if you muck around in its HTML, so I suggest modifying the data source instead (this will require the encoded option for all items in the DS).

在keyup处理程序中,只要搜索清除突出显示,就会重置DS,而不是替换你直接使用元素的HTML设置模型的文本属性:

In the keyup handler, you reset the DS whenever you search to clear highlighting, then instead of replacing the element's HTML directly, you set the model's text property:

$('#search-term').on('keyup', function () { var treeView = $("#treeview-sprites").getKendoTreeView(); treeView.dataSource.data(pristine); // ignore if no search term if ($.trim($(this).val()) == '') { return; } var term = this.value.toUpperCase(); var tlen = term.length; $('#treeview-sprites span.k-in').each(function (index) { var text = $(this).text(); var html = ''; var q = 0; while ((p = text.toUpperCase().indexOf(term, q)) >= 0) { html += text.substring(q, p) + '<span class="highlight">' + text.substr(p, tlen) + '</span>'; q = p + tlen; } if (q > 0) { html += text.substring(q); var dataItem = treeView.dataItem($(this)); dataItem.set("text", html); $(this).parentsUntil('.k-treeview').filter('.k-item').each( function (index, element) { $('#treeview-sprites').data('kendoTreeView').expand($(this)); $(this).data('search-term', term); }); } }); $('#treeview-sprites .k-item').each(function () { if ($(this).data('search-term') != term) { $('#treeview-sprites').data('kendoTreeView').collapse($(this)); } }); });

树定义需要 encoded 选项这个工作:

The tree definition needs the encoded option for this to work:

var pristine = [{ encoded: false, text: "Kendo UI Project", expanded: true, spriteCssClass: "folder", items: [{ encoded: false, text: "about.html", spriteCssClass: "html" }, { encoded: false, text: "index.html", spriteCssClass: "html" }, { encoded: false, text: "logo.png", spriteCssClass: "image" }] }, { encoded: false, text: "New Web Site", expanded: true, spriteCssClass: "folder", items: [{ encoded: false, text: "mockup.jpg", spriteCssClass: "image" }, { encoded: false, text: "Research.pdf", spriteCssClass: "pdf" }, ] }, { encoded: false, text: "Reports", expanded: true, spriteCssClass: "folder", items: [{ encoded: false, text: "February.pdf", spriteCssClass: "pdf" }, { encoded: false, text: "March.pdf", spriteCssClass: "pdf" }, { encoded: false, text: "April.pdf", spriteCssClass: "pdf" }] }]; $("#treeview-sprites").kendoTreeView({ dataSource: [{ text: "My Documents", expanded: true, spriteCssClass: "rootfolder", items: pristine }] });

(演示)

更多推荐

Kendo TreeView搜索突出显示

本文发布于:2023-10-31 01:40:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544611.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Kendo   TreeView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!