擴(kuò)展jquery easyui tree的搜索樹節(jié)點(diǎn)方法(推薦)
如下所示:
/** * 1)擴(kuò)展jquery easyui tree的節(jié)點(diǎn)檢索方法。使用方法如下: * $("#treeId").tree("search", searchText); * 其中,treeId為easyui tree的根UL元素的ID,searchText為檢索的文本。 * 如果searchText為空或"",將恢復(fù)展示所有節(jié)點(diǎn)為正常狀態(tài) */ (function($) { $.extend($.fn.tree.methods, { /** * 擴(kuò)展easyui tree的搜索方法 * @param tree easyui tree的根DOM節(jié)點(diǎn)(UL節(jié)點(diǎn))的jQuery對象 * @param searchText 檢索的文本 * @param this-context easyui tree的tree對象 */ search: function(jqTree, searchText) { //easyui tree的tree對象??梢酝ㄟ^tree.methodName(jqTree)方式調(diào)用easyui tree的方法 var tree = this; //獲取所有的樹節(jié)點(diǎn) var nodeList = getAllNodes(jqTree, tree); //如果沒有搜索條件,則展示所有樹節(jié)點(diǎn) searchText = $.trim(searchText); if (searchText == "") { for (var i=0; i<nodeList.length; i++) { $(".tree-node-targeted", nodeList[i].target).removeClass("tree-node-targeted"); $(nodeList[i].target).show(); } //展開已選擇的節(jié)點(diǎn)(如果之前選擇了) var selectedNode = tree.getSelected(jqTree); if (selectedNode) { tree.expandTo(jqTree, selectedNode.target); } return; } //搜索匹配的節(jié)點(diǎn)并高亮顯示 var matchedNodeList = []; if (nodeList && nodeList.length>0) { var node = null; for (var i=0; i<nodeList.length; i++) { node = nodeList[i]; if (isMatch(searchText, node.text)) { matchedNodeList.push(node); } } //隱藏所有節(jié)點(diǎn) for (var i=0; i<nodeList.length; i++) { $(".tree-node-targeted", nodeList[i].target).removeClass("tree-node-targeted"); $(nodeList[i].target).hide(); } //折疊所有節(jié)點(diǎn) tree.collapseAll(jqTree); //展示所有匹配的節(jié)點(diǎn)以及父節(jié)點(diǎn) for (var i=0; i<matchedNodeList.length; i++) { showMatchedNode(jqTree, tree, matchedNodeList[i]); } } }, /** * 展示節(jié)點(diǎn)的子節(jié)點(diǎn)(子節(jié)點(diǎn)有可能在搜索的過程中被隱藏了) * @param node easyui tree節(jié)點(diǎn) */ showChildren: function(jqTree, node) { //easyui tree的tree對象??梢酝ㄟ^tree.methodName(jqTree)方式調(diào)用easyui tree的方法 var tree = this; //展示子節(jié)點(diǎn) if (!tree.isLeaf(jqTree, node.target)) { var children = tree.getChildren(jqTree, node.target); if (children && children.length>0) { for (var i=0; i<children.length; i++) { if ($(children[i].target).is(":hidden")) { $(children[i].target).show(); } } } } }, /** * 將滾動條滾動到指定的節(jié)點(diǎn)位置,使該節(jié)點(diǎn)可見(如果有滾動條才滾動,沒有滾動條就不滾動) * @param param { * treeContainer: easyui tree的容器(即存在滾動條的樹容器)。如果為null,則取easyui tree的根UL節(jié)點(diǎn)的父節(jié)點(diǎn)。 * targetNode: 將要滾動到的easyui tree節(jié)點(diǎn)。如果targetNode為空,則默認(rèn)滾動到當(dāng)前已選中的節(jié)點(diǎn),如果沒有選中的節(jié)點(diǎn),則不滾動 * } */ scrollTo: function(jqTree, param) { //easyui tree的tree對象??梢酝ㄟ^tree.methodName(jqTree)方式調(diào)用easyui tree的方法 var tree = this; //如果node為空,則獲取當(dāng)前選中的node var targetNode = param && param.targetNode ? param.targetNode : tree.getSelected(jqTree); if (targetNode != null) { //判斷節(jié)點(diǎn)是否在可視區(qū)域 var root = tree.getRoot(jqTree); var $targetNode = $(targetNode.target); var container = param && param.treeContainer ? param.treeContainer : jqTree.parent(); var containerH = container.height(); var nodeOffsetHeight = $targetNode.offset().top - container.offset().top; if (nodeOffsetHeight > (containerH - 30)) { var scrollHeight = container.scrollTop() + nodeOffsetHeight - containerH + 30; container.scrollTop(scrollHeight); } } } }); /** * 展示搜索匹配的節(jié)點(diǎn) */ function showMatchedNode(jqTree, tree, node) { //展示所有父節(jié)點(diǎn) $(node.target).show(); $(".tree-title", node.target).addClass("tree-node-targeted"); var pNode = node; while ((pNode = tree.getParent(jqTree, pNode.target))) { $(pNode.target).show(); } //展開到該節(jié)點(diǎn) tree.expandTo(jqTree, node.target); //如果是非葉子節(jié)點(diǎn),需折疊該節(jié)點(diǎn)的所有子節(jié)點(diǎn) if (!tree.isLeaf(jqTree, node.target)) { tree.collapse(jqTree, node.target); } } /** * 判斷searchText是否與targetText匹配 * @param searchText 檢索的文本 * @param targetText 目標(biāo)文本 * @return true-檢索的文本與目標(biāo)文本匹配;否則為false. */ function isMatch(searchText, targetText) { return $.trim(targetText)!="" && targetText.indexOf(searchText)!=-1; } /** * 獲取easyui tree的所有node節(jié)點(diǎn) */ function getAllNodes(jqTree, tree) { var allNodeList = jqTree.data("allNodeList"); if (!allNodeList) { var roots = tree.getRoots(jqTree); allNodeList = getChildNodeList(jqTree, tree, roots); jqTree.data("allNodeList", allNodeList); } return allNodeList; } /** * 定義獲取easyui tree的子節(jié)點(diǎn)的遞歸算法 */ function getChildNodeList(jqTree, tree, nodes) { var childNodeList = []; if (nodes && nodes.length>0) { var node = null; for (var i=0; i<nodes.length; i++) { node = nodes[i]; childNodeList.push(node); if (!tree.isLeaf(jqTree, node.target)) { var children = tree.getChildren(jqTree, node.target); childNodeList = childNodeList.concat(getChildNodeList(jqTree, tree, children)); } } } return childNodeList; } })(jQuery);
因?yàn)閖query easyui tree原生的搜索只支持ID搜索,所以擴(kuò)展了jquery easyui tree搜索樹節(jié)點(diǎn)的方法,使其支持節(jié)點(diǎn)名稱的模糊匹配,并將不匹配的節(jié)點(diǎn)隱藏。
以上就是小編為大家?guī)淼臄U(kuò)展jquery easyui tree的搜索樹節(jié)點(diǎn)方法(推薦)全部內(nèi)容了,希望大家多多支持腳本之家~
- easyui中combotree循環(huán)獲取父節(jié)點(diǎn)至根節(jié)點(diǎn)并輸出路徑實(shí)現(xiàn)方法
- 淺談EasyUi ComBotree樹修改 父節(jié)點(diǎn)選擇的問題
- EasyUI 中combotree 默認(rèn)不能選擇父節(jié)點(diǎn)的實(shí)現(xiàn)方法
- EasyUi combotree 實(shí)現(xiàn)動態(tài)加載樹節(jié)點(diǎn)
- 淺談EasyUI中Treegrid節(jié)點(diǎn)的刪除
- Easyui Tree獲取當(dāng)前選擇節(jié)點(diǎn)的所有頂級父節(jié)點(diǎn)
相關(guān)文章
一個簡單的jQuery計(jì)算器實(shí)現(xiàn)了連續(xù)計(jì)算功能
這篇文章主要介紹了一個簡單的jQuery計(jì)算器主要實(shí)現(xiàn)了連續(xù)計(jì)算功能,需要的朋友可以參考下2014-07-07基于jquery的設(shè)置頁面文本框 只能輸入數(shù)字的實(shí)現(xiàn)代碼
之前寫過的方法有缺陷,可以輸入空格?,F(xiàn)在將空格也屏蔽了。就是在之前的代碼里加入了過濾空格的功能。2011-04-04基于jquery的使ListNav兼容中文首字拼音排序的實(shí)現(xiàn)代碼
jQuery的字母排序插件ListNav不支持中文,比較頭疼,最后找到一個取中文首字母的JS函數(shù),再配合ListNav,可以完善支持中文按首字母進(jìn)行排序。2011-07-07EasyUI學(xué)習(xí)之DataGird分頁顯示數(shù)據(jù)
這篇文章主要介紹了EasyUI學(xué)習(xí)之DataGird分頁顯示數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12使用jQuery快速解決input中placeholder值在ie中無法支持的問題
本篇文章主要介紹了使用jQuery快速解決input中placeholder值在ie中無法支持的問題。需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01jquery 實(shí)現(xiàn)表單驗(yàn)證功能代碼(簡潔)
jquery 實(shí)現(xiàn)表單驗(yàn)證功能代碼,代碼比較簡潔,需要的朋友可以參考下2012-07-07jQuery Ajax之$.get()方法和$.post()方法
load()方法通常用來從Web服務(wù)器上獲取靜態(tài)的數(shù)據(jù)文件,然而這并不能體現(xiàn)Ajax的全部價值。在項(xiàng)目中,如果需要傳遞一些參數(shù)給服務(wù)器中的頁面,那么可以使用$.get()或者$.post()方法(或者是后面要講解到的$.ajax方法)。2009-10-10