Jfinal集成easyui标签-第二章JAVA标签编写

阅读更多

 第一步编写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 List columnOptList = 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("
\n"); for (DataGridColumn columnQuery : columnQueryList) { buffer.append("
"); if (ToolString.isNotEmpty(columnQuery.getReplace())) { buffer.append(""); } else if (ToolString.isNotEmpty(columnQuery.getDict())) { List typeList = CacheKit.get(Constant.CACHE_NAME_SYSTEM_TYPE, ThreadParamInit.CACHESTART_TYPE + columnQuery.getDict()); buffer.append(""); }else if("group".equals(columnQuery.getQueryMode())){ buffer.append(""); buffer.append("~"); buffer.append(""); }else{ buffer.append(""); } buffer.append("
"); } buffer.append("
\n"); buffer.append("\n"); buffer.append("
\n"); buffer.append("
"); buffer.append("
\n"); buffer.append(" \n"); buffer.append(" \n"); buffer.append("
\n"); buffer.append("
\n"); buffer.append("
\n"); buffer.append("
\n"); buffer.append("
"); } } public void setTableBegin(DataGrid dataGrid,StringBuffer buffer){ buffer.append(""); if(dataGrid.isCheckbox()){ buffer.append(""); } } public void setTableEnd(DataGrid dataGrid,StringBuffer buffer){ buffer.append("
"); } public void setColumn(DataGridColumn column,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 = ""; List typeList = 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(List listOpt,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();
                for (String test : testvalue)
                {
                    value.add("\"+row." + test + " +\"");
                }
                url = formatter.format(url, value.toArray());
            }
            if (url != null && opt.getValue() == null)
            {

                url = formatUrl(url);
            }
            String exp = opt.getExp();// 判断显示表达式
            if (ToolString.isNotEmpty(exp))
            {
                String[] ShowbyFields = exp.split("&&");
                for (String ShowbyField : ShowbyFields)
                {
                    int beginIndex = ShowbyField.indexOf("#");
                    int endIndex = ShowbyField.lastIndexOf("#");
                    String exptype = ShowbyField.substring(beginIndex + 1,
                            endIndex);// 表达式类型
                    String field = ShowbyField.substring(0, beginIndex);// 判断显示依据字段
                    String[] values = ShowbyField.substring(endIndex + 1,
                            ShowbyField.length()).split(",");// 传入字段值
                    String value = "";
                    for (int i = 0; i < values.length; i++)
                    {
                        value += "'" + "" + values[i] + "" + "'";
                        if (i < values.length - 1)
                        {
                            value += ",";
                        }
                    }
                    if ("eq".equals(exptype))
                    {
                   	 buffer.append("if($.inArray(\"\"+row." + field + ",[" + value
                                + "])>=0){");
                    }
                    if ("ne".equals(exptype))
                    {
                   	 buffer.append("if($.inArray(\"\"+row." + field + ",[" + value
                                + "])<0){");
                    }
                    if ("empty".equals(exptype) && value.equals("'true'"))
                    {
                   	 buffer.append("if(\"\"+row." + field + "==''){");
                    }
                    if ("empty".equals(exptype) && value.equals("'false'"))
                    {
                   	 buffer.append("if(\"\"+row." + field + "!=''){");
                    }
                }
            }
            if (OptTypeDirection.del.toString().equals(opt.getType()))
            {
            	buffer.append("href+=\" \";");
            }
            if (OptTypeDirection.fun.toString().equals(opt.getType()))
            {
            	String name = TagUtil.getFunction(opt.getFunname());
                String parmars = TagUtil.getFunParams(opt.getFunname());
            	buffer.append("href+=\" \";");
            }
            if (OptTypeDirection.openwin.toString().equals(opt.getType()))
            {
            	buffer.append("href+=\" \";");
            }
            if (ToolString.isNotEmpty(exp))
            {
                for (int i = 0; i < exp.split("&&").length; i++)
                {
               	 buffer.append("}");
                }

            }
            
		}
    	buffer.append("return href;}");
    }
	
	
	/**
	 * 获取自定义函数名
	 * 
	 * @param functionname
	 * @return
	 */
	public static String getFunction(String functionname) {
		int index = functionname.indexOf("(");
		if (index == -1) {
			return functionname;
		} else {
			return functionname.substring(0, functionname.indexOf("("));
		}
	}
	/**
	 * 获取自定义函数的参数
	 * 
	 * @param functionname
	 * @return
	 */
	public static String getFunParams(String functionname) {
		int index = functionname.indexOf("(");
		String param="";
		if (index != -1) {
			String testparam = functionname.substring(functionname.indexOf("(")+1,
					functionname.length() - 1);
			if(ToolString.isNotEmpty(testparam))
			{
			String[] params=testparam.split(",");
			for (String string : params) {
				//是数字表示,手动传入状态
				if(ToolString.isNumeric(string)){
					 param+= string.replaceAll("_", "")+",";
			    }else{
			        param+="'\"+row."+ string + "+\"',";
			    }
			}
			}
		} 
		param+="'\"+index+\"'";//传出行索引号参数
		return param;
	}
	/**
     * 格式化URL
     * 
     * @return
     */
	
	private static String formatUrl(String url)
    {
        String parurlvalue = "";
        if (url.indexOf("?") >= 0)
        {
            String beforeurl = url.substring(0, url.indexOf("?"));// 截取请求地址
            String parurl = url.substring(url.indexOf("?") + 1, url.length());// 截取参数
            String[] pras = parurl.split("&");
            List value = new ArrayList();
            int j = 0;
            for (int i = 0; i < pras.length; i++)
            {
                if (pras[i].indexOf("{") >= 0 || pras[i].indexOf("#") >= 0)
                {
                    String field = pras[i].substring(pras[i].indexOf("{") + 1,
                            pras[i].lastIndexOf("}"));
                    if(i == 0){
                    	parurlvalue += "?"+ pras[i].replace("{" + field + "}", "{" + j + "}");
                    }else{
                    	parurlvalue += "&"+ pras[i].replace("{" + field + "}", "{" + j + "}");
                    }
                    value.add("\"+row." + field + " +\"");
                    j++;
                }
                else
                {
                    parurlvalue += "" + pras[i];
                }
            }
            url = MessageFormat.format(beforeurl + parurlvalue, value.toArray());
        }
        return url;

    }
} 
  

 

你可能感兴趣的:(easyui,jFinal,beetl,标签,嵌套标签)