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");


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM