ExtJs高级查询之参数装配

主要实现的是高级查询面板向后台传递参数,自动装配,进行查询。高级查询面板的代码如下:

	var searchForm = new Ext.FormPanel({
		labelWidth : 35,
		layout : 'column',
		floating : false,
		bodyStyle : 'padding:5px 5px 0',
		draggable : false,
		defaults : {
			width : 230
		},
		defaultType : 'textfield',
		items : [{
			fieldLabel : '用户名',
			name : 'username',
			allowBlank : true,
			emptyText : "请输入用户名",
			id : 'username'
		}, {
			fieldLabel : '昵称',
			name : 'nickname',
			allowBlank : true,
			emptyText : "请输入用户昵称",
			id : 'nickname'
		}, {
			xtype : 'combobox',
			fieldLabel : '性别',
			emptyText : '请选择操作员性别',
			store : store_sex,
			displayField : 'text',
			valueField : 'sex',
			name : 'sex',
			id : 'sex',
			allowBlank : true
		}, {
			fieldLabel : '注册时间从',
			name : 'registDate',
			xtype : 'datefield',
			readOnly : false,
			format : 'Y-m-d H:i:s',
			allowBlank : true,
			id : 'registDateFrom'
		}, {
			fieldLabel : '到',
			name : 'registDate',
			xtype : 'datefield',
			readOnly : false,
			format : 'Y-m-d H:i:s',
			allowBlank : true,
			id : 'registDateTo'
		}],

		buttons : [{
			text : '查询',
			type : 'submit',
			handler : function() {
				store.on('beforeload', function() {
					store.proxy.extraParams = {
						username_LIKE_STRING : Ext.getCmp('username')
								.getValue(),
						nickname_LIKE_STRING : Ext.getCmp('nickname')
								.getValue(),
						sex_EQ_INT : Ext.getCmp('sex').getValue(),
						registDate_GT_DATE : Ext.getCmp('registDateFrom')
								.getValue(),
						registDate_LT_DATE : Ext.getCmp('registDateTo')
								.getValue()
					};
				});
				store.load({
					params : {
						start : 0,
						limit : 10
					}
				});
			}
		}, {
			text : '重置',
			handler : function() {
				searchForm.getForm().reset();
			}
		}]
	}).render("admindata");
	/**
	 * 装配参数hql语句
	 * 
	 * @param entity
	 * @return
	 * @throws ParseException
	 */
	public String completeHQL(String entity) throws ParseException {
		String hql = " where ";
		Enumeration params = (Enumeration) this.servletRequest
				.getParameterNames();
		while (params.hasMoreElements()) {
			String param = (String) params.nextElement();
			if (param.contains("_")) {
				String[] array = param.split("_");
				String paramname = "";
				String action = "";
				String type = "";
				if (array.length == 3) {
					paramname = array[0];
					action = array[1];
					type = array[2];
				}
				if (array.length == 4) {
					paramname = array[0] + "." + array[1];
					action = array[2];
					type = array[3];
				}
				if (Util.isValidSring(paramname) && Util.isValidSring(action)
						&& Util.isValidSring(type)) {
					String paramvalue = this.servletRequest.getParameter(param);
					if (Util.isValidSring(paramvalue) == true) {
						String g = this.completeFormula(action);
						hql += " " + entity + "." + paramname + g;
						if (g.equals(" like ")) {
							paramvalue = "%" + paramvalue + "%";
						}
						if (type.equals("DATE")) {
							hql += "'"
									+ DateUtil.formatDate(DateFormat
											.getDateInstance()
											.parse(paramvalue)) + "'" + " and ";
						} else {
							hql += "'" + paramvalue + "'" + " and ";
						}
					}
				}
			}
		}
		hql += "1=1";
		return hql;
	}

	/**
	 * 匹配公式符号
	 * 
	 * @param f
	 * @return
	 */
	public String completeFormula(String f) {
		if (f.equals("LIKE")) {
			return " like ";
		}
		if (f.equals("GT")) {
			return " > ";
		}
		if (f.equals("LT")) {
			return " < ";
		}
		if (f.equals("EQ")) {
			return " = ";
		} else {
			return "";
		}
	}

	
