第一步编写DataGridColumnTag(表格列)
public class DataGridColumnTag extends BaseGeneralVarTagBinding{ @Override public void render() { } }
第二步编写DataGridOptTag(操作列)
public class DataGridOptTag extends BaseGeneralVarTagBinding{ @Override public void render(){ } }
第三步编写DataGridTag(表格属性)
/** * @ClassName: DateGridTag * @Description:表格标签 * @author 杨力 * @date 2016年11月28日 下午9:39:31 * */ public class DataGridTag extends BaseGeneralVarTagBinding{ public ListcolumnOptList = new ArrayList<>();//操作列集合 public List columnImgList = new ArrayList<>();//操作列集合 protected List columnValueList = new ArrayList ();// 值替换集合 protected List columnQueryList = new ArrayList ();// 查询列集合 public String tableName;//表名 @Override public void render() { this.getBodyContent(); DataGrid dataGrid = initParam(DataGrid.class, super.args); tableName = dataGrid.getId(); StringBuffer buffer = new StringBuffer(); StringBuffer queryBuffer = new StringBuffer(); try { setTableBegin(dataGrid, buffer); List columnList = getColumn(); for (TagNestContext column : columnList) { if("tr".equals(column.getTags()[0])){ setColumn(initParam(DataGridColumn.class, column.getTags()), buffer); }else if("opt".equals(column.getTags()[0])){ columnOptList.add(initParam(DataGridOpt.class, column.getTags())); } } setTableEnd(dataGrid, buffer); buffer.append(""); setSearchPage(dataGrid, queryBuffer);//设置搜索表单 queryBuffer.append(buffer); ctx.byteWriter.writeString(queryBuffer.toString()); clearList();//清理集合 } catch (IOException e) { e.printStackTrace(); } } public void setSearchPage(DataGrid dataGrid,StringBuffer buffer){ if(dataGrid.isShowSearch()){ buffer.append(" \n"); buffer.append("\n"); buffer.append(""); } } public void setTableBegin(DataGrid dataGrid,StringBuffer buffer){ buffer.append(""); } public void setColumn(DataGridColumn column,StringBuffer buffer){ buffer.append("
"); if(dataGrid.isCheckbox()){ buffer.append(" "); } } public void setTableEnd(DataGrid dataGrid,StringBuffer buffer){ buffer.append(" "+column.getText()+" "); //值替换 if (ToolString.isNotEmpty(column.getReplace())) { String[] test = column.getReplace().split(","); String text = ""; String value = ""; for (String string : test) { text += string.substring(0, string.indexOf("_")) + ","; value += string.substring(string.indexOf("_") + 1) + ","; } setColumnReplace(column.getField(), text, value); } //数据字典 if (ToolString.isNotEmpty(column.getDict())) { String text = ""; String value = ""; ListtypeList = CacheKit.get(Constant.CACHE_NAME_SYSTEM_TYPE, ThreadParamInit.CACHESTART_TYPE + column.getDict()); if (typeList != null && !typeList.isEmpty()) { for (TType type : typeList) { text += type.getName() + ","; value += type.getCode() + ","; } setColumnReplace(column.getField(), text, value); } } //图片列 if(column.getImage()){ columnImgList.add(column); } //查询列 if(column.getQuery()){ columnQueryList.add(column); } } public void setOptColumn(StringBuffer buffer){ TagUtil.setOptColumn(columnOptList, buffer, tableName); } public List getColumn(){ HttpServletRequest request = (HttpServletRequest) this.ctx.getGlobal("request"); TagNestContext tnc = (TagNestContext) request.getAttribute("tagContext"); List list = tnc.getChildren(); return list; } /** * @Title: searchForm * @Description: 组装搜索方法 * @param @param buffer 设定文件 * @return void 返回类型 * @throws */ public void setSearchForm(StringBuffer buffer,DataGrid dataGrid){ //搜索方法开始 buffer.append("function search_"+tableName+"(){\n"); if(dataGrid.treeTable){ buffer.append("$('#"+tableName+"').treegrid('load',\n"); }else{ buffer.append("$('#"+tableName+"').datagrid('load',\n"); } buffer.append("serializeObject($('#form_"+tableName+"'))\n"); buffer.append(");};"); //清除查询条件、返回初始数据展示状态 if(dataGrid.treeTable){ buffer.append("function clean_"+tableName+"(){$('#"+tableName+"').treegrid('load',{});$(':input','#form_"+tableName+"').not(':button, :submit, :reset') .val('') .removeAttr('checked') .removeAttr('selected');}"); }else{ buffer.append("function clean_"+tableName+"(){$('#"+tableName+"').datagrid('load',{});$(':input','#form_"+tableName+"').not(':button, :submit, :reset') .val('') .removeAttr('checked') .removeAttr('selected');}"); } } public void clearList(){ tableName = null; columnOptList.clear(); columnValueList.clear(); columnImgList.clear(); columnQueryList.clear(); } /** * @Title: 获取图片列方法 * @author:YangLi * @param @param buffer 设定文件 * @return void 返回类型 * @throws */ private void getImgColumn(StringBuffer buffer){ for (DataGridColumn column : columnImgList) { buffer.append("function "+column.getField()+"Img(value,rec,index){"); if(ToolString.isEmpty(column.getImageWidth())){ buffer.append(" if(value == \"\" || value == null){return ''}"); buffer.append(" return ''}"); }else{ buffer.append(" if(value == \"\" || value == null){return ''}"); buffer.append(" return ''}"); } } } /** * * Summary: setColumn(设置字段替换值) * * @param name * @param text * @param value */ public void setColumnReplace(String name, String text, String value) { DataGridColumnReplace columnValue = new DataGridColumnReplace(); columnValue.setName(name); columnValue.setText(text); columnValue.setValue(value); columnValueList.add(columnValue); } protected void setReplace(StringBuffer buffer){ // 值替換 if (columnValueList.size() > 0) { for (DataGridColumnReplace columnValue : columnValueList) { String testString = ""; String[] value = columnValue.getValue().split(","); String[] text = columnValue.getText().split(","); buffer.append("function "+columnValue.getName()+"Replace(value,rec,index){"); for (int j = 0; j < value.length; j++) { if(value[j] == "' '" || "' '".equals(value[j])){ value[j] = ""; } testString += "if(value=='" + value[j]+ "'){return \'" + text[j] + "\'}"; } buffer.append(testString); buffer.append("else{return value}"); buffer.append("}"); } } } }
第四步编写标签工具类TagUtil
/** * * @ClassName : TagUtil * 标签工具类 * @author : YangLi * @date : 2015年6月3日 下午2:30:29 * */ @SuppressWarnings({ "rawtypes", "unchecked" ,"static-access"}) public class TagUtil { /** * @Title: setOptColumn * @Description: 设置操作列 * @param @param listOpt * @param @param buffer * @param @param tableName 设定文件 * @return void 返回类型 * @throws */ public static void setOptColumn(ListlistOpt,StringBuffer buffer,String tableName){ if(listOpt == null || listOpt.size() == 0) return; buffer.append("function formatter_opt_"+tableName+"(val,row,index){ "); buffer.append("var href='';"); String url = null; for (DataGridOpt opt : listOpt) { url = opt.getUrl(); MessageFormat formatter = new MessageFormat(""); if (opt.getValue() != null) { String[] testvalue = opt.getValue().split(","); List value = new ArrayList