First First Second Second ...
-
First
/** * combobox和combotree模糊查詢 * combotree 結果顯示兩級父節點(手動設置數量) * 鍵盤上下鍵選擇葉子節點 * 鍵盤迴車鍵設置文本的值 */ (function(){ //combobox可編輯,自定義模糊查詢 $.fn.combobox.defaults.editable = true; $.fn.combobox.defaults.filter = function(q, row){ var opts = $(this).combobox('options'); return row[opts.textField].indexOf(q) >= 0; }; //combotree可編輯,自定義模糊查詢 $.fn.combotree.defaults.editable = true; //選中行索引 var leafBlockIndex=0; //當前選中行索引 var nowLeafBlockIndex=-1; //combotree 組件 var comboTree=null; //葉子節點DOM Array var leafBlocks=null;; //將所有的結果葉子節點放入 DOM Array中 function getDOMArray(data){ comboTree = $(data).combotree('tree'); leafBlocks=new Array(); var leafs = comboTree.tree('getChildren'); for (var i = 0; i < leafs.length; i++) { var leaf = leafs[i]; var dis =$(leaf.target).css('display')+''; if('block' == dis){ if(comboTree.tree('isLeaf',leaf.target)){ leafBlocks.push(leaf.target); } } }; }; //------------------------------------- $.extend($.fn.combotree.defaults.keyHandler,{ up:function(e){ leafBlockIndex=nowLeafBlockIndex; leafBlockIndex--; getDOMArray(this); if(leafBlockIndex <0){ leafBlockIndex=leafBlocks.length-1; } comboTree.tree('select',leafBlocks[leafBlockIndex]); // easyui 1.3.4版開始可用 // comboTree.tree('scrollTo',leafBlocks[leafBlockIndex]); nowLeafBlockIndex=leafBlockIndex; }, down:function(e){ leafBlockIndex=nowLeafBlockIndex; leafBlockIndex++; getDOMArray(this); if(leafBlockIndex >= leafBlocks.length){ leafBlockIndex=0; } comboTree.tree('select',leafBlocks[leafBlockIndex]); // easyui 1.3.4版開始可用 // comboTree.tree('scrollTo',leafBlocks[leafBlockIndex]); nowLeafBlockIndex=leafBlockIndex; }, left: function(e){ console.log('left'); // var val = $(this).combo('getText'); // $(this).combo('setText',val+' '); }, right: function(e){ console.log('right'); // var val = $(this).combo('getText'); // console.log(val); // $(this).combo('setText',val+' '); }, enter:function(e){ var leaf =$(leafBlocks[nowLeafBlockIndex]); var value =leaf.children('span').last().text(); $(this).combo('setText',value); $(this).combo('hidePanel') }, query:function(q){ var t = $(this).combotree('tree'); var nodes = t.tree('getChildren'); for(var i=0; i<nodes.length; i++){ var node = nodes[i]; if (node.text.indexOf(q) >= 0){ $(node.target).show(); var parent=t.tree('getParent',node.target); if(parent){ $(parent.target).show(); if(parent){ parent=t.tree('getParent',parent.target); $(parent.target).show(); } } } else { $(node.target).hide(); } } var opts = $(this).combotree('options'); if (!opts.hasSetEvents){ opts.hasSetEvents = true; var onShowPanel = opts.onShowPanel; opts.onShowPanel = function(){ var nodes = t.tree('getChildren'); for(var i=0; i<nodes.length; i++){ $(nodes[i].target).show(); } onShowPanel.call(this); }; $(this).combo('options').onShowPanel = opts.onShowPanel; } } }); })(jQuery);
-
Second
(function ($) { //combotree可編輯,自定義模糊查詢 $.fn.combotree.defaults.editable = true; $.extend($.fn.combotree.defaults.keyHandler, { query: function (q) { var t = $(this).combotree('tree'); t.tree("search", q); } }); $.extend($.fn.tree.methods, { /** * 擴展easyui tree的搜索方法 * @param tree easyui tree的根DOM節點(UL節點)的jQuery對象 * @param searchText 檢索的文本 * @param this-context easyui tree的tree對象 */ search: function (jqTree, searchText) { //easyui tree的tree對象。可以通過tree.methodName(jqTree)方式調用easyui tree的方法 var tree = this; //獲取所有的樹節點 var nodeList = getAllNodes(jqTree, tree); //如果沒有搜索條件,則展示所有樹節點 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(); } //展開已選擇的節點(如果之前選擇了) var selectedNode = tree.getSelected(jqTree); if (selectedNode) { tree.expandTo(jqTree, selectedNode.target); } return; } //搜索匹配的節點並高亮顯示 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); } } //隱藏所有節點 for (var i = 0; i < nodeList.length; i++) { $(".tree-node-targeted", nodeList[i].target).removeClass("tree-node-targeted"); $(nodeList[i].target).hide(); } //摺疊所有節點 tree.collapseAll(jqTree); //展示所有匹配的節點以及父節點 for (var i = 0; i < matchedNodeList.length; i++) { showMatchedNode(jqTree, tree, matchedNodeList[i]); } } }, /** * 展示節點的子節點(子節點有可能在搜索的過程中被隱藏了) * @param node easyui tree節點 */ showChildren: function (jqTree, node) { //easyui tree的tree對象。可以通過tree.methodName(jqTree)方式調用easyui tree的方法 var tree = this; //展示子節點 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(); } } } } }, /** * 將滾動條滾動到指定的節點位置,使該節點可見(如果有滾動條才滾動,沒有滾動條就不滾動) * @param param { * treeContainer: easyui tree的容器(即存在滾動條的樹容器)。如果為null,則取easyui tree的根UL節點的父節點。 * argetNode: 將要滾動到的easyui tree節點。如果targetNode為空,則預設滾動到當前已選中的節點,如果沒有選中的節點,則不滾動 * } */ scrollTo: function (jqTree, param) { //easyui tree的tree對象。可以通過tree.methodName(jqTree)方式調用easyui tree的方法 var tree = this; //如果node為空,則獲取當前選中的node var targetNode = param && param.targetNode ? param.targetNode : tree.getSelected(jqTree); if (targetNode != null) { //判斷節點是否在可視區域 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); } } } }); /** * 展示搜索匹配的節點 */ function showMatchedNode(jqTree, tree, node) { //展示所有父節點 $(node.target).show(); $(".tree-title", node.target).addClass("tree-node-targeted"); var pNode = node; while ((pNode = tree.getParent(jqTree, pNode.target))) { $(pNode.target).show(); } //展開到該節點 tree.expandTo(jqTree, node.target); //如果是非葉子節點,需摺疊該節點的所有子節點 if (!tree.isLeaf(jqTree, node.target)) { tree.collapse(jqTree, node.target); } } /** * 判斷searchText是否與targetText匹配 * @param searchText 檢索的文本 * @param targetText 目標文本 * @return true-檢索的文本與目標文本匹配;否則為false. */ function isMatch(searchText, targetText) { return $.trim(targetText) != "" && targetText.indexOf(searchText) != -1; } /** * 獲取easyui tree的所有node節點 */ 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的子節點的遞歸演算法 */ 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);