时间:2021-05-26
简单表格排序
可以双击编辑 自定义编辑后的 规则
可拖动列进行列替换
可推动边框进行列宽度的缩放
复制代码 代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://pare :function(n,type){
return function (a1,a2){
var convert ={
int : function(v){return parseInt(v)},
float : function(v){return parseFloat(v)},
date : function(v){return v.toString()},
string : function(v){return v.toString()}
};
!convert[type]&&(convert[type]=function(v){return v.toString()});
a1 =convert[type](a1.cells[n].innerHTML);
a2 =convert[type](a2.cells[n].innerHTML);
return a1==a2?0:a1<a2?-1:1;
};
},
edit: function(e){
var elem = this.inputtd=e.srcElement || e.target;
if(!elem.getAttribute('edit'))return;
this.input.value = elem.innerHTML;
elem.innerHTML = "";
elem.appendChild(this.input);
this.input.focus();
},
save : function(elem){
var editinfo=this.editconfig[elem.parentNode.getAttribute('edit')],status={
"[object Function]" : 'length' in editinfo.rule&&editinfo.rule(this.input.value)||false,
"[object RegExp]" : 'test' in editinfo.rule&&editinfo.rule.test(this.input.value)||false
}[Object.prototype.toString.call(editinfo.rule)],_self=this;
//如果不符合条件 修改提示信息
typeof status != "boolean"&&(editinfo.message = status);
if(status===true){
this.inputtd.innerHTML = this.input.value;
this.inputtd=null;
}else{
alert(editinfo.message);
//firefox下 直接用input.focus()不会执行 用setTimeout可以执行
setTimeout(function(){_self.input.focus()},0);
}
},
theadHover : function(e){
var elem = e.srcElement || e.target;
if(elem.nodeName.toLowerCase() ==='td'&&this.closConfig.on){
this.closConfig.totd = elem.getAttribute('clos');
!hasClass(elem,'thover')&&addClass(elem,'thover');
}
},
theadOut : function(e){
var elem = e.srcElement || e.target;
if(elem.nodeName.toLowerCase() ==='td'&&this.closConfig.on)removeClass(elem,'thover')
},
dragOrWidth : function(e){
var elem = e.srcElement || e.target,widthConfig=this.widthConfig;
//执行拖拽
if(elem.nodeName.toLowerCase()==='td'){
this.closConfig.td = elem.getAttribute('clos');
addListener(document,'mousemove',BindAsEventListener(this,this.dragMove));
addListener(document,'mouseup',Bind(this,this.dragUp));
this.closConfig.on = true;
Sys.ie?this.thead.setCapture(false):e.preventDefault();
}
//执行宽度缩放
if(elem.nodeName.toLowerCase()==='div'){
Sys.ie?(e.cancelBubble=true):e.stopPropagation();
//如果是最后一个td里面的div 不进行缩放
if(this.theadTds[this.theadTds.length-1]===elem.parentNode)return
Sys.ie?this.thead.setCapture(false):e.preventDefault();
widthConfig.x = e.clientX;
widthConfig.td = elem.parentNode;
widthConfig.nexttd = widthConfig.td.nextSibling;
while(widthConfig.nexttd.nodeName.toLowerCase()!="td"){
widthConfig.nexttd = widthConfig.nexttd.nextSibling;
};
widthConfig.tdwidth = widthConfig.td.offsetWidth;
widthConfig.nexttdwidth = widthConfig.nexttd.offsetWidth;
this.line.style.height = this.table.offsetHeight +"px";
addListener(document,'mousemove',BindAsEventListener(this,this.widthMove));
addListener(document,'mouseup',Bind(this,this.widthUp));
}
},
dragMove : function(e){
window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
setStyle(this.div,{display:"block",left:e.clientX+9+"px",top:e.clientY+20+"px"});
},
dragUp :function(){
var closConfig = this.closConfig,rows = this.table.getElementsByTagName('tr'),td,n,o,i=0,l=rows.length;
this.div.style.display = "none";
removeListener(document,'mousemove');
removeListener(document,'mouseup');
Sys.ie&&this.thead.releaseCapture();
closConfig.on = false;
if(closConfig.totd===null)return;
removeClass(this.theadTds[closConfig.totd],'thover');
//在同一列 不进行列替换
if(closConfig.td === closConfig.totd)return;
//进行列替换 如果
if(closConfig.td*1+1===closConfig.totd*1){
n = closConfig.totd;
o = closConfig.td;
}else{
n = closConfig.td;
o = closConfig.totd;
}
for(;i<l;i++){
td = rows[i].getElementsByTagName('td');
rows[i].insertBefore(td[n],td[o]);
}
//重新标识表头
for(i=0,l=this.theadTds.length;i<l;i++)
this.theadTds[i].setAttribute('clos',i);
closConfig.totd=closConfig.td=null;
},
widthMove : function(e){
window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
var widthConfig = this.widthConfig,x = e.clientX - widthConfig.x,left = e.clientX,clientX=left;
if(clientX<widthConfig.x&&widthConfig.x - clientX>widthConfig.tdwidth-this.minWidth){
left = widthConfig.x - widthConfig.tdwidth+this.minWidth;
}
if(clientX>widthConfig.x&&clientX - widthConfig.x>widthConfig.nexttdwidth-this.minWidth){
left =widthConfig.x + widthConfig.nexttdwidth-this.minWidth;
}
setStyle(this.line,{display:"block",left:left+"px"});
},
widthUp : function(){
this.line.style.display = "none";
var widthConfig = this.widthConfig,x= parseInt(this.line.style.left) - widthConfig.x;
widthConfig.nexttd.style.width = widthConfig.nexttdwidth -x -1 +'px';
widthConfig.td.style.width = widthConfig.tdwidth + x -1 +'px';
Sys.ie&&this.thead.releaseCapture();
removeListener(document,'mousemove');
removeListener(document,'mouseup');
}
});
window.onload = function(){
function checkName(val){
if(val.replace(/^\s+$/g,'')==='') return '姓名输入不能为空';
if(val.replace(/^\s+|\s+$/,'').length>10) return '姓名长度不能大于10个字符';
if(!/^[\u4e00-\u9fa5a-z]+$/i.test(val)) return '姓名只能输入中文或者是字母';
return true;
};
function checkRemark(val){
if(val.replace(/^\s+$/g,'')==='') return '备注输入不能为空';
if(val.replace(/^\s+|\s+$/,'').length>15) return '备注长度不能大于15个字符';
if(!/^[\u4e00-\u9fa5\w\s]+$/i.test(val)) return '备注只能输入中文数字下划线空格';
return true;
}
var set = [
{id:0,type:"int"},
{id:2,type:"string",edit:{rule:checkName,message:''}},
{id:3,type:"date",edit:{rule:/^\d{4}\-\d{2}\-\d{2}$/,message:"按这中格式输入日期 1985-02-30"}},
{id:4,type:"string",edit:{rule:checkRemark,message:''}}
];
new Table($("tab"),set);
}
</script>
</body>
</html>
已知BUG:
ie6下 中文不自动换行
非ie下字母和数字也不自动换行确实让人恼火
chrome浏览器下点击运行好像问题很大 拿到本地测试会比较好
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了JavaScript实现拖拽和缩放效果的具体代码,供大家参考,具体内容如下拖拽缩放*{margin:0;padding:0}#box{wid
基本的鼠标互动:拖拽(draganddropping)、排序(sorting)、选择(selecting)、缩放(resizing)各种互动效果:手风琴式的折叠
本文实例讲述了JavaScript实现表格点击排序的方法。分享给大家供大家参考。具体分析如下:这里实现基于JS的表格点击排序效果,可以根据表格内的数据大小自动按
本文实例为大家分享了js实现拖拽移动与缩放效果的具体代码,供大家参考,具体内容如下效果图如下-实现了简单的拖拽和缩放功能第一步—简单的拖拽功能//创建一个Mov
本文实例讲述了javascript实现Table排序的方法。分享给大家供大家参考。具体实现方法如下:表格排序body{margin:0;padding:0;fo