日期:2014-05-16  浏览次数:20631 次

MzTreeView能通过text或者data上挂载的数据聚焦某节点吗?
MzTreeView能通过text或者data上挂载的数据聚焦某节点吗???

focus方法好像只能用id,但是我获取到的数据只有节点的text和data,无法得到节点的id值。



------解决方案--------------------
可以,前段时间也有位朋友问到过这样的问题。

首先扩展一个属性 this.txt = [];

然后给this.txt赋值:
MzTreeView.prototype.dataFormat = function()
{
var a = new Array(),b = new Array(),d = this.nodes;
for (var i in d){
a[a.length] = i;
b[b.length] = d[i].match(/[^:]+(?=;)/)+','+i.replace(/(?:\d*_)*?(\d+)$/,'$1');
}
this.names = a.join(this._d + this._d);
this.txt = b.slice(0);
this.totalNode = a.length; a = null; b = null;
};

最后修改getPath方法:
MzTreeView.prototype.getPath= function(sourceId)
{
Array.prototype.indexOf = function(item)
{
for(var i=0; i<this.length; i++)
{
if(this[i]==item) return i;
}
return -1;
};
Array.prototype.indexStr = function(str){
for(var i=0; i<this.length; i++){
var aTmp = this[i].split(',');
if(aTmp[0]==str) return aTmp[1];
}
return -1;
};
var _d = this._d, d = this.divider;
var A = new Array();
var aTxt = this.txt;
var id = (isNaN(sourceId))?aTxt.indexStr(sourceId):sourceId; A[0] = id;

while(id!="0" && id!="")
{
var str = "(^|"+_d+")([^"+_d+d+"]+"+d+ id +")("+_d+"|$)";
if (new RegExp(str).test(this.names))
{
id = RegExp.$2.substring(0, RegExp.$2.indexOf(d));
if(A.indexOf(id)>-1) break;
A[A.length] = id;
}
else break;
}
return A.reverse();
};
------解决方案--------------------
JScript code

//example focusTree("0_1_2")
//0_1_2是一个完整的路径
function focusTree(arrSourceIndex)
{
    var arr = arrSourceIndex.split("_");
    for(var i=0;i<arr.length-1;i++)
        expandNode(arr[i]+"_"+arr[i+1]);
}
function expandNode(sourceIndex)
{
    try
    {
        var expandId =     tree.getAttribute(tree.nodes[sourceIndex],"index");        
        tree.focusClientNode(expandId);    
        if (!tree.node[expandId].isExpand)
            tree.newExpand(expandId);
    }
    catch(e){}    
}