欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

老生常谈combobox和combotree模糊查询

程序员文章站 2022-05-26 13:37:14
first /** * combobox和combotree模糊查询 * combotree 结果显示两级父节点(手动设置数量) * 键盘上下键选择...

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);

以上这篇老生常谈combobox和combotree模糊查询就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。