一段自以为写得不错的JS代码
// expand event:
// mouseClick plus/minus
// rowSelected row
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
ctit.treeNode = Class.create(
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
initialize:function(element, options)
{
this.element = $(element);
this.options = options;
this.eventMouseClick = this.options.mouseClick.bindAsEventListener(this);
this.eventRowSelected = this.rowSelected.bindAsEventListener(this);
this._onRequest = false;
Event.observe(this.element, "click", this.eventMouseClick);
Event.observe(this.options.row, "dblclick", this.eventMouseClick);
Event.observe(this.options.row, "click", this.eventRowSelected);
},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
destroy:function()
{
Event.stopObserving(this.element, "click", this.eventMouseClick);
Event.stopObserving(this.options.row, "dblclick", this.eventMouseClick);
Event.stopObserving(this.options.row, "click", this.eventRowSelected);
},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
rowSelected:function(event)
{
var cs = this.options.table.getElementsByClassName(this.options.currentSelectedClass);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(var i=0; i<cs.length; i++)
{
Element.removeClassName(cs[i], this.options.currentSelectedClass);
cs[i].style.backgroundColor = '#fff';
}
var tds = this.options.row.cells;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(var i=0; i<tds.length; i++)
{
Element.addClassName(tds[i], this.options.currentSelectedClass);
tds[i].style.backgroundColor = '#d7e2e8';
}
},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
ajaxRequest:function(url, param, onSuccessCallback)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(!this._beginRequest())
{
return;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(this._changePlusMinusIcon())
{
param.ajax = true;
pThis = this;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Ajax.Request(url,
{
method: 'post',
postBody: $H(param).toQueryString(),
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
onSuccess: function(o)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(typeof(onSuccessCallback) === 'function')
{
var ls = o.responseText.evalJSON();
onSuccessCallback(ls, pThis.options.tBody, pThis.options.row);
}
},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
onFailure: function(o)
{
alert("Connect to server failure.");
},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
onComplete: function(o)
{
pThis._endRequest();
},
evalScripts:false
});
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else
{
this._endRequest();
}
},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
_changePlusMinusIcon:function()
{
var imgs = this.element.getElementsByTagName('img');
if(!imgs) return false;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(imgs[0].src.indexOf('inactive_img.gif')>0)
{
var src = imgs[0].src.replace(/\binactive_img.gif\b/, 'active_img.gif');
imgs[0].src = src;
imgs[0].alt = 'Open';
this._removeItWithChildren();
return false;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else
{
var src = imgs[0].src.replace(/\bactive_img.gif\b/, 'inactive_img.gif');
imgs[0].src = src;
imgs[0].alt = 'Close';
return true;
}
},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
_beginRequest:function()
{
if(this._onRequest) return false;
this._onRequest = true;
return true;
},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
_endRequest:function()
{
this._onRequest = false;
},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
_removeItWithChildren:function()
{
var row = this.options.row;
var level = this._getLevel(row);
var tBody = this.options.tBody;
var i=0;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while(true)
{
var nextRow = row.nextSibling;
if(isNull(nextRow)) break;
var pLevel = this._getLevel(nextRow);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(pLevel>level)
{
tBody.removeChild(nextRow);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else
{
break;
}
}
},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
_getLevel:function(row)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(!isNull(row))
{
var o = row.cells[this.options.treeNodeIndex];
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(o)
{
var mtch = o.className.match(/\bcontent_indent([0-9]+)\b/);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(mtch && mtch[1])
{
return parseInt(mtch[1]);
}
}
}
return 0;
}
});
原文地址:https://www.cnblogs.com/afxcn/p/1089572.html