/*
|
* the param is: url,data:json[[id,title,lft,rgt,pid]] 格式,
|
*/
|
(function($,window){
|
jQuery.fn.mtree=function(options){
|
var $owner=this,
|
settings={url:'',data:'',collapse:true,folderitemcallback:function(id,object){return false;},itemcallback:function(id,object){return false;}};
|
settings=jQuery.extend(settings, $owner.settings || {}, options || {});
|
//todo:...
|
$owner.settings=settings;
|
var treeroot=jQuery('<ul class="mtree" lft="0" rgt="99999"></ul>');
|
|
$owner.on('click',':checkbox.mtree_item',function(){
|
var theurl=settings.url;
|
var $tthis=$(this);
|
if(!$tthis.is(':checked'))
|
return;
|
var id=$tthis.val();
|
if(id==null||id=='')
|
return;
|
if(theurl!=null && theurl!='')
|
{
|
var pnode=$tthis.parent('li');
|
var ulnode=pnode.children('ul');
|
if(!ulnode.is('UL'))
|
{
|
jQuery.getJSON(theurl, {id:id}, function(json, textStatus) {
|
//optional stuff to do after success
|
if(json==null||json=='')
|
return;
|
parsedata($tthis,json,settings.collapse);
|
});
|
|
}
|
}
|
var folderitemcallback=settings.folderitemcallback;
|
folderitemcallback(id);
|
});
|
$owner.live('click','a.mtree_item_a',function(){
|
var id=$(this).attr('rev')
|
var itemcallback=settings.itemcallback;
|
itemcallback(id);
|
});
|
function parsedata(treenode,data,collapse)
|
{
|
var plft=treenode.attr('lft');
|
var prgt=treenode.attr('rgt');
|
var pnode=treenode;
|
var tnode=null;
|
var valindex;
|
for (valindex in data) {
|
var val=data[valindex];
|
var lft=val[2];
|
var rgt=val[3];
|
var id=val[0];
|
var title=val[1];
|
var pid=val[4];
|
var tempnode=jQuery('<li lft="'+lft+'" rgt="'+rgt+'"></li>');
|
if(rgt-lft>1)
|
{
|
var checkobj=null;
|
if(collapse==true||collapse=='true')
|
checkobj=jQuery('<input type="checkbox" class="mtree_item" id="item-'+id+'" value="'+id+'" checked="checked"/>');
|
else
|
checkobj=jQuery('<input type="checkbox" class="mtree_item" id="item-'+id+'" value="'+id+'" />');
|
var labelobj=jQuery('<label for="item-'+id+'">'+title+'</label>');
|
checkobj.appendTo(tempnode);
|
labelobj.appendTo(tempnode);
|
}else{
|
var aobj=jQuery('<a href="javascript:void(0);" class="mtree_item_a" id="item-'+id+'" rev="'+id+'">'+title+'</a>')
|
aobj.appendTo(tempnode);
|
}
|
|
var linode=tempnode;
|
while(true)
|
{
|
plft=pnode.attr('lft');
|
prgt=pnode.attr('rgt');
|
if(lft>plft&&lft<prgt)
|
{
|
if(pnode.is("UL"))
|
{
|
pnode.append(linode);
|
}else
|
{
|
var tnode=pnode.children("UL");
|
if(!tnode.is("UL"))
|
tnode=jQuery('<ul lft="'+plft+'" rgt="'+prgt+'"></ul>');
|
tnode.append(linode);
|
pnode.append(tnode);
|
}
|
pnode=linode;
|
break;
|
}else{
|
var tppnode=pnode.parent("li");
|
if(!tppnode.is("LI"))
|
{
|
tppnode=pnode.parent("ul");
|
if(!tppnode.is("UL"))
|
break;
|
}
|
pnode=tppnode;
|
}
|
}
|
}
|
}
|
var url=$owner.settings.url;
|
var jsondata=null;
|
if (url!=null&&url!='') {
|
jQuery.getJSON('url', {}, function(json, textStatus) {
|
//optional stuff to do after success
|
jsondata=json;
|
});
|
}
|
if($owner.settings.data!=null&&$owner.settings.data!='')
|
{
|
try{
|
jsondata=$owner.settings.data;
|
}catch(e){alert(e);}
|
}
|
if(jsondata==null||jsondata=='')
|
return null;
|
parsedata(treeroot,jsondata,$owner.settings.collapse);
|
$owner.append(treeroot);
|
return {abort: function () {}};
|
}
|
})(jQuery,window);
|