var g_nodeList = [];
var g_zTree;
//筛选需要显示的节点
function updateNodes()
{
var showNodeList = [];
for( var i = 0; i < g_nodeList.length; i++)
{
//添加要显示的节点到显示列表中
showNodeList.push(g_nodeList[i]);
//添加所有父节点
//为了防止添加的节点是第一个节点时,没有父节点
if( 0 != g_nodeList[i].level )
{
var parentNode = g_nodeList[i].getParentNode();
//查看结点是否存在,存在就不再添加该结点到框架中
while( parentNode.level != 0 )
{
//检查此节点是否已在showNodeList里面
var Result = IsNodeExist( showNodeList, parentNode );
if( false == Result)
{
showNodeList.push( parentNode );
}
parentNode = parentNode.getParentNode();
}
//添加祖先节点,并检查是否已存在该节点
if( parentNode != undefined && !(IsNodeExist(showNodeList, parentNode)) )
{
showNodeList.push( parentNode );
}
}
}
//得到所有的结点
var hideNodes = [];
AllNodeList = g_zTree.getNodes();
NodesArray = g_zTree.transformToArray(AllNodeList);
getHideNodes( NodesArray, showNodeList, hideNodes);
//隐藏节点
HideNodes(hideNodes);
SelectedSearchNode(true);
};
//得到所有要隐藏的节点
function getHideNodes( NodesArray, showNodeList, hideNodes)
{
for(var i = 0; i < NodesArray.length; i++)
{
var Result = false;
for( var j = 0; j < showNodeList.length; j++)
{
if( NodesArray[i] == showNodeList[j])
{
Result = true;
}
}
if( false == Result )
{
hideNodes.push(NodesArray[i]);
}
}
};
function HideNodes( hideNodes )
{
for( var i = 0; i < hideNodes.length; i++)
{
if( false == hideNodes[i].isHidden )
{
g_zTree.hideNode( hideNodes[i] );
}
}
//展开所有父节点
g_zTree.expandAll(true);
};
//查看节点是否已存在
function IsNodeExist(showNodeList, parentNode)
{
for( var i = 0; i < showNodeList.length; i++)
{
if( showNodeList[i].moid == parentNode.moid)
{
return true;
}
}
return false;
};
function searchNode()
{
//为了上次已选节点的颜色置为空色
SelectedSearchNode(false);
var value = $(".searchDomain")[0].value;
g_zTree = $.fn.zTree.getZTreeObj("deviceTree");
//显示所有隐藏节点
var nodes = g_zTree.getNodesByParam("isHidden", true);
g_zTree.showNodes(nodes);
var searchResult = false;
if( ("" == value) || ("搜索" == value) )
{
g_zTree.expandAll(false);
var rootnode =g_zTree.getNodeByParam("parentMoid",-1, null);
g_zTree.expandNode(rootnode, true, false, false);
MessageBox("请输入搜索内容");
return ;
}
else
{
//根据节点数据的属性搜索,获取条件完全匹配的节点数据 JSON 对象集合
g_nodeList = g_zTree.getNodesByParam("name", value);
}
//精确匹配
if( g_nodeList.length > 0 )
{
//更新节点
updateNodes();
searchResult = true;
}
else
{
//部分的部分匹配,更精确的模糊匹配
//searchResult = NormalSearch(value);
//根据需求改为不是很精确的贪婪匹配
g_nodeList = g_zTree.getNodesByParamFuzzy("name", value);
if( g_nodeList.length > 0)
{
updateNodes();
searchResult = true;
}
}
if( false == searchResult )
{
MessageBox("查找失败!");
}
}
//用户输入的内容过多时,截断字符进行匹配
function NormalSearch(value)
{
//纯数字字符的子串与字符的规则不一样,所以在此分了两种
var searchResult = false;
if( false == isNaN(value) )
{
searchResult = NumberSearch(value);
}
else
{
searchResult = StringSearch(value);
}
return searchResult;
};
function NumberSearch(value)
{
for(var i = 0; i < value.length; i++)
{
var subValue = value.substring(value,i);
if( "" == subValue)
{
return false;
}
g_nodeList = g_zTree.getNodesByParamFuzzy("name", subValue);
if( g_nodeList.length > 0)
{
updateNodes();
return true ;
}
}
return false;
}
function StringSearch(value)
{
for(var i = value.length; i > 0; i-- )
{
var subValue = value.substring(value,i);
if( "" == subValue)
{
return false;
}
g_nodeList = g_zTree.getNodesByParamFuzzy("name", subValue);
if( g_nodeList.length > 0)
{
updateNodes();
return true ;
}
}
return false;
}
function SelectedSearchNode(highlight)
{
//var zTree = $.fn.zTree.getZTreeObj("treeDemo");
for( var i=0, l=g_nodeList.length; i<l; i++)
{
g_nodeList[i].highlight = highlight;
//更新节点
g_zTree.updateNode(g_nodeList[i]);
}
}
//设置搜素到元素的样式
function setFontCss(treeId, treeNode)
{
return (!!treeNode.highlight) ? {color:"#A60000", "font-weight":"bold"} : {};
}
function focusKey(e) {
if (key.hasClass("empty")) {
key.removeClass("empty");
}
}
function blurKey(e) {
if (key.get(0).value === "") {
key.addClass("empty");
}
}
easyui Tree中对文本搜素的文章:
http://blog.csdn.net/stalwartwill/article/details/28292173
dftree搜索的实现:
//搜索域
function SearchDomaintree()
{
var DomainName = $("#searchDomain").val();
for(var i = 0;i < g_treeData.length;i++)
{
if( g_treeData[i].name.search( DomainName ) != -1 )
{
if( g_treeData[i].parentMoid == -1 )
{
var searchNode = tree.getNodeById(g_treeData[i].moid);
searchNode.open();
}
else
{
var parentNode = tree.getNodeById(g_treeData[i].parentMoid);
while( parentNode != null )
{
parentNode.open();
parentNode = parentNode._parent;
}
}
//选中搜索到的节点
var searchNode = tree.getNodeById(g_treeData[i].moid);
searchNode._select();
//在右侧显示相应的内容
g_node = searchNode;
GetMeetingInfo(searchNode.id);
}
}
}