/**
	 * 获取管理员信息
	 * 
	 * @throws ParseException
	 * @throws NumberFormatException
	 */
	public void infos() throws NumberFormatException, ParseException {
		// 参数
		String limit = this.servletRequest.getParameter("limit");
		String page = this.servletRequest.getParameter("page");
		int st = (Integer.parseInt(page) - 1) * Integer.parseInt(limit);
		// 查询结果
		Result result = this.service.find("from AdminUser u "
				+ this.completeHQL("u") + " order by u.createDate desc",
				new String[] {}, st, Integer.parseInt(limit));
		int total = this.service.count("select count(*) from AdminUser u "
				+ this.completeHQL("u") + "");
		// 得到管理员信息
		List<AdminUser> list = (List<AdminUser>) result.getData();
		// JSON声明
		JSONArray jsonArray = new JSONArray();
		JSONObject jsono = new JSONObject();
		// 拼写JSON字符串
		for (AdminUser a : (List<AdminUser>) list) {
			jsono.put("id", a.getId());
			jsono.put("username", a.getUsername());
			jsono.put("createDate", DateUtil.formatDate(a.getCreateDate()));
			jsono.put("registDate", DateUtil.formatDate(a.getRegistDate()));
			jsono.put("sex", a.getSex());
			jsonArray.add(jsono);
			jsono.clear();
		}
		JSONObject j = new JSONObject();
		// 设置回传参数
		j.put("totalCount", total);
		j.put("items", jsonArray);
		j.put("start", st);
		j.put("limit", limit);
		// 回传
		JsonResult.json(j.toString(), servletResponse);
	}
public class DateUtil {

	private static final SimpleDateFormat formatter = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	public static synchronized String formatDate(Date date) {
		return formatter.format(date);
	}

}
	public static Boolean isValidSring(String str) {
		if (!str.equals("") && str != null) {
			return true;
		} else {
			return false;
		}
	}


    var searchForm = new Ext.FormPanel({
        labelWidth : 35,
        layout : 'column',
        floating : false,
        bodyStyle : 'padding:5px 5px 0',
        draggable : false,
        defaults : {
            width : 230
        },
        defaultType : 'textfield',
        items : [{
            fieldLabel : '用户名',
            name : 'username',
            allowBlank : true,
            emptyText : "请输入用户名",
            id : 'username'
        }, {
            fieldLabel : '昵称',
            name : 'nickname',
            allowBlank : true,
            emptyText : "请输入用户昵称",
            id : 'nickname'
        }, {
            xtype : 'combobox',
            fieldLabel : '性别',
            emptyText : '请选择操作员性别',
            store : store_sex,
            displayField : 'text',
            valueField : 'sex',
            name : 'sex',
            id : 'sex',
            allowBlank : true
        }, {
            fieldLabel : '注册时间从',
            name : 'registDate',
            xtype : 'datefield',
            readOnly : false,
            format : 'Y-m-d H:i:s',
            allowBlank : true,
            id : 'registDateFrom'
        }, {
            fieldLabel : '到',
            name : 'registDate',
            xtype : 'datefield',
            readOnly : false,
            format : 'Y-m-d H:i:s',
            allowBlank : true,
            id : 'registDateTo'
        }],

        buttons : [{
            text : '查询',
            type : 'submit',
            handler : function() {
                store.on('beforeload', function() {
                    store.proxy.extraParams = {
                        username_LIKE_STRING : Ext.getCmp('username')
                                .getValue(),
                        nickname_LIKE_STRING : Ext.getCmp('nickname')
                                .getValue(),
                        sex_EQ_INT : Ext.getCmp('sex').getValue(),
                        registDate_GT_DATE : Ext.getCmp('registDateFrom')
                                .getValue(),
                        registDate_LT_DATE : Ext.getCmp('registDateTo')
                                .getValue()
                    };
                });
                store.load({
                    params : {
                        start : 0,
                        limit : 10
                    }
                });
            }
        }, {
            text : '重置',
            handler : function() {
                searchForm.getForm().reset();
            }
        }]
    }).render("admindata");

原文地址:https://www.cnblogs.com/tatame/p/2497985.html