Ext 實時驗證某個輸入值是否已經與數據庫中的某條記錄重名

步驟:


1. js文件:

		this.bIsExist = false;//判斷是否已存在的標誌

		{
							xtype : 'textfield',
							fieldLabel : '合同號',
							name : 'RegisterField_contractNum',
							value:contractNum,
							labelStyle : 'text-align:right;',
							validator : this.checkUserName,//validator爲自定義校驗函數屬性
							invalidText : '該合同號已存在!',
							allowBlank : false
						}

	 /**
	 * 功能:驗證合同號是否已存在
	 */
	,checkIsExist : function() {

		//定義變量值
		var number = this.ownerCt.find('name','RegisterField_contractNum')[0].getValue();//獲取textfield的name屬性值
		
		/*-------------- 發送請求----------------*/
		Ext.Ajax.request({
					url : 'ContractAction_checkExist.do',
					params : {
						contractNum : number
					},
					method : 'POST',
					scope : this.ownerCt,
					success : function(response, options) {
						var responseArray = Ext.util.JSON
								.decode(response.responseText);
						if (responseArray.success == true) // 合同號已經被使用
							this.bIsExist = false;// 給變量賦值
						else
							// 合同號可以使用
							this.bIsExist = true;// 給變量賦值
					}
				});
		 /*-------------/ 發送請求----------------*/
		return this.ownerCt.bIsExist;
	}


2. Action中:

	/**檢查合同號是否已經存在
	 *@author William
	 *@return String
	 * */
	public String checkExist(){
		super.setContentType(super.JSON);
		String	contractNum	 = super.getRequest().getParameter("contractNum"); // 獲取合同號
		//驗證用戶是否存在
		try {
			int id = contractService.sContractIdByInfo(contractNum);
			if( 0 < id )
				super.outPrint("{success:true}");
			else
				super.outPrint("{success:false}");
		} catch (SQLException e) { 
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "json";
	}


3. Service中:

	/**根據合同號查詢合同是否已經存在
	 * @author William
	 * @return id 如果存在則返回合同id, 如果不存在則返回 0
	 * @param String 合同號
	 * */
	public int sContractIdByInfo(String contractNum ) throws SQLException{
		return contractDao.sContractIdByInfo(contractNum);
	}


4. Dao中:

	/**根據合同號查詢合同是否已經存在
	 * @author William
	 * @return id 如果存在則返回合同id, 如果不存在則返回 0
	 * @param String 合同號
	 * */
	public int sContractIdByInfo(String contractNum ) throws SQLException{
		int id = 0;
		Object oReturn = sqlMapClient.queryForObject("Contract.sContractIdByInfo",contractNum);
		if( null != oReturn ){
			id = (Integer)oReturn;
		}
		return id;
	}


5. xml中:

	<!-- 根據合同號 查詢合同是否存在 -->
	<select id="sContractIdByInfo" resultClass="int" parameterClass="string" >
		<![CDATA[ SELECT id FROM sd_contract  WHERE  num = #contractNum# ]]>
	</select>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章