文章
纯javascrit代码制作可编辑分页表格
2011/7/28 13:35:36
/** * 时间管理对象 管理注册函数和注册函数的参数列表信息 * 支持对一个函数内部执行之前的注册before 和执行之后的注册 after * @return */ function JEventMenager(){ this.indexArray=["addRow","setDataGrid","getRowColDataOfCurrentPage","getCurrentPageIndex","gotoCurrentPage","setTitles"]; this.bFuncs=new Array(); this.aFuncts=new Array(); this.bindBeforeFunc=function(_aimFuncName,srcFuncInstanc,args){ var _index = this.indexArray.indexOf(_aimFuncName); if(_index<0) this.indexArray.push(_aimFuncName); _index = this.indexArray.indexOf(_aimFuncName); this.bFuncts[_index].setFunc(srcFuncInstanc);//=srcFuncInstanc; this.bFuncts[_index].setArgs(args); }; this.bindAfterFunc=function(_aimFuncName,srcFuncInstanc,args){ var _index = this.indexArray.indexOf(_aimFuncName); if(_index<0) this.indexArray.push(_aimFuncName); _index=this.indexArray.indexOf(_aimFuncName); this.aFuncts[_index].setFunc(srcFuncInstanc);//=srcFuncInstanc; this.aFuncts[_index].setArgs(args); }; this.getBeforeFunc=function(_aimFuncName){ var _index = this.indexArray.indexOf(_aimFuncName); if(_index<0) return null; return this.bFuncts[_index].getFunc(); }; this.getBeforeArg=function(_aimFuncName){ var _index = this.indexArray.indexOf(_aimFuncName); if(_index<0) return null; return this.bFuncs[_index].getArgs(); }; this.getAfterFunc=function(_aimFuncName){ var _index = this.indexArray.indexOf(_aimFuncName); if(_index<0) return null; return this.aFuncts[_index].getFunc(); }; this.getAfterArg=function(_aimFuncName){ var _index = this.indexArray.indexOf(_aimFuncName); if(_index<0) return null; this.aFuncts[_index].getArgs(); }; for(var i=0 ;i<100;i++){ this.bFuncs.push(new JFunc(null,null)); this.aFuncts.push(new JFunc(null,null)); } } function JFunc(func,args){ this.func=null; this.args=null; this.getFunc=function(){ return this.func; }; this.getArgs=function(){ return this.args; }; this.setFunc=function(func){ this.func =func; }; this.setArgs=function(arg){ this.args =arg; }; } /** * 表格对象买卖IC网 * @disPane 展现该表格的布局对象[必须] * @pageSize 分页的大小[必须] * @tableName 表格的名称[必须] * @tableWidth 表格的宽度--单位:像素 * @return no return; */ function JTable(disPane,pageSize,tableName,tableWidth){ /** * 当前表格将要展现的地方, */ this._PANE=disPane; this._TABLE_NAME=tableName; this._PAGE_SIZE =pageSize||20; this.TABLE_WIDTH=tableWidth||"150px"; this._TABLE_TITLE="表格"; /** * 当前表格的行数 */ this.ROWS=0; /** * 当前表格的列数 */ this.COLS=0; /** * 表格数据存储装置 */ this.tJDataGrid=null; /** * 表格分页控制器 */ this.tJPage = new JPage(this.tJDataGrid,10); /** * 存储配置信息 */ this.tJConfig = new JConfig(); /** * 存储当前页的数据 以便展现 */ this.currentPageData = new JDataGrid(); /** * 执行后处理事件的绑定 * @param _aimActionName 宿主函数的名称 * @param sourceFunc 寄生函数的实例 * @param args 参数列表 */ this.tJEventMenager= new JEventMenager(); this.bindBeforeFunc=function(_aimFuncName,srcFuncInstanc,args){ this.tJEventMenager.bindBeforeFunc(_aimFuncName, srcFuncInstanc, args); }; this.bindAfterFunc=function(_aimFuncName,srcFuncInstanc,args){ this.tJEventMenager.bindAfterFunc(_aimFuncName, srcFuncInstanc, args); }; this.getBeforeFunc=function(_aimFuncName){ return this.tJEventMenager.getBeforeFunc(_aimFuncName); }; this.getBeforeArg=function(_aimFuncName){ return this.getBeforeArg(_aimFuncName); }; this.getAfterFunc=function(_aimFuncName){ return this.getAfterFunc(_aimFuncName); }; this.getAfterArg=function(_aimFuncName){ return this.getAfterArg(_aimFuncName); }; /** * 装载需要显示的数据买卖IC网 */ this.setDataGrid=function(tJDataGrid){ var _aimFuncName="setDataGrid"; this.beforeAgent(_aimFuncName); if(tJDataGrid==null ||tJDataGrid.getRowCount()<=0) tJDataGrid=new JDataGrid(0,1); this.tJDataGrid = tJDataGrid; this.tJPage = new JPage(this.tJDataGrid,this._PAGE_SIZE); this.currentPageData = this.tJPage.getFirst_page(); this.afterAgent(_aimFuncName); }; this.setDataArray=function(_dataArray){ var _aimFuncName="setDataArray"; this.beforeAgent(_aimFuncName); this.tJDataGrid=new JDataGrid(0,1); this.tJDataGrid.setDataArray(_dataArray); this.tJPage = new JPage(this.tJDataGrid,this._PAGE_SIZE); this.currentPageData = this.tJPage.getFirst_page(); this.afterAgent(_aimFuncName); } /** * 对指的行列进行设置data */ this.setRowColData=function(row,col,data){ var _aimFuncName="setRowColData"; this.beforeAgent(_aimFuncName); this.tJDataGrid.setRowColData(row, col, data); this.afterAgent(_aimFuncName); this.gotoCurrentPage(); }; /** * 对指的行列进行设置data */ this.getRowColDataOfCurrentPage=function(row,col){ return this.currentPageData.getRowColData(row, col); }; this.getTableTitle=function(){ return this._TABLE_TITLE||"表格"; }; this.setTableTitle=function(_table_name){ var _aimFuncName="setTableTitle"; this.beforeAgent(_aimFuncName); this._TABLE_TITLE=_table_name||"表格"; this.afterAgent(_aimFuncName); }; /** * 对某页的某个单元格进行html设置 */ this.setRowColHTML=function(pageIndex,row,col,html,refresh){ var _aimFuncName="setRowColHTML"; this.beforeAgent(_aimFuncName); var realRowIndex=(pageIndex-1) * this.tJPage.PAGE_SIZE + row*1; this.tJDataGrid.setRowColData(realRowIndex, col, html); var reDraw=refresh||"0"; if(reDraw=='1'){ this.gotoCurrentPage(); } this.afterAgent(_aimFuncName); }; this.setJConfig=function(tJConfig){ var _aimFuncName="setJConfig"; this.beforeAgent(_aimFuncName); if(tJConfig instanceof JConfig) this.tJConfig = tJConfig; else{ alert("类型错误,参数应该是 JConfigle类型 "); return false; } this.afterAgent(_aimFuncName); }; /** * 获取表格的指定行数据 * 起始行为 1 * 返回的为一个数组买卖IC网 */ this.getRowData=function(_rowIndex){ if(this.tJDataGrid.getRowCount() >= _rowIndex && _rowIndex > 0){ return this.tJDataGrid.getRowData(_rowIndex); } return null; }; /** * 获取表格的指定行数据 * 起始行为 1 * 返回的为一个数组 */ this.getRowDataOfCurrentPage=function(_rowIndex){ var pageIndex= this.getCurrentPageIndex(); var realRowIndex=(pageIndex-1) * this.tJPage.PAGE_SIZE + _rowIndex*1; if(this.tJDataGrid.getRowCount() >= realRowIndex && realRowIndex > 0){ return this.tJDataGrid.getRowData(realRowIndex); } return null; }; /** * 获取当前数据源的给定列的所有数据 * 起始列 为 1 * 返回的为一个数组 */ this.getColData=function(_colIndex){ if(this.tJDataGrid.getColCount() > _colIndex && _colIndex > 0){ return this.tJDataGrid.getColData(_colIndex); } return new Array(this.tJDataGrid.getRowCount()); }; /** * 获取当前页中指定列的数据 * 起始列 为1 * 返回的为一个数组 */ this.getColDataOfCurrentPage=function(_colIndex){ if(this.currentPageData==null) return new Array(); if(this.currentPageData.getColCount() > _colIndex && _colIndex > 0){ return this.currentPageData.getColData(_colIndex); } return new Array(this.tJDataGrid.getRowCount()); }; /** * 为当前表格追加一行 */ this.addRow=function(){ var _aimFuncName="addRow"; this.beforeAgent(_aimFuncName) var newRowReference=this.tJDataGrid.appendRow(); this.afterAgent(_aimFuncName); this.gotoLastPage(); }; this.beforeAgent=function(_aimFuncName){ if(this.tJEventMenager.getBeforeArg(_aimFuncName)!=null){ var args=this.tJEventMenager.getBeforeArg(_aimFuncName); this.tJEventMenager.getBeforeFunc(_aimFuncName).apply(this,args); } }; this.afterAgent=function(_aimFuncName){ if(this.tJEventMenager.getAfterFunc(_aimFuncName)!=null){ var args=this.tJEventMenager.getAfterArg(_aimFuncName); this.tJEventMenager.getAfterFunc(_aimFuncName).apply(this,args); } }; /** * 为当前表格追加一行 * 并且在执行完添加行之后 执行换入的函数 * afterActionFunction:将要被调用的函数 * agrs: 被调用的函数的数组 */ this.addRowWithAfterAction=function(afterActionFunction,agrs){ var newRowReference=this.tJDataGrid.appendRow(); var ags=[]; for(var i =0 ;i <agrs.length;i++){ ags.push(agrs[i]); } /** * 外界的方法被借用到这里 */ afterActionFunction.apply(this,ags); this.gotoLastPage(); }; /** * 返回当前的页面号码 */ this.getCurrentPageIndex=function(){ return this.tJPage.CURRENT_PAGE; }; /** * 删除给定页面的给定行 */ this.deleteRow=function(_pageIndex,_rowIndex){ /** * 要把行数转化成实际的行数 */ var _aimFuncName="deleteRow"; this.beforeAgent(_aimFuncName); var realRowIndex=(_pageIndex-1) * this.tJPage.PAGE_SIZE + _rowIndex*1; this.tJDataGrid.deleteRow(realRowIndex); this.afterAgent(_aimFuncName); this.gotoCurrentPage(); }; /** * 当前的表格进入可编辑状态的时候处罚此函数 */ this.getEditStatus=function(elmt){ var baseElmt=$(elmt); var divPrent=baseElmt.find("div"); var htmlStr="<input type=\"text\" style=\"width:100%; margin:0px; padding:0px;border: 1px solid; border-color: #CCC #EEE #EEE #CCC;overflow:hidden;\" value=\""+divPrent.html()+"\" onBlur='"+this._TABLE_NAME+".lostEditStatus(this)'>"; if(divPrent.find("input").size() <=0){ divPrent.html(htmlStr); } divPrent.find("input").focus(); }; /** * 可编辑表单失去焦点 的处理 */ this.lostEditStatus=function(elmt){ var valu=$(elmt).val(); /** * 保存修改结果到数据集 页数,行数 列数 */ var baseElmt=$(elmt); var _pageIndex=baseElmt.parent().parent().attr("_pageIndex"); var _rowIndex=baseElmt.parent().parent().attr("_rowIndex"); var _colIndex=baseElmt.parent().parent().attr("_colIndex"); baseElmt.parent().parent().html(valu); this.updateDataGridData(_pageIndex, _rowIndex, _colIndex, valu); this.gotoCurrentPage(); }; /** * 当当前表格可以编辑的时候 * 保存当前的编辑结果到数据存储区 */ this.updateDataGridData=function(_pageIndex,_rowIndex,_colIndex,data){ /** * 要把行数转化成实际的行数 */ var realRowIndex=(_pageIndex-1) * this.tJPage.PAGE_SIZE + _rowIndex*1; this.tJDataGrid.setRowColData(realRowIndex, _colIndex, data); this.gotoCurrentPage(); }; /** * 展现当前表格在给定的html标签上 * @return */ this.showTable=function (){ $("#"+this._PANE).html(this.drawHeader()+this.drawData()+this.drawFooter()); }; /** * 表格的头部的绘制 */ this.drawHeader=function(){ var colcount = this.tJConfig.getTitleArray().length; var returnStr=""; returnStr+="<table width=\""+this.TABLE_WIDTH+"\" align=\"center\" border=\"0\" cellpadding=\"4\" cellspacing=\"1\" bgcolor=\"#CBD8AC\" style=\"margin-bottom:0px\">"; returnStr+="<tr bgcolor=\"#EEF4EA\">"; returnStr+="<td colspan=\""+(colcount+1)+"\" background=\"/menage/html/skin/images/frame/wbg.gif\" class='title'><span>"+this.getTableTitle()+"</span></td>"; returnStr+="</tr>"; returnStr+="<tr bgcolor=\"#FFFFFF\" >"; returnStr+="<td width=\"2%\" background=\"/menage/html/skin/images/frame/wbg.gif\" bgcolor=\"#FFFFFF\">N</td>"; for(var colIndex=0;colIndex<colcount;colIndex++){ returnStr+="<td width=\""+this.tJConfig.getConf(colIndex, this.tJConfig._COL_WIDTH)+"\" background=\"/menage/html/skin/images/frame/wbg.gif\" bgcolor=\"#FFFFFF\">"+this.tJConfig.getConf(colIndex, this.tJConfig._COL_NAME)+"</td>"; } returnStr+="</tr>"; return returnStr; }; /** * 表格的数据绘制 */ this.drawData=function(){ var returnStr=""; if(this.currentPageData!= null && this.currentPageData.getRowCount()>0) { var rowCount = this.currentPageData.getRowCount(); var colCount = this.tJConfig.getTitleArray().length; for(var row=1;row <=rowCount;row++){ returnStr+="<tr height=\"20\" onmouseover='"+this._TABLE_NAME+"._mouseover(this);' onmouseout='"+this._TABLE_NAME+"._mouseout(this);'>"; returnStr+="<td width=\"\" bgcolor=\"#FFFFFF\">"+row+"</td>"; /** * 当前第几页 */ var page_Index=this.tJPage.CURRENT_PAGE; var row_Index=row; var col_Index=0; var tableWidthPx=this.TABLE_WIDTH.replace("px","")*1; var colType=""; for(var colIndex=1;colIndex<=colCount;colIndex++){ // this.tJConfig= new JConfig(); colType=this.tJConfig.getConf(colIndex-1, this.tJConfig._COL_TYPE) var tempPerce=this.tJConfig.getConf(colIndex-1, this.tJConfig._COL_WIDTH); col_Index=colIndex; returnStr+="<td nowrap=\"nowrap\" style=\"overflow:hidden;\" _pageIndex=\""+page_Index+"\" _rowIndex=\""+row_Index+"\" _colIndex=\""+col_Index+"\" width=\""+this.tJConfig.getConf(colIndex-1, this.tJConfig._COL_WIDTH)+"\" bgcolor=\"#FFFFFF\" "; /** * 现在支持三种定义方式 1 可双击编辑类型 2 不可编辑类型 4 html类型 */ if(colType=="1"){ //双击可以编辑类型 returnStr+="title=\""+this.currentPageData.getRowColData(row, colIndex)+"\" "; returnStr+="ondblclick=\""+this._TABLE_NAME+".getEditStatus(this);\">"; } else if(colType=="2"){ //双击不可编辑类型 returnStr+="title=\""+this.currentPageData.getRowColData(row, colIndex)+"\" "; returnStr+=" >"; } else if(colType=="4"){ //html类型 returnStr+=" >"; } returnStr+=" <div style=\"width:"+(tableWidthPx*(tempPerce.replace("%","")/100)*0.82 )+"px; margin:0px; padding:0px;overflow:hidden;\">"; /** * 支持虚拟列的展现 * :当给定的数据宽度小于表格的配置宽度的时候,表格正常展现 * 后修改为:当对表格进行设值的时候,可以动态地修改表格的数据结构 , * 但扩展之后的宽度不会超过配置中给定的宽度,否则会发生越界问题 */ if(colIndex > this.currentPageData.getColCount()){ returnStr+=""; } else returnStr+=this.currentPageData.getRowColData(row, colIndex); returnStr+="</div></td>"; } returnStr+="</tr>"; } } else{ returnStr+="<tr height=\"20\">"; returnStr+="<td colspan=\""+(this.tJConfig.getTitleArray().length+1)+"\" bgcolor=\"#FFFFFF\">"; returnStr+="没有查询结果 "; returnStr+="</td>"; returnStr+="</tr>"; } return returnStr; }; this.drawFooter=function(){ var colcount = this.tJConfig.getTitleArray().length; var tableWidthPx=this.TABLE_WIDTH.replace("px","")*1; var returnStr=""; returnStr+="<tr bgcolor=\"#FFFFFF\" >"; returnStr+="<td width=\"100%\" colspan=\""+(colcount+1)+"\" align='right'>"; returnStr+="<table width=\"300px\">"; returnStr+="<tr>"; returnStr+="<td width=\"25%\" bgcolor=\"#FFFFFF\">第"+this.tJPage.CURRENT_PAGE+"/"+this.tJPage.getPageCount()+"页</td>"; returnStr+="<td width=\"10%\" bgcolor=\"#FFFFFF\"><a href=\"javascript:"+this._TABLE_NAME+".gotoFirstPage();\">首页</a></td>"; returnStr+="<td width=\"15%\" bgcolor=\"#FFFFFF\"><a href=\"javascript:"+this._TABLE_NAME+".gotoPrePage();\">上一页</a></td>"; returnStr+="<td width=\"15%\" bgcolor=\"#FFFFFF\"><a href=\"javascript:"+this._TABLE_NAME+".gotoNextPage();\">下一页</a></td>"; returnStr+="<td width=\"10%\" bgcolor=\"#FFFFFF\"><a href=\"javascript:"+this._TABLE_NAME+".gotoLastPage();\">尾页</a></td>"; returnStr+="<td width=\"5%\" bgcolor=\"#FFFFFF\"><a href=\"javascript:"+this._TABLE_NAME+".addRow();\">+</a></td>"; returnStr+="</tr>"; returnStr+="</table>"; returnStr+="</td>"; returnStr+="</tr>"; returnStr+="</table>"; return returnStr; }; this._mouseover=function(_el){ $.each($(_el).children(),function(i,elm){ $(elm).css("background-color","#C0C0C0"); }); }; this._mouseout=function(_el){ $.each($(_el).children(),function(i,elm){ $(elm).css("background-color","#FFFFFF"); }); }; /** * 刷新当前页 */ this.gotoCurrentPage=function(){ var _aimFuncName="gotoCurrentPage"; this.beforeAgent(_aimFuncName); this.currentPageData=this.tJPage.refreshCurrentPage(); this.afterAgent(_aimFuncName); this.showTable(); }; this.gotoFirstPage=function(){ var _aimFuncName="gotoFirstPage"; this.beforeAgent(_aimFuncName); this.currentPageData=this.tJPage.getFirst_page(); this.afterAgent(_aimFuncName); this.showTable(); }; this.gotoPrePage=function(){ var _aimFuncName="gotoPrePage"; this.beforeAgent(_aimFuncName); this.currentPageData=this.tJPage.getPre_page(); this.afterAgent(_aimFuncName); this.showTable(); }; this.gotoNextPage=function(){ var _aimFuncName="gotoNextPage"; this.beforeAgent(_aimFuncName); this.currentPageData=this.tJPage.getNext_page(); this.afterAgent(_aimFuncName); this.showTable(); }; this.gotoLastPage=function(){ var _aimFuncName="gotoLastPage"; this.beforeAgent(_aimFuncName); this.currentPageData=this.tJPage.getLast_page(); this.afterAgent(_aimFuncName); this.showTable(); }; /** * 设置表格的头 * like tarray = new Array("字段1","字段2","字段3","字段4"); */ this.setTitles=function(titleArray){ this.tJConfig.setTitles(titleArray); }; /** * like tarray = new Array("10%","10%","10%","10%"); */ this.setColWidths=function(widthsArray){ this.tJConfig.setColWidths(widthsArray); }; /** * 字符串类型1 下拉列表类型2 输入框类型3 单选框4 复选框5 like tarray = new Array("1","2","3","4"); * 1: 字符类型 ;2: 整形 ;3: 浮点型; 4: html */ this.setColTypes=function(typesArray){ this.tJConfig.setColTypes(typesArray); }; /** * 是否隐藏 1 隐藏 0 不隐藏 like tarray = new Array("0","0","1","1"); */ this.setHidden=function(hiddenArray){ this.tJConfig.setHidden(hiddenArray); }; /** * 以下的是初始化过程 */ if(this.tJDataGrid==null){ /** * 如果用户没有设置结果集合 * 那么需要表格自动处理结果集的创建 * 默认创建的是1行一列的数据 */ this.setDataGrid(null); } } /** * 数据对象 * @return */ function JDataGrid(rows,cols){ /** * 当前行数 */ this.rows=rows||0; /** * 当前列数 */ this.cols=cols||0; /** * 存储数据的对象 */ this.dataGrid=new Array(); /** * 用二维数组构建一个JDataGrid * @param _dataArray * @return */ this.setDataArray=function(_dataArray){ if(_dataArray instanceof Array){ this.rows=_dataArray.length; var colData; var maxCol=0; for(var row=0 ; row<this.rows ; row++){ colData=_dataArray[row]; if(colData instanceof Array){ for(var colIndex=0 ; colIndex < colData.length ; colIndex++){ this.setRowColData(row+1,colIndex+1,colData[colIndex]); } } else{ this.setRowColData(row+1,1,_dataArray[row]); } } } }; /** * 删除指定位置的行 * @param rowIndex * @return */ this.deleteRow=function (rowIndex){ if(this.rows < rowIndex){ return false; } this.dataGrid.splice(rowIndex-1,1); this.rows=this.dataGrid.length; }; /** * 释放指定的行的空间 * 只服务于 JTreeTable * @param rowIndex * @return */ this.deleteRowForHide=function (rowIndex){ if(this.rows < rowIndex){ return ; } this.dataGrid[rowIndex-1]=null; }; /** * 在指定的位置插入一行 * @param rowIndex * @return */ this.insertRow=function (rowIndex){ if(this.rows < rowIndex){ return false; } var newArray=new Array(this.cols); for(var colIndex=0;colIndex<this.cols;colIndex++){ newArray[colIndex]=""; } this.dataGrid.splice(rowIndex,0,new Array(this.cols)); this.rows=this.dataGrid.length; }; /** * 在末尾追加一行 * * @return 新添加行的引用 */ this.appendRow=function (){ var array = new Array(this.cols); for(var index=0;index<this.cols;index++) array[index]=""; this.dataGrid.splice(this.rows,0,array); this.rows=this.dataGrid.length; return array; }; /** * 获取某一位置的数据 * @param row * @param col * @return */ this.getRowColData=function(row,col){ if(this.rows< row) { alert("行数 "+row+" 超过范围"); return false; } if(this.cols < col){ // alert("列数 "+col+" 超过范围"); return ""; } if(this.dataGrid[row-1].length < this.cols){ if(this.dataGrid[row-1].length <=col) return this.dataGrid[row-1][col-1]; else return ""; } return this.dataGrid[row-1][col-1]; }; this.sortBy=function(_colIndex){ if(this.cols < _colIndex){ return ; } var map=new Array(); for(var _row=1 ; _row<=this.rows ; _row++){ map[""]=this.dataGrid[_row][_colIndex-1]; } }; /** * 对某一位置设置数据 * @param row * @param col * @param data * @return */ this.setRowColData=function(row,col,data){ var col=col*1; /** * 扩充当前的行数 */ while(this.rows < row){ this.rows=this.dataGrid.push(new Array(this.cols)); } if(!(this.dataGrid[row-1] instanceof Array)){ this.dataGrid[row-1] =[""]; } if(this.cols < col) { var oldRowCount=this.dataGrid.length; for(var row_index=0 ; row_index<oldRowCount ; row_index++){ var oldLenth=this.dataGrid[row_index].length; var sub=col-oldLenth; while(sub-- > 0){ this.dataGrid[row_index][oldLenth++]=""; } } /** * 为了支持虚拟列的展现对超过数据定义范围的操作 * 只虚拟扩展本行记录 */ this.dataGrid[row-1][col-1]=data; this.cols=col; } else{ this.dataGrid[row-1][col-1]=data; } }; /** * 获得当前的最大行数 * @return */ this.getMaxRow=function getMaxRow(){ return this.rows-1; }; this.getRowCount=function (){ return this.rows; }; this.getColCount=function(){ return this.cols; }; /** * 获得当前最大列数 * @return */ this.getMaxCol=function getCols(){ return this.cols; }; /** * 举行区域数据拷贝 * @param rowStart * @param colStart * @param rowEnd * @param colEnd * @return */ this.copyRectangle=function(rowStart,colStart,rowEnd,colEnd){ if(rowStart >rowEnd ||rowEnd>this.rows ||colStart>colEnd ||colEnd>this.cols){ // alert("chaochufanwer"); return null; } var returnGrid = new JDataGrid(rowEnd-rowStart+1,colEnd-colStart+1); var tempRowLimit=rowEnd-rowStart; var tempColLimit = colEnd-colStart; var currentRowColCount=0; for(var row=0 ; row<=tempRowLimit;row++ ){ /** * 当前的行的实际列数 */ currentRowColCount=this.getColCountOfRow(row+1); /** * 计算的时候使用的计算出来的列索引 */ var countColIndex=0; for(var col=0;col<=tempColLimit;col++){ countColIndex=colStart+col; /** * 计算出来的索引超过了实际的列数 * 需要返回空 */ if(countColIndex > currentRowColCount) returnGrid.setRowColData(row+1, col+1, ""); else returnGrid.setRowColData(row+1, col+1, this.getRowColData(rowStart+row, colStart+col)); } } return returnGrid; }; /** * 测试数据集的使用 * @return */ this.toString=function(){ var returnString=""; returnString+="<table>"; for(var row=1 ; row <= this.rows ; row++){ returnString+="<tr>"; for(var col = 1;col <= this.cols;col++){ returnString+="<td>"; returnString+=this.getRowColData(row, col); returnString+="</td>"; } returnString+="</tr>"; } returnString+="</table>"; return returnString; }; /** * 获取指定行的数据 * 起始行为1 * @return */ this.getRowData=function(_rowIndex){ if(this.getRowCount() >= _rowIndex && _rowIndex >0){ return this.dataGrid[_rowIndex-1]; } return new Array(); }; /** * 获取指定列的数据 * 起始列为 1 * @return */ this.getColData=function(_colIndex){ var rowCount= this.dataGrid.length; var returnArray= new Array(rowCount); if(this.getColCount() >= _colIndex && _colIndex >0){ for(var i=1;i<= rowCount;i++){ returnArray[i-1]=this.getRowColData(i, _colIndex); } } return returnArray; }; /** * 获取某一行的实际列数 */ this.getColCountOfRow=function(_rowIndex){ if(this.getRowCount() >= _rowIndex && _rowIndex >0){ return this.dataGrid[_rowIndex-1].length; } return -1; }; /** * 一下的是初始化过程 */ var temp=this.rows; while( temp > this.dataGrid.length){ this.insertRow(this.getRowCount()); } } /** * 翻页对象 * @return */ function JPage(tJDataGrid,tPAGE_SIZE){ this.PAGE_SIZE=tPAGE_SIZE; this.DATA_GRID= tJDataGrid; /** * 总页数 */ this.PAGE_COUNT=0; /** * 当前页 */ this.CURRENT_PAGE=0; /** * 上一页 */ this.PRE_PAGE=0; /** * 下一页 */ this.NEXT_PAGE=0; /** * 首页 */ this.FIRST_PAGE=0; /** * 下一页 */ this.LAST_PAGE=0; /** * 获得上一页的数据 * @return JDataGrid */ this.getPre_page=function(){ if(this.CURRENT_PAGE>1){ this.CURRENT_PAGE-=1; } return this.getPage(this.CURRENT_PAGE); }; /** * 获取下一页 * @return */ this.getNext_page=function(){ if(this.CURRENT_PAGE < this.getPageCount()) this.CURRENT_PAGE+=1; return this.getPage(this.CURRENT_PAGE); }; this.refreshCurrentPage=function(){ return this.getPage(this.CURRENT_PAGE); }; /** * 获取第一页 * @return */ this.getFirst_page=function(){ var tt =this.getPage(1); return tt; }; /** * 获取最后一页 * @return */ this.getLast_page=function(){ return this.getPage(this.getPageCount()); }; /** * 获取某一页的数据集 */ this.getPage=function(pageIndex){ var pageCount=this.getPageCount(); var colcount=this.DATA_GRID.getColCount(); if(pageIndex > pageCount){ return null; } if(this.DATA_GRID==null) this.DATA_GRID=new JDataGrid(1,1); this.CURRENT_PAGE=pageIndex; var rowStart,rowEnd; rowStart =(pageIndex-1)*this.PAGE_SIZE+1; rowEnd=pageIndex * this.PAGE_SIZE; if(this.getPageCount() == pageIndex){ if(this.getPageCount() % this.PAGE_SIZE == 0){ rowEnd=pageIndex * this.PAGE_SIZE; } else rowEnd=this.DATA_GRID.getRowCount(); } this.CURRENT_PAGE=pageIndex; return this.DATA_GRID.copyRectangle(rowStart, 1, rowEnd, colcount); }; /** * 获得当前的总页数 */ this.getPageCount=function(){ if(this.DATA_GRID!=null && this.DATA_GRID.getRowCount()>0){ var up = Math.ceil(this.DATA_GRID.getRowCount() / this.PAGE_SIZE); this.PAGE_COUNT=up; return up; } else{ this.PAGE_COUNT=0; return 0; } }; /** * 一下的是初始化过程 */ this.PAGE_COUNT=this.getPageCount(); this.CURRENT_PAGE=1; } function JConfig(){ this.TABLE_WIDTH='150px'; this.defaultTableWidth=150; this._COL_NAME="name"; this._COL_WIDTH="width"; this._COL_TYPE="type"; this._COL_HIDDEN="hidden"; this.titleArray=new Array(); this.default_titleArray=["测试列1","测试列2","测试列3","测试列4","测试列5"]; this.widthsArray=new Array(); this.default_widthsArray=["25%","25%","25%","25%","25%"]; this.typesArray=new Array(); this.default_typesArray=["1","1","1","1","1"]; this.hiddenArray=new Array(); this.default_typesArray=["0","0","0","0","0"]; this.getTitleArray=function(){ if(this.titleArray== null ||this.titleArray.length<=0){ return this.default_titleArray; } return this.titleArray; }; this.setTableWidth=function(_table_Width){ this.TABLE_WIDTH=(this.defaultTableWidth+"px")||_table_Width; }; this.getTableWidth=function(){ return this.TABLE_WIDTH||this.defaultTableWidth+"px"; }; /** * 设置表格的头 * like tarray = new Array("字段1","字段2","字段3","字段4"); */ this.setTitles=function(titleArray){ this.titleArray = titleArray; }; /** * like tarray = new Array("10%","10%","10%","10%"); */ this.setColWidths=function(widthsArray){ this.widthsArray = widthsArray; }; /** * 字符串类型1 下拉列表类型2 输入框类型3 单选框4 复选框5 like tarray = new Array("1","2","3","4"); */ this.setColTypes=function(typesArray){ this.typesArray= typesArray; }; /** * 是否隐藏 1 隐藏 0 不隐藏 like tarray = new Array("0","0","1","1"); */ this.setHidden=function(hiddenArray){ this.hiddenArray = hiddenArray; }; this.getConf=function(colIndex,configType){ if(configType==this._COL_NAME){ if(this.titleArray==null ||this.titleArray.length<=0){ return this.default_titleArray[colIndex]; } return this.titleArray[colIndex]; } if(configType==this._COL_WIDTH){ if(this.widthsArray==null ||this.widthsArray.length<=0){ return this.default_widthsArray[colIndex]; } return this.widthsArray[colIndex]; } if(configType==this._COL_TYPE){ if(this.typesArray==null ||this.typesArray.length<=0){ return this.default_typesArray[colIndex]; } return this.typesArray[colIndex]; } if(configType==this._COL_HIDDEN){ if(this.hiddenArray==null ||this.hiddenArray.length<=0){ return this.default_hiddenArray[colIndex]; } return this.hiddenArray[colIndex]; } }; }