dhDataGrid Ver1.0.0发布

在原来做的一些项目里都有用到MS OFFICE的LISTVIEW控件,感觉都还不错,但由于有些机器没有安装MS OFFICE故无法正常使用该控件,最近特别抽出时间模拟了一个功能基本实现的DataGrid - dhDataGrid Ver1.0.0

dhDataGrid Ver1.0.0的组成:

1、数据显示 dhDataGrid.htm

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Author" content="dh20156">
<title>dhDataGrid Ver1.0.0</title>
<link rel="stylesheet" type="text/css" href="dhDataGrid.css">
</head>

<body onselectstart="return false;">
<p>dhDataGrid Ver1.0.0</p>
<p>Thank JK,Meizz and other friends.</p>

<div id="dhDataGridPanel" onkeyDown="return false;">
<div id="dhDataGridLine" style="position:absolute;left:0;top:0;height:expression(document.getElementById('dhDataGridTable').offsetHeight);color:#ff0000;z-index:99;visibility:hidden;font-size:1px;width:1px;background-color:#808080;"></div>
<table id="dhDataGridTable" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="zero" width="20">&nbsp;</td>
<td class="titleTD" width="150">
test Column 1
</td>
<td class="titleTD" width="150">
ddd
</td>
<td class="titleTD" width="150">
ddd
</td>
<td class="lastCol">&nbsp;</td>
</tr>
<tr>
<td width="20" class="firstCol">&nbsp;</td>
<td class="dataTD">
Test data 1 aaaa fdsa fda fdsa fdsa fdsa fdsa fdsa fda fdsa fdsafdsa fdsa fdsa a
</td>
<td class="dataTD">
test data 1 bbbb
</td>
<td class="dataTD">
test data 1 dddd
</td>
<td class="lastDataTD">&nbsp;</td>
</tr>
<tr>
<td width="20" class="firstCol">&nbsp;</td>
<td class="dataTD">
test data 2 aaaa fdsa fda fdsa fdsa fdsa fdsa fdsa fda fdsa fdsafdsa fdsa fdsa a
</td>
<td class="dataTD">
Test data 2 bbbb
</td>
<td class="dataTD">
test data 2 dddd
</td>
<td class="lastDataTD">&nbsp;</td>
</tr>
<tr>
<td width="20" class="firstCol">&nbsp;</td>
<td class="dataTD">
test data 3 aaaa fdsa fda fdsa fdsa fdsa fdsa fdsa fda fdsa fdsafdsa fdsa fdsa a
</td>
<td class="dataTD">
test data 3 bbbb
</td>
<td class="dataTD">
test data 3 dddd
</td>
<td class="lastDataTD">&nbsp;</td>
</tr>
<tr>
<td width="20" class="firstCol">&nbsp;</td>
<td class="dataTD">
test data 4 aaaa fdsa fda fdsa fdsa fdsa fdsa fdsa fda fdsa fdsafdsa fdsa fdsa a
</td>
<td class="dataTD">
test data 4 bbbb
</td>
<td class="dataTD">
test data 4 dddd
</td>
<td class="lastDataTD">&nbsp;</td>
</tr>
<tr>
<td width="20" class="firstCol">&nbsp;</td>
<td class="dataTD">
test data 5 aaaa fdsa fda fdsa fdsa fdsa fdsa fdsa fda fdsa fdsafdsa fdsa fdsa a
</td>
<td class="dataTD">
test data 5 bbbb
</td>
<td class="dataTD">
test data 5 dddd
</td>
<td class="lastDataTD">&nbsp;</td>
</tr>
<tr>
<td width="20" class="firstCol">&nbsp;</td>
<td class="dataTD">
test data 6 aaaa fdsa fda fdsa fdsa fdsa fdsa fdsa fda fdsa fdsafdsa fdsa fdsa a
</td>
<td class="dataTD">
test data 6 bbbb
</td>
<td class="dataTD">
test data 6 dddd
</td>
<td class="lastDataTD">&nbsp;</td>
</tr>
<tr>
<td width="20" class="firstCol">&nbsp;</td>
<td class="dataTD">
test data 7 aaaa fdsa fda fdsa fdsa fdsa fdsa fdsa fda fdsa fdsafdsa fdsa fdsa a
</td>
<td class="dataTD">
test data 7 bbbb
</td>
<td class="dataTD">
test data 7 dddd
</td>
<td class="lastDataTD">&nbsp;</td>
</tr>
<tr>
<td width="20" class="firstCol">&nbsp;</td>
<td class="dataTD">
test data 8 aaaa fdsa fda fdsa fdsa fdsa fdsa fdsa fda fdsa fdsafdsa fdsa fdsa a
</td>
<td class="dataTD">
test data 8 bbbb
</td>
<td class="dataTD">
test data 8 dddd
</td>
<td class="lastDataTD">&nbsp;</td>
</tr>
<tr>
<td width="20" class="firstCol">&nbsp;</td>
<td class="dataTD">
test data 9 aaaa fdsa fda fdsa fdsa fdsa fdsa fdsa fda fdsa fdsafdsa fdsa fdsa a
</td>
<td class="dataTD">
test data 9 bbbb
</td>
<td class="dataTD">
test data 9 dddd
</td>
<td class="lastDataTD">&nbsp;</td>
</tr>
<tr>
<td width="20" class="firstCol">&nbsp;</td>
<td class="dataTD">
test data 10 aaaa fdsa fda fdsa fdsa fdsa fdsa fdsa fda fdsa fdsafdsa fdsa fdsa a
</td>
<td class="dataTD">
test data 10 bbbb
</td>
<td class="dataTD">
test data 10 dddd
</td>
<td class="lastDataTD">&nbsp;</td>
</tr>
<tr>
<td width="20" class="firstCol">&nbsp;</td>
<td class="dataTD">
test data 11 aaaa fdsa fda fdsa fdsa fdsa fdsa fdsa fda fdsa fdsafdsa fdsa fdsa a
</td>
<td class="dataTD">
test data 11 bbbb
</td>
<td class="dataTD">
test data 11 dddd
</td>
<td class="lastDataTD">&nbsp;</td>
</tr>
<tr>
<td width="20" class="firstCol">&nbsp;</td>
<td class="dataTD">
test data 12 aaaa fdsa fda fdsa fdsa fdsa fdsa fdsa fda fdsa fdsafdsa fdsa fdsa a
</td>
<td class="dataTD">
test data 12 bbbb
</td>
<td class="dataTD">
test data 12 dddd
</td>
<td class="lastDataTD">&nbsp;</td>
</tr>
</table>
</div>
</body>
</html>

2、样式控制 dhDataGrid.css

td {height:16px;font-size:12px;font-family:arial;cursor:default;padding:0px 2px 0px 2px;}
#dhDataGridPanel {behavior:url(dhDataGridPanel.htc);border:2px inset;width:500px;height:180px;overflow:auto;z-index:100;}
#dhDataGridTable {width:100%;table-layout:fixed;}
.titleTD {behavior:url(dhDataGridTitle.htc);font-weight:bold;height:17px;background-color:buttonface;border-top:1px solid #fff;border-right:1px solid #404040;border-bottom:1px solid #404040;border-left:1px solid #fff;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;position:relative;top:expression(document.getElementById("dhDataGridPanel").scrollTop);}
.dataTD {border-right:1px solid buttonface;border-bottom:1px solid buttonface;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;}
.zero {width:17px;background:buttonface;border-top:1px solid #fff;border-right:1px solid #404040;border-bottom:1px solid #404040;border-left:1px solid #fff;position:relative;top:expression(document.getElementById("dhDataGridPanel").scrollTop);left:expression(document.getElementById("dhDataGridPanel").scrollLeft);z-index:2;}
.firstCol {width:17px;text-align:center;color:#000;background:buttonface;border-top:1px solid #fff;border-right:1px solid #404040;border-bottom:1px solid #404040;border-left:1px solid #fff;position:relative;left:expression(document.getElementById("dhDataGridPanel").scrollLeft);}
.lastCol {background-color:buttonface;border-top:1px solid #fff;border-bottom:1px solid #404040;border-left:1px solid #fff;overflow:hidden;text-overflow:clip;white-space:nowrap;word-break:keep-all;position:relative;top:expression(document.getElementById("dhDataGridPanel").scrollTop);}
.lastDataTD {border-bottom:1px solid buttonface;padding:1px;overflow:hidden;text-overflow:clip;white-space:nowrap;}

3、功能实现 dhDataGridPanel.htc

<attach event="onmousedown" onevent="down()"/>
<attach event="onkeydown" onevent="keyDown()"/>
<script language="Javascript">
//author:dh20156;
var rl = this.children[1].rows.length;
var selectedRow = null;

//鼠标点击行事件
function down(){
var obj = event.srcElement.parentElement;
if(obj.tagName=="TR"){
if(obj.rowIndex!=0){
if(selectedRow!=null){
selectedRow.cells[0].style.background = "buttonface";
selectedRow.style.backgroundColor = "";
selectedRow.style.color = "";
}
obj.cells[0].style.background = "buttonface url('selected.gif') center center no-repeat";
obj.style.backgroundColor = "highlight";
obj.style.color = "highlighttext";
selectedRow = obj;
}
}
}

//键盘Up & Down事件
function keyDown(){
var kc = event.keyCode;
switch(kc){
case 38://Up;
if(selectedRow!=null && selectedRow.rowIndex>1){
selectedRow.cells[0].style.background = "buttonface";
selectedRow.style.backgroundColor = "";
selectedRow.style.color = "";
this.children[1].rows[selectedRow.rowIndex-1].cells[0].style.background = "buttonface url('selected.gif') center center no-repeat";
this.children[1].rows[selectedRow.rowIndex-1].style.backgroundColor = "highlight";
this.children[1].rows[selectedRow.rowIndex-1].style.color = "highlighttext";
this.children[1].parentElement.scrollTop=selectedRow.offsetHeight*(selectedRow.rowIndex-1)-selectedRow.offsetHeight;
selectedRow = this.children[1].rows[selectedRow.rowIndex-1];
};
break;
case 40://Down;
if(selectedRow!=null && selectedRow.rowIndex<rl-1){
selectedRow.cells[0].style.background = "buttonface";
selectedRow.style.backgroundColor = "";
selectedRow.style.color = "";
this.children[1].rows[selectedRow.rowIndex+1].cells[0].style.background = "buttonface url('selected.gif') center center no-repeat";
this.children[1].rows[selectedRow.rowIndex+1].style.backgroundColor = "highlight";
this.children[1].rows[selectedRow.rowIndex+1].style.color = "highlighttext";
this.children[1].parentElement.scrollTop=selectedRow.offsetHeight*(selectedRow.rowIndex+1)-selectedRow.offsetHeight;
selectedRow = this.children[1].rows[selectedRow.rowIndex+1];
};
break;
}
}
</script>

4、功能实现 dhDataGridTitle.htc

<attach event="onmousemove" onevent="move();"/>
<attach event="onmousedown" onevent="down();"/>
<attach event="onmouseup" onevent="up();"/>
<attach event="onmouseout" onevent="out();"/>
<attach event="ondblclick" onevent="reColWidth();"/>
<script language="Javascript">
//author:dh20156;

//鼠标拖动列头分隔条事件
function move(){
if(!this.mouseDownX){
if((event.offsetX>event.srcElement.offsetWidth-6 && event.offsetX<event.srcElement.offsetWidth)){
this.style.cursor='col-resize';
}else{
this.style.cursor='default';
}
return false;
}else{
this.style.cursor='col-resize';
this.newWidth = this.TdW*1+event.clientX*1-this.mouseDownX;
dhDataGridLine.style.left=this.offsetLeft+this.newWidth;
}
}

//鼠标点击列头事件
function down(flag){
if((event.offsetX>event.srcElement.offsetWidth-6 && event.offsetX<event.srcElement.offsetWidth) || flag=="resize"){
this.mouseDownX = event.clientX;
this.TdW = this.offsetWidth;
dhDataGridLine.style.visibility='visible';
dhDataGridLine.style.left=this.offsetLeft+this.TdW-1;
this.setCapture();
getMinWidth();
getMaxWidth();
}else{
this.execSort = 1;
if(this.sort==null){
this.sort = 0;
}
this.sort==1?this.sort=0:this.sort=1;
this.style.borderRight='1px solid #ffffff';
this.style.borderBottom='1px solid #ffffff';
this.style.borderLeft='1px solid #404040';
this.style.borderTop='1px solid #404040';
this.style.position='relative';
this.style.left='1px';
}
}

//鼠标点击列头后鼠标按钮弹起事件
function up(){
out();
if(this.newWidth>20){
this.style.width = this.newWidth;
}
if(this.execSort==1){
if(this.sort==0){
Asc();
}else{
Desc();
}
}
this.newWidth = 0;
this.execSort=0;
dhDataGridLine.style.visibility='hidden';
this.releaseCapture();
this.mouseDownX = 0;
}

//鼠标离开列头事件
function out(){
this.style.borderRight='1px solid #404040';
this.style.borderBottom='1px solid #404040';
this.style.borderLeft='1px solid #ffffff';
this.style.borderTop='1px solid #ffffff';
this.style.position='relative';
this.style.left='0px';
}

//获得该列最大文本宽度
function getMaxWidth(){
if(this.maxWidth==null){
var ci = this.cellIndex;
var rl = this.parentElement.parentElement.rows.length;
for(var i=0;i<rl;i++){
if(this.maxWidth==null || this.maxWidth<this.parentElement.parentElement.rows[i].cells[ci].scrollWidth){
this.maxWidth=this.parentElement.parentElement.rows[i].cells[ci].scrollWidth;
}
}
}
}

//获得初始列宽
function getMinWidth(){
if(this.minWidth==null){
this.minWidth = this.scrollWidth;
}
}

//改变列宽
function reColWidth(){
if((event.offsetX>event.srcElement.offsetWidth-6 && event.offsetX<event.srcElement.offsetWidth)){
if(this.offsetWidth==this.maxWidth){
this.style.width = this.minWidth;
}else{
this.style.width = this.maxWidth;
}
down("resize");
up();
}
}

//A-->Z 排序
function Asc(){
var rl = this.parentElement.parentElement.rows.length;
var ci = this.cellIndex;
for(var i=1;i<rl-1;i++){
for(var j=i+1;j<rl;j++){
var u = this.parentElement.parentElement.rows[i].cells[ci].innerText.toLowerCase();
var d = this.parentElement.parentElement.rows[j].cells[ci].innerText.toLowerCase();
if(u>d){
this.parentElement.parentElement.moveRow(j,i);
}
}
}
for(var c=1;c<this.parentElement.cells.length-1;c++){
this.parentElement.cells[c].style.background = "buttonface";
}
this.style.background = "buttonface url('asc.gif') right center no-repeat";
}

//Z-->A 排序
function Desc(){
var rl = this.parentElement.parentElement.rows.length;
var ci = this.cellIndex;
for(var i=1;i<rl-1;i++){
for(var j=i+1;j<rl;j++){
var u = this.parentElement.parentElement.rows[i].cells[ci].innerText.toLowerCase();
var d = this.parentElement.parentElement.rows[j].cells[ci].innerText.toLowerCase();
if(u<d){
this.parentElement.parentElement.moveRow(j,i);
}
}
}
for(var c=1;c<this.parentElement.cells.length-1;c++){
this.parentElement.cells[c].style.background = "buttonface";
}
this.style.background = "buttonface url('desc.gif') right center no-repeat";
}
</script>

5、小图标:selected.gif 、asc.gif 、desc.gif

6、实现效果:

7、测试链接:

http://www.v-ec.com/dh20156/code/dhdatagrid/dg_default.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值