Java现实自定义表格标签(支持排序,分页,汇总,滚动,多行列)

package com.ts.taglib.html;

import java.lang.reflect.Field;

import javax.servlet.jsp.tagext.BodyTagSupport;
/**
 * 事件处理
 * @author 陈双
 * @date 2012-09-22
 * @mail [email protected]
 */
public abstract class BaseHandlerTag extends BodyTagSupport{
    private static final long serialVersionUID = 715968190636480266L;
    private String onclick;
    private String ondblclick;
    private String onmouseover;
    private String onmouseout;
    private String onmousedown;
    private String onmouseup;
    private String onmousemove;
    private String onkeydown;
    private String onkeyup;
    private String onkeypress;
    private String onfocus;
    private String onblur;
    private String onchange;
    private String onselect;
    private String styleId;
    private String style;
    private String styleClass;
    private boolean readonly;
    private boolean disabled;
    public BaseHandlerTag()
    {
    	super();
    	init();
    }
    public void init()
    {
    	οnclick=null;
    	οndblclick=null;
    	οnmοuseοver=null;
    	οnmοuseοut=null;
    	οnmοusedοwn=null;
    	οnmοuseup=null;
    	οnmοusemοve=null;
    	οnkeydοwn=null;
    	οnkeyup=null;
    	οnkeypress=null;
    	οnfοcus=null;
    	οnblur=null;
    	οnchange=null;
    	οnselect=null;
    	readonly=false;
    	disabled=false;
    }
	public String getOnclick() {
		return onclick;
	}
	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}
	public String getOndblclick() {
		return ondblclick;
	}
	public void setOndblclick(String ondblclick) {
		this.ondblclick = ondblclick;
	}
	public String getOnmouseover() {
		return onmouseover;
	}
	public void setOnmouseover(String onmouseover) {
		this.onmouseover = onmouseover;
	}
	public String getOnmouseout() {
		return onmouseout;
	}
	public void setOnmouseout(String onmouseout) {
		this.onmouseout = onmouseout;
	}
	public String getOnmousedown() {
		return onmousedown;
	}
	public void setOnmousedown(String onmousedown) {
		this.onmousedown = onmousedown;
	}
	public String getOnmouseup() {
		return onmouseup;
	}
	public void setOnmouseup(String onmouseup) {
		this.onmouseup = onmouseup;
	}
	public String getOnmousemove() {
		return onmousemove;
	}
	public void setOnmousemove(String onmousemove) {
		this.onmousemove = onmousemove;
	}
	public String getOnkeydown() {
		return onkeydown;
	}
	public void setOnkeydown(String onkeydown) {
		this.onkeydown = onkeydown;
	}
	public String getOnkeyup() {
		return onkeyup;
	}
	public void setOnkeyup(String onkeyup) {
		this.onkeyup = onkeyup;
	}
	public String getOnkeypress() {
		return onkeypress;
	}
	public void setOnkeypress(String onkeypress) {
		this.onkeypress = onkeypress;
	}
	public String getOnfocus() {
		return onfocus;
	}
	public void setOnfocus(String onfocus) {
		this.onfocus = onfocus;
	}
	public String getOnblur() {
		return onblur;
	}
	public void setOnblur(String onblur) {
		this.onblur = onblur;
	}
	public String getOnchange() {
		return onchange;
	}
	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}
	public String getOnselect() {
		return onselect;
	}
	public void setOnselect(String onselect) {
		this.onselect = onselect;
	}
	public String getStyleId() {
		return styleId;
	}
	public void setStyleId(String styleId) {
		this.styleId = styleId;
	}
	public String getStyle() {
		return style;
	}
	public void setStyle(String style) {
		this.style = style;
	}
	public String getStyleClass() {
		return styleClass;
	}
	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}
	public boolean isReadonly() {
		return readonly;
	}
	public void setReadonly(boolean readonly) {
		this.readonly = readonly;
	}
	public boolean isDisabled() {
		return disabled;
	}
	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}
	public String prepareStyles()
	{
		StringBuffer handlers=new StringBuffer();
		prepareAttribute(handlers,"id",getStyleId());
		prepareAttribute(handlers,"style",getStyle());
		prepareAttribute(handlers,"class",getStyleClass());
		return handlers.toString();
	}
	public String prepareEvents()
	{
		StringBuffer handlers=new StringBuffer();
		prepareMouseEvents(handlers);
		prepareKeyEvents(handlers);
		prepareFocusEvents(handlers);
		prepareTextEvents(handlers);
		return handlers.toString();
	}
	public void prepareMouseEvents(StringBuffer handlers)
	{
		prepareAttribute(handlers,"onclick",getOnclick());
		prepareAttribute(handlers,"ondblclick",getOndblclick());
		prepareAttribute(handlers,"onmouseover",getOnmouseover());
		prepareAttribute(handlers,"onmouseout",getOnmouseout());
		prepareAttribute(handlers,"onmousedown",getOnmousedown());
		prepareAttribute(handlers,"onmouseup",getOnmouseup());
		prepareAttribute(handlers,"onmousemove",getOnmousemove());
	}
	public void prepareKeyEvents(StringBuffer handlers)
	{
		prepareAttribute(handlers,"onkeydown",getOnkeydown());
		prepareAttribute(handlers,"onkeyup",getOnkeyup());
		prepareAttribute(handlers,"onkeypress",getOnkeypress());
	}
	public void prepareFocusEvents(StringBuffer handlers)
	{
		prepareAttribute(handlers,"onfocus",getOnfocus());
		prepareAttribute(handlers,"onblur",getOnblur());
		if(isReadonly())
		{
			handlers.append(" readonly=\"readonly\"");
		}
		if(isDisabled())
		{
			handlers.append(" disabled=\"disabled\"");
		}
	}
	public void prepareTextEvents(StringBuffer handlers)
	{
		prepareAttribute(handlers,"onselect",getOnselect());
		prepareAttribute(handlers,"onchange",getOnchange());
	}
	public void prepareAttribute(StringBuffer handler,String property,Object value)
	{
		if(handler!=null&&property!=null&&value!=null)
		{
			handler.append(" ");
			handler.append(property);
			handler.append("=\"");
			handler.append(value);
			handler.append("\"");
		}
	}
	/**
	 * 取值
	 * @param name
	 * @return
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 */
	public Object findValue(String name) throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException
	{
		String[] elements=name.split("\\.");
		if(elements!=null&&elements.length>0)
		{
			Object bean=pageContext.getAttribute(elements[0],2);
			if(elements.length>2)
			{
				for(int i=1;i\n");
		handlers.append("\n");
		handlers.append("\n\n\n\n
共"); Object total=pageContext.getAttribute("total", 2); if(total==null) { total=0; } handlers.append(total); handlers.append("页  当前显示第"); Object current=pageContext.getAttribute("current",2); if(current==null) { current=0; } handlers.append(current); handlers.append("页  上页 下页  
\n"); handlers.append("
\n"); return handlers.toString(); } /** * 构建所有的列 * @param list * @return * @throws JspException */ private String getColumns(List list) throws JspException { StringBuffer columns=new StringBuffer(); if(list!=null&&list.size()>0) { columns.append("function getColumns_grid_"); columns.append(name); columns.append("()\n{\n"); columns.append("var columns=new Array();\n"); for(int i=0;i转成javascript中List形式 * @param list列表 * @return */ public String getRows(List list) { StringBuffer handlers=new StringBuffer("\n function getRows_grid_"); handlers.append(getName()); handlers.append("()\n{\n"); SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); handlers.append("var rows=new List();\n"); if(list!=null&&list.size()>0) { for(int i=0;i 

package com.ts.taglib.html;

import java.util.List;

import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport;

import com.ts.taglib.data.Column; /**  * 构建列控件  * @author 陈双  * @date 2012-09-23  * @mail [email protected]  */ public class ColumnTag extends TagSupport {

 private static final long serialVersionUID = 5477014869077280329L;     private String name;//名称     private String label;//标签     private String type;//类型     private String width;//宽度     private String height;//高度     private String size;//字符个数  private String data;//如果是combox时,选项取数来源  private String colspan;//跨列  private String ondblclick;//双击事件,只是针对参照列  private String drillEvent;//数据钻取时调用的函数  private boolean hidden;//是否隐藏  private boolean disabled;//是否可用  private boolean sum;//是否汇总  public ColumnTag()     {      super();      name=null;      label=null;      type=null;      width=null;      height=null;      size="14";      data=null;      colspan=null;      οndblclick=null;      drillEvent=null;      hidden=false;      disabled=false;      sum=false;     }  public String getName() {   return name;  }  public void setName(String name) {   this.name = name;  }  public String getLabel() {   return label;  }  public void setLabel(String label) {   this.label = label;  }  public String getType() {   return type;  }  public void setType(String type) {   this.type = type;  }  public String getWidth() {   return width;  }

 public void setWidth(String width) {   this.width = width;  }

 public String getHeight() {   return height;  }

 public void setHeight(String height) {   this.height = height;  }     public String getSize() {   return size;  }  public void setSize(String size) {   this.size = size;  }  public String getData() {   return data;  }  public void setData(String data) {   this.data = data;  }  public String getColspan() {   return colspan;  }

 public void setColspan(String colspan) {   this.colspan = colspan;  }  public String getOndblclick() {   return ondblclick;  }  public void setOndblclick(String ondblclick) {   this.ondblclick = ondblclick;  }  public String getDrillEvent() {   return drillEvent;  }  public void setDrillEvent(String drillEvent) {   this.drillEvent = drillEvent;  }  public boolean isHidden() {   return hidden;  }  public void setHidden(boolean hidden) {   this.hidden = hidden;  }  public boolean isDisabled() {   return disabled;  }  public void setDisabled(boolean disabled) {   this.disabled = disabled;  }  public boolean isSum() {   return sum;  }

 public void setSum(boolean sum) {   this.sum = sum;  }  public int doEndTag() throws JspException {   Column column=new Column();   column.setName(name);   column.setLabel(label);   column.setType(type);   column.setWidth(width);   column.setHeight(height);   column.setSize(size);   column.setData(data);   column.setColspan(colspan);   column.setOndblclick(ondblclick);   column.setDrillEvent(drillEvent);   column.setHidden(hidden);   column.setDisabled(disabled);   column.setSum(sum);   List list=(List)pageContext.getAttribute("columnHeader", 2);   list.add(column);   return 6;  }  public int doStartTag() throws JspException {      return 0;  }  public void release() {

  super.release();   name=null;      label=null;      type=null;      width=null;      height=null;      data=null;      colspan=null;      size=null;      οndblclick=null;      drillEvent=null;      hidden=false;      disabled=false;      sum=false;  } }

 

package com.ts.taglib.data;

import java.io.Serializable; /**  * 列数据  * @author 陈双  * @date 2012-09-23  * @mail [email protected]  */ public class Column implements Serializable {

 private static final long serialVersionUID = -2581722061552795107L;  private String name;//名称  private String label;//标签  private String type;//类型  private String width;//宽度  private String height;//高度  private String size;//字符个数  private String data;//如果是combox时,选项取数来源  private String colspan;//跨列  private String rowspan;//跨行  private String ondblclick;//双击事件,只是针对参照列  private String drillEvent;//数据钻取时调用的函数  private boolean hidden;//是否隐藏  private boolean disabled;//是否可用  private boolean sum;//是否汇总  public String getName() {   return name;  }  public void setName(String name) {   this.name = name;  }  public String getLabel() {   return label;  }  public void setLabel(String label) {   this.label = label;  }  public String getType() {   return type;  }  public void setType(String type) {   this.type = type;  }  public String getWidth() {   return width;  }  public void setWidth(String width) {   this.width = width;  }  public String getHeight() {   return height;  }  public void setHeight(String height) {   this.height = height;  }  public String getSize() {   return size;  }  public void setSize(String size) {   this.size = size;  }  public String getData() {   return data;  }  public void setData(String data) {   this.data = data;  }  public String getColspan() {   return colspan;  }  public void setColspan(String colspan) {   this.colspan = colspan;  }  public String getRowspan() {   return rowspan;  }  public void setRowspan(String rowspan) {   this.rowspan = rowspan;  }  public String getOndblclick() {   return ondblclick;  }  public void setOndblclick(String ondblclick) {   this.ondblclick = ondblclick;  }  public String getDrillEvent() {   return drillEvent;  }  public void setDrillEvent(String drillEvent) {   this.drillEvent = drillEvent;  }  public boolean isHidden() {   return hidden;  }  public void setHidden(boolean hidden) {   this.hidden = hidden;  }  public boolean isDisabled() {   return disabled;  }  public void setDisabled(boolean disabled) {   this.disabled = disabled;  }  public boolean isSum() {   return sum;  }  public void setSum(boolean sum) {   this.sum = sum;  } }

package com.ts.taglib.data;

import java.io.Serializable; import java.util.List; import java.util.Map; /**  * 用于构建多级表头动态列表格对象  * @author 陈双  * @date 2012-09-22  * @mail [email protected]  *  */ public class DataGrid implements Serializable {

 private static final long serialVersionUID = 3814622301555705194L;     private String name;//名称     private String width;//宽度     private String height;//高度     private boolean multiple;//多行数据是否有勾选框     private List columns;//多级动态列     private List> data;//行数据  private String dblclick;//双击事件     public String getName() {   return name;  }  public void setName(String name) {   this.name = name;  }  public String getWidth() {   return width;  }  public void setWidth(String width) {   this.width = width;  }  public String getHeight() {   return height;  }  public void setHeight(String height) {   this.height = height;  }  public boolean isMultiple() {   return multiple;  }  public void setMultiple(boolean multiple) {   this.multiple = multiple;  }  public List getColumns() {   return columns;  }  public void setColumns(List columns) {   this.columns = columns;  }  public List> getData() {   return data;  }  public void setData(List> data) {   this.data = data;  }  public String getDblclick() {   return dblclick;  }  public void setDblclick(String dblclick) {   this.dblclick = dblclick;  }   }

 

js代码:

//=========================================================================== /**  *表格操作  *@author 陈双  *@date 2012-09-26  *@mail [email protected]  */ //=========================================================================== var globeVariable=new Map();//全局变量列表 /**  * 注册全局变量  * @param object 对象id  * @param property 变量名称  * @param value 值  * @return  */ function put(object,property,value) {  globeVariable.put(object+"_"+property,value); } /**  * 取出全局变量  * @param object 对象id  * @param property 变量名称  * @return  */ function get(object,property) {  return globeVariable.get(object+"_"+property); } /**  * 获取选中行单元格的值  * @param id 表格id  * @param cellIndex 单元格索引  * @param isMultiple 是否有勾选框  * @return Array 选中行的id值的集合  */ function getSelectedCellValue(id,cellIndex,isMultiple){  if(!id)return null;  var values=new Array();//所有选中的id值  if(isMultiple)  {   var checkList=document.getElementsByName(id+"chbox");   var rows=new Array();//选中的行集合   var index=-1;   if(checkList && checkList.length>0)   {    for(var i=0;i";     }    }    else    {     title+="";    }    for(var j=0;j";      }      else      {//第二行       title+="";      }     }     else     {//执行跨行      var rs=columns[j].label.indexOf(',');//是否是跨列      if(i==0)      {//跨行操作       if(rs==-1)       {//跨行        title+="";       }      }      else      {//跨列操作       if(rs!=-1)       {//跨行        title+="";       }      }     }    }    if(i==0)    {//最后一列     title+=" ";    }    else    {     title+="";    }   }  }  else  {//正常情况   title=" ";   if(isMultiple)   {    title+="";   }   for(var i=0;i";   }   title+=" ";  }  if(columns&&columns.length>0)  {//汇总表格   sum="汇总";   if(isMultiple)   {    sum+=" ";   }   for(var i=0;i0)  {//构建第一列和数据表格   if(flag)   {    first=" ";    first+=" ";   }   else   {    first=" ";   }      for(var i=0;i";    data+=""+(i+1)+"";    if(isMultiple)    {     data+="";    }    var rowId="rowid_"+(i+1);    var map=rows.get(i);    for(var j=0;j";    data+=" ";   }  }  //构建汇总列     _sum_columns=new Array();//初始化  var k=0;  for(var i=0;iwidth)  {   total=2;   _isHbar=true;  }  put(id, '_isHbar', _isHbar);  //构建隐藏行  for(var i=0;i ";   for(var j=0;j ";   }   data+=" ";   first+=" ";  }  var title_html="

"+title+"
";  var first_html=""+first+"
";  var data_html="";  data_html+=title+data+"
";  var sum_html=""+sum+"
";  _execute(id, parentId, width, height, first_html+title_html+data_html+sum_html,isMultiple);  sumRowForGrid(id); } /**  * 列结构  * @param name名称  * @param label 标签  * @param type 类型  * @param width 宽度  * @param height 高度  * @param data 列数据来源是一个数组  * @param hidden 是否隐藏  * @param disabled 是否可用  * @param sum 是否汇总  * @param size 字符个数  * @param colspan 跨列  * @param rowspan 跨行  * @param ondblclick 鼠标双击事件  * @param drillEvent 数据钻取处理函数  */ function GridColumn(name,label,type,width,height,data,hidden,disabled,sum,size,colspan,ondblclick,drillEvent) {  this.name=name;  this.label=label;  this.type=type;  this.width=width;  this.height=height;  this.data=data;  this.hidden=hidden;  this.disabled=disabled;  this.sum=sum;  this.size=size;  this.colspan=colspan;  this.οndblclick=ondblclick;  this.drillEvent=drillEvent; } /**  *@param 排序  *@param id为表格id  *@param index:列索引  *@param sort:asc是升序,dsc是降序  *@param isMultiple是否多行  */ function sortTableForGrid(id,index,isMultiple) {  var flag=get(id, 'flag');  var _isHbar=get(id, '_isHbar');  var tr=new Array();//行集合  var td=new Array();//列集合  var temp;//临时单元格     var table=document.getElementById(id);     var header=document.getElementById("header_"+id);     var sort;     if(flag)     {      return;     }     if(isMultiple)     {      index+=2;     }     else     {      index+=1;     }     var html=header.rows[0].cells[index].childNodes[1].innerHTML;     if(html=="")     {        sort="asc";        header.rows[0].cells[index].childNodes[1].innerHTML="▲";     }     else if(html=="▲")     {        sort="dsc";        header.rows[0].cells[index].childNodes[1].innerHTML="▼";     }     else if(html=="▼")     {        sort="asc";        header.rows[0].cells[index].childNodes[1].innerHTML="▲";     }     var k=1;     if(isMultiple)     {      k=2;     }     for(var i=k;itd[j].innerHTML)                      {                          temp=td[i];                          td[i]=td[j];                          td[j]=temp;                      }                 }             }             else             {//数字比较                if(sort=="asc")                 {                      if(parseFloat(td[i].innerHTML)parseFloat(td[j].innerHTML))                      {                          temp=td[i];                          td[i]=td[j];                          td[j]=temp;                      }                 }             }          }     }     var cellCount=table.rows[0].cells.length;     var tempTR=new Array();//临时行     for(var i=0;i0)   {     var newValue=value.replace(/\s+/g,"");     value=newValue;   }   else   {    return false;   }   if(value.length>0 && value.indexOf('.')!=-1)   {//判断是否是小数    var express=/^\d+.\d+$/;//匹配小数    if(express.test(value)){      return true;    }    else    {     return false;    }   }   else   {    var express=/^\d+$/;    if(express.test(value))    {     return true;    }    else    {     return false;    }   }   return false; } /**  * 全选  *@param checked是否选中true为选中,false没选中  *@param name 名称  */ function checkAllForGrid(checked,name) {  var checkList=document.getElementsByName(name);  if(checkList && checkList.length>0)  {   for(var i=0;i

/**  * 构建表格以及滚动条  * @param id 表格id  * @param parentId 父节点,将建构建好的表格节点添加到父节点中  * @param width 表格宽度  * @param height 表格高度  * @param ondblclick 表格双击事件  * @param html (构建好的表格=序列号表格+表头表格+数据表格+汇总表格)  * @return  */ function _execute(id,parentId,width,height,html,isMultiple) {  /**   * 定义构建表格需要的变量   */  var _first_object=null;//第一列,序数表格  var _header_object=null;//表头  var _data_object=null;//数据表格  var _sum_object=null;//汇总表格  var _hbar_object=null;//横向滚动条  var _vbar_object=null;//纵向滚动条  var _current_row=null;//当前选中行  var mainFrame=document.createElement("DIV");  mainFrame.id="DIV_"+id;  mainFrame.style.width=width;  mainFrame.style.height=height;  mainFrame.className="datagrid";  mainFrame.οnmοusedοwn=function (e){//鼠标按下事件   e=e||window.event;   _selectedRow(e,id,isMultiple);//选中行  }  mainFrame.innerHTML=html;  /*   * 添加滚动事件,根据IE的冒泡特性子节点事件触发自后如果父节点也有相同的事件   * 就会接着执行父节点的事件   */  _addScrollEvent(mainFrame,id);  //构建横向滚动条  var hbar=document.createElement("DIV");  hbar.id="hbar";  hbar.style.position="absolute";  hbar.style.width="100%";  hbar.style.height="17px";  hbar.style.overflowX="auto";  hbar.style.top=height-17;  hbar.style.zIndex="10";  hbar.οnscrοll=function(){   _h_scroll(id);//横向滚动  }  hbar.innerHTML="

 
";  //构建纵向滚动条  var vbar=document.createElement("DIV");  vbar.id="vbar";  vbar.style.position="absolute";  vbar.style.width="17px";  vbar.style.height="100%";  vbar.style.overflowY="auto";  vbar.style.left=width-17;  vbar.style.zIndex="10";  vbar.οnscrοll=function(){   _v_scroll(id);//纵向滚动  }  vbar.innerHTML="
 
";  //将表格和滚动条组合在一起  mainFrame.appendChild(hbar);  mainFrame.appendChild(vbar);  //将构建好的表格节点追加到父节点中  document.getElementById(parentId).appendChild(mainFrame);  _first_object=document.getElementById("first_"+id);//第一列,序数表格  _header_object=document.getElementById("header_"+id);//表头  _data_object=document.getElementById(id);//数据表格  _sum_object=document.getElementById("sum_"+id);//汇总表格  _hbar_object=hbar;//横向滚动条  _vbar_object=vbar;//纵向滚动条  /*   * 注册全局变量   */  put(id,'_first_object',_first_object);  put(id,'_header_object',_header_object);  put(id,'_data_object',_data_object);  put(id,'_sum_object',_sum_object);  put(id,'_hbar_object',_hbar_object);  put(id,'_vbar_object',_vbar_object);  var bt=_getCurrentStyle(mainFrame,"borderTopWidth");  var bb=_getCurrentStyle(mainFrame,"borderBottomWidth");  var bl=_getCurrentStyle(mainFrame,"borderLeftWidth");  var br=_getCurrentStyle(mainFrame,"borderRightWidth");  _hbar_object.style.top=parseInt(_hbar_object.style.top)-parseInt(bt)-parseInt(bb);  _vbar_object.style.left=parseInt(_vbar_object.style.left)-parseInt(bl)-parseInt(br);  _block_scroll(id);//设置滚动块 } /**  * 当鼠标按下时选中行  * @param e  * @return  */ function _selectedRow(e,id,isMultiple) {  if(isMultiple)   return;  var td_object=e.srcElement?e.srcElement:e.target;  var _data_object=get(id,'_data_object');  var table=document.getElementById(id);  if(td_object.parentNode.tagName=="TR")  {   var tr_object=td_object.parentNode;   var rowIndex=tr_object.rowIndex;//行索引   var _current_row=get(id,'_current_row');   var flag=get(id,'flag');   var _isHbar=get(id,'_isHbar');   if(flag)   {    if(rowIndex==0||rowIndex==1)    {     return;    }   }   else   {    if(rowIndex==0)    {     return;    }   }   if(_isHbar)   {    if(rowIndex==table.rows.length-1||rowIndex==table.rows.length-2)    {     return;    }   }   else   {    if(rowIndex==table.rows.length-1)    {     return;    }   }   if(_current_row!=null)   {//取消之前的选中行状态    _data_object.rows[_current_row].className="";   }   //重新设置选中行状态   _data_object.rows[rowIndex].className="selectedrow";   _current_row=rowIndex;   put(id,'_current_row',_current_row);  } } /**  * 添加滚动事件监听器  * @param element要添加事件的父节点  * @return  */ function _addScrollEvent(element,id) {  var handler=function(e)  {   _mouseScrollEvent.call(this, e,id);  }  if(document.attachEvent)  {//微软自定义的添加事件监听器   element.attachEvent("onmousewheel",handler);  }  else  {//W3C规范定义的添加事件监听器   element.addEventListener("DOMMouseScroll",handler,false);  } } /**  * 鼠标轮滚动事件和列表事件  * @param e  * @return  */ function _mouseScrollEvent(e,id) {  e=e||window.event;  var _vbar_object=get(id,'_vbar_object');  if(e.wheelDelta<=0 || e.detail>0)  {   _vbar_object.scrollTop+=18;//设置滚动步长为一行的高度  }  else  {   _vbar_object.scrollTop-=18;  } } /**  * 横向滚动  * @return  */ function _h_scroll(id) {  var _hbar_object=get(id,'_hbar_object');  var _header_object=get(id,'_header_object');  var _data_object=get(id,'_data_object');  var _sum_object=get(id,'_sum_object');  _header_object.style.left=-(_hbar_object.scrollLeft);  _data_object.style.left=-(_hbar_object.scrollLeft);  _sum_object.style.left=-(_hbar_object.scrollLeft); } /**  *纵向滚动  * @return  */ function _v_scroll(id) {  var _vbar_object=get(id,'_vbar_object');  var _first_object=get(id,'_first_object');  var _data_object=get(id,'_data_object');  _first_object.style.top=-(_vbar_object.scrollTop);  _data_object.style.top=-(_vbar_object.scrollTop); } /**  * 取出当前元素非style定义的样式  * @param element  * @param property  * @return  */ function _getCurrentStyle(element,property) {  if(element.currentStyle)  {//元素中非style定义的样式,包括内嵌样式和外部样式表中定义的样式   return element.currentStyle[property];  }  else if(window.getComputedStyle)  {//firefox的方式   property=property.replace(/([A-Z])/g, "-$1").toLowerCase();   return window.getComputedStyle(element,null).getPropertyValue(property);  }  else  {   return null;  } } /**  * 设置滚动块  * @return  */ function _block_scroll(id) {  var _hbar_object=get(id,'_hbar_object');  var _vbar_object=get(id,'_vbar_object');  var _data_object=get(id,'_data_object');  _hbar_object.style.display="block";  _vbar_object.style.display="block";  _hbar_object.childNodes[0].style.width=_data_object.offsetWidth;  _vbar_object.childNodes[0].style.height=_data_object.offsetHeight;  if(_hbar_object.childNodes[0].offsetWidth<=_hbar_object.offsetWidth)  {   _hbar_object.style.display="none";  }  if(_vbar_object.childNodes[0].offsetHeight<=_vbar_object.offsetHeight)  {   _vbar_object.style.display="none";  } }

/
/**
 * js通用对象,主要包括List和Map集合对象的定义
 * @author 陈双
 * @date 2012-10-01
 * @mail [email protected]
 */

/**
 * List对象
 * @return
 */
function List()
{
 this.container=new Array(1000);//容器
 this.index=-1;//索引
 this.add=function(element){//添加元素
  this.index++;
  this.container[this.index]=element;
 }
 this.get=function(i){//获取元素
  if(this.index==-1||i<0)
  {
   return null;
  }
  return this.container[i];
 }
 this.size=function(){//List 对象的大小
  return this.index+1;
 }
 this.clear=function(){//清空List对象
  if(this.index!=-1)
  {
   for(var i=0;i<=this.index;i++)
   {
    this.container[i]=null;
   }
  }
  this.index=-1;
 }
 this.contain=function(element){//是否包含某元素
  if(this.index==-1||(!element))
  {
   return false;
  }
  for(var i=0;i<=this.index;i++)
  {
   if(this.container[i]==element)
   {
    return true;
   }
  }
 }
 this.isEmpty=function(){//是否为空
  return this.index==-1?true:false;
 }
 this.remove=function(element){//删除一个元素
  if((!element)||(!this.index))
  {
   return;
  }
  var po=-1;
  var flag=false;//是否移除了
  for(var i=0;i<=this.index;i++)
  {
   if(this.container[i]==element)
   {
    po=i;
    if(po==this.index)
    {
     po=-1;
    }
    flag=true;
   }
   if(po!=-1)
   {
    this.container[po]=this.container[po+1];
    po++;
   }
  }
  if(flag)
  {
   this.index=this.index-1;
  }
 }
 this.removeIn=function(i){//删除指定位置的元素
  if(i<0||this.index<0)
  {
   return;
  }
  var po=-1;
  var flag=false;//是否移除了
  for(var j=0;j<=this.index;j++)
  {
   if(j==i)
   {
    po=j;
    if(po==this.index)
    {
     po=-1;
    }
    flag=true;
   }
   if(po!=-1)
   {
    this.container[po]=this.container[po+1];
    po++;
   }
  }
  if(flag)
  {
   this.index=this.index-1;
  }
 }
}
/**
 * Map对象
 * @return
 */
function Map()
{
 this.index=-1;
 this.entrys=new Array(1000);
 this.get=function(key){//通过key映射value
  if(this.index==-1)
  {
   return null;
  }
  for(var i=0;i<=this.index;i++)
  {
   var entry=this.entrys[i];
   if(entry.key==key)
   {
    return entry.value;
   }
  }
  return null;
 }
 this.size=function(){//返回元素个数
  return this.index+1;
 }
 this.containsKey=function(key){//是否包含key值
  if(this.index==-1)
  {
   return false;
  }
  for(var i=0;i<=this.index;i++)
  {
   var entry=this.entrys[i];
   if(entry.key==key)
   {
    return true;
   }
  }
  return false;
 }
 this.containsValue=function(value){//是否包含值
  if(this.index==-1)
  {
   return false;
  }
  for(var i=0;i<=this.index;i++)
  {
   var entry=this.entrys[i];
   if(entry.value==value)
   {
    return true;
   }
  }
  return false;
 }
 this.put=function(key,value){//添加元素
  if(this.containsKey(key))
  {
   this.remove(key);
  }
  this.index++;
  this.entrys[this.index]=new Entry(key,value);
 }
 this.keySet=function(){//返回key集合
  if(this.index==-1)
  {
   return null;
  }
  var keys=new List();
  for(var i=0;i<=this.index;i++)
  {
   keys.add(this.entrys[i].key);
  }
  return keys;
 }
 this.values=function(){//返回值集合
  if(this.index==-1)
  {
   return null;
  }
  var value=new List();
  for(var i=0;i<=this.index;i++)
  {
   value.add(this.entrys[i].value);
  }
  return value;
 }
 this.clear=function(){//清空
  if(this.index!=-1)
  {
   for(var i=0;i<=this.index;i++)
   {
    this.entrys[i]=null;
   }
   this.index=-1;
  }
 }
 this.remove=function(key){//移除某个元素
  if(this.index==-1)
  {
   return;
  }
  var po=-1;
  var flag=false;
  for(var i=0;i<=this.index;i++)
  {
   var entry=this.entrys[i];
   if(entry.key==key)
   {
    po=i;
    if(i==this.index)
    {
     po=-1;
    }
    flag=true;
   }
   if(po!=-1)
   {
    this.entrys[po]=this.entrys[po+1];
    po++;
   }
  }
  if(flag)
  {
   this.index=this.index-1;
  }
 }
 this.isEmpty=function(){//是否为空
  if(this.index==-1)
  {
   return true;
  }
  return false;
 }
 this.entrySet=function(){//返回Entry
  if(this.index==-1)
  {
   return null;
  }
  var entry=new List();
  for(var i=0;i<=this.index;i++)
  {
   entry.add(this.entrys[i]);
  }
  return entry;
 }
}
/**
 * Map中的节点对象
 * @param key
 * @param value
 * @return
 */
function Entry(key,value)
{
 this.key=key;
 this.value=value;
}
css代码:

/*datagrid 大块样式*/
.datagrid {position:relative;background:white;margin:0px;padding:0px;overflow:hidden;border:1px inset;-moz-user-select:none;}
/*datagrid 表格全局样式*/
.datagrid table {table-layout:fixed;margin:0px;}
.datagrid table td {height:18px;cursor:default;font-size:12px;font-family:verdana;text-indent:2px;border-right:1px solid #cccccc;border-bottom:1px solid #cccccc;text-overflow:ellipsis;overflow:hidden;white-space:nowrap;word-break:keep-all;}
.datagrid table td .arrow {font-size:8px;color:#808080;}
.datagrid table .lastdata {border-right:none;}
.datagrid table .column {cursor:default;background:buttonface;border-top:1px solid #fff;border-right:1px solid #404040;border-bottom:1px solid #404040;border-left:1px solid #fff;}
.datagrid table .over {cursor:default;background:buttonface;border-top:1px solid #fff;border-right:1px solid #404040;border-bottom:1px solid #404040;border-left:1px solid #fff;}
.datagrid table .sortdown {cursor:default;background:buttonface;border-right:1px solid #ffffff;border-bottom:1px solid #ffffff;border-left:1px solid #404040;border-top:1px solid #404040;position:relative;left:1px;}
.datagrid table .dataover {background:#FAFAFA;}
.datagrid table .firstcolumn {width:30px;text-indent:0px;text-align:center;background:buttonface;border-top:1px solid #fff;border-right:1px solid #404040;border-bottom:1px solid #404040;border-left:1px solid #fff;}
.datagrid table .lastcolumn {width:17px;background:buttonface;border-top:1px solid #fff;border-right:1px solid #404040;border-bottom:1px solid #404040;border-left:1px solid #fff;}
/*datagrid 选定行样式*/
.datagrid table .selectedrow {background:highlight;color:white;}
/*datagrid 表头样式*/
.titlecolumn {width:100%;position:absolute;top:0px;left:0px;z-index:3;}
/*datagrid 左边栏样式*/
.slidecolumn {width:30px;position:absolute;top:0px;left:0px;z-index:2;}
.slidecolumn td {width:30px;text-indent:0px;text-align:center;background:buttonface;border-top:1px solid #fff;border-right:1px solid #404040;border-bottom:1px solid #404040;border-left:1px solid #fff;}
/*datagrid 内容表体样式*/
.datacolumn {width:100%;position:absolute;top:0px;left:0px;}
.datacolumn td {top:0px;left:0px;margin:0px;padding:0px}
.datacolumn td input {margin:0px;border:0px #cccccc solid;}

多级动态暂未实现


 

你可能感兴趣的:(java,java,string,object,null,function,datagrid)