部分文档来源说明:【EasyUI】combotree和combobox模糊查询
/* easyuiTreeLoad:start */
var pidSet = new Set();
function initSet(data){
for ( var i = 0; i < data.length; i++) {
pidSet.add(data[i].pid);
}
}
function easyuiTreeLoad(combotreeId, url, rootId, queryParams) {
$("#" + combotreeId).combotree({
lines : true,
url : url,
queryParams:queryParams,
// 1、过滤数据显示
valueField : 'id',
textField : 'text',
editable : false,
cascadeCheck:false,
multiple:false,
collapse:true,
loadFilter : function(data) {
initSet(data);
return getDepartmentTree(rootId, data);
},
onLoadSuccess:function(node1,data){
/* combotree:仅展示到第2层:start*/
var ds=$('#unitSel').combotree('tree').tree('find', data[0].id);
$('#unitSel').combotree('tree').tree('collapseAll');
$('#unitSel').combotree('tree').tree('expand', ds.target);
/* combotree:仅展示到第2层:start*/
}
});
}
function getDepartmentTree(rootId, data) {
var departmentTree = '';
for ( var i = 0; i < data.length; i++) {
var departmentNode = null;
if (data[i].pid == rootId) {
departmentNode = "{\"id\":\"" + data[i].id + "\",\"text\":\"" + data[i].name +"\"";
// 调用findChild方法,开始遍历整个树,寻找当前节点的子节点。
if(pidSet.has(data[i].id)){
var child = findChild(data[i].id, data);
if (child != null) {
departmentNode += child;
}
}
departmentNode += "}";
if(departmentTree != ''){
departmentTree += "," +departmentNode;
}else{
departmentTree = departmentNode;
}
}
}
departmentTree = "[" + departmentTree + "]";
return JSON.parse(departmentTree);
}
function findChild(rootId, data) {
var departmentTree = '';
for ( var i = 0; i < data.length; i++) {
var departmentNode = null;
if (data[i].pid == rootId) {
departmentNode = "{\"id\":\"" + data[i].id + "\",\"text\":\"" + data[i].name +"\"";
// 调用findChild方法,开始遍历整个树,寻找当前节点的子节点。
if(pidSet.has(data[i].id)){
var child = findChild(data[i].id, data);
if (child != null) {
departmentNode += child;
}
}
departmentNode += "}";
if(departmentTree != ''){
departmentTree += "," +departmentNode;
}else{
departmentTree = departmentNode;
}
}
}
departmentTree = ",\"children\":[" + departmentTree + "]";
return departmentTree;
}
/* easyuiTreeLoad:end */
/* combotree:支持模糊查询:start*/
(function(){
$.fn.combotree.defaults.editable = true;
$.extend($.fn.combotree.defaults.keyHandler,{
up:function(){
console.log('up');
},
down:function(){
console.log('down');
},
enter:function(){
console.log('enter');
},
query:function(q){
$(this).combotree('tree').tree('expandAll'); //由于模糊查询不到隐藏的结构,故先展示全部结构
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();
} 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);
/* combotree:支持模糊查询:end*/
<select id="unitSel" name="unitSel" style="width:200px" class="easyui-combotree"></select>
easyuiTreeLoad('unitSel', rootPath + '/zzjg/getZzjgTreeBySession.do','root',null);
<select id="getZzjgTreeBySession" resultMap="zzjgTreeMap">
select id, zzjgjc, nvl(parent_id, 'root') parent_id, 0 as checked, shortdm
from t_sys_zzjg
<if test="_parameter != null and _parameter !=''">
connect by prior zzjgdm = parent_id start with zzjgdm = #{_parameter}
</if>
order by id, show_index asc
</select>