๐Ÿ‘จ‍๐Ÿ’ป kimtongting

*์†Œ์†์„ ํ†ตํ•ด ํ•ด๋‹น ์†Œ์†์˜ ๊ฐ€๋งน์  ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค์ž.

 

 

java - controller

 

//๊ฐ€๋งน์  ๋ชฉ๋ก
	@RequestMapping(value = "/aJaxUserOrgList.do", method = RequestMethod.GET)
	public void aJaxUserOrgList(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
		//Controller์—์„œ ๋ฐ”๋กœ write๋ฅผ ํ•ด์ค„ ์˜ˆ์ •์ด๊ธฐ์— ๋ฐ˜ํ™˜ํ˜•์€ void. ๋•Œ์— ๋”ฐ๋ผ ์ˆ˜์ •
        //Log4j2.logger.info(Log4j2.log + getClass + Util.getMethodName());

		//javascript์—์„œ ํ˜ธ์ถœํ•œ Parameter.
		String franchisor_id=request.getParameter("franchisor_id");
        
        //ํ•ด๋‹น Parameter ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ , DB์—์„œ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์กฐ์ง ์ •๋ณด๋ฅผ List<Model>์— ์ €์žฅ.
		List<OrgVO> userOrgList=userDao.getOrgList(franchisor_id);
		//Log4j2.logger.info(Log4j2.logT+userOrgList);

		//List์— ๋‹ด๊ธด ๊ฐ Model๋“ค์„ Json Array ๋ฐฐ์—ด์— ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด JSONArray ์„ ์–ธ
		JSONArray jsonArray= new JSONArray();
		try {
		
        //์กฐ์ง์˜ ๊ฐฏ์ˆ˜๋งŒํผ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉฐ JSONArray์— ์ €์žฅ.
		for(int i=0;i<userOrgList.size();i++){
        	//ํ•œ ์กฐ์ง๋‹น ํ•˜๋‚˜์˜ JSONObject๋ฅผ ๋งŒ๋“ค๊ธฐ.
			JSONObject jsonObject=new JSONObject();
			jsonObject.put("franchisor_id",userOrgList.get(i).getFranchisor_id());
			jsonObject.put("user_name",userOrgList.get(i).getUser_name());
            
            //์œ„์—์„œ ๋งŒ๋“ค์–ด์ง„ JSONObject๋ฅผ Array ๋ฐฐ์—ด์— ์ˆœ์„œ๋Œ€๋กœ Add
			jsonArray.add(jsonObject);
		}
        
        //response ์„ค์ •
		response.setContentType("application/json; charset=utf-8");
		response.getWriter().write(jsonArray.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

 

 

 

 

jsp

ํƒ€์ดํ‹€ ์ž…๋ ฅ๋ถ€๋ถ„

 

<div class="form-group col-6">
<label>๊ฐ€๋งน๋ณธ๋ถ€</label>
<c:if test="${gubun=='0'}">
<select class="form-control" id="franchisor_id" name="franchisor_id" onchange="getOrg(franchisor_id.value)">
    <c:forEach var="userCompanyList" items="${userCompanyList}">
	<option value="${userCompanyList.member_id}">${userCompanyList.user_name}</option>
    </c:forEach>
</select>
</c:if>
<c:if test="${gubun=='2'}">
    <input type="text" class="form-control" value="${member_id}" disabled="disabled">(${user_id})
    <input type="hidden" name="franchisor_id" value="${member_id}">
</c:if>
</div>
<div class="form-group col-6">
<label>๋‹จ๋ง๊ธฐ ์กฐ์ง</label>
<select class="form-control" id="member_id" name="member_id">
    <option>-๊ฐ€๋งน์ -</option>
    <c:forEach var="userOrgList" items="${userOrgList2}">
	<option value="${userOrgList.member_id}">${userOrgList.user_name}</option>
    </c:forEach>
</select>
</div>

 

 

 

javascript

ํƒ€์ดํ‹€ ์ž…๋ ฅ๋ถ€๋ถ„

 

    <script type="text/javascript">
        function getOrg(franchisor_id){
            var user_name;
            $.ajax({
                type: "GET",
                url:'aJaxUserOrgList.do',
                dataType:"json",
                data: {franchisor_id : franchisor_id, user_name : user_name},
                success: function(data){
                    console.log(data);

                    if(data.length>0){
                        $("#member_id").find("option").remove().end().append("<option value=''>-๊ฐ€๋งน์  ๋ชฉ๋ก-</option>");
                        //๋ฐฐ์—ด ๊ฐœ์ˆ˜ ๋งŒํผ option ์ถ”๊ฐ€
                        $.each(data, function(i){
                            $("#member_id").append("<option value='"+data[i].franchisor_id+"'>"+data[i].user_name+"</option>")
                        });
                    }else{
                        $("#member_id").find("option").remove().end().append("<option value=''>-๊ฐ€๋งน์ ์—†์Œ-</option>");
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert("์˜ค๋ฅ˜ ๋ฐœ์ƒ");
                }
            });
        }
    </script>

 

 

 

 

 

 

 

๋ฐ˜์‘ํ˜•
profile

๐Ÿ‘จ‍๐Ÿ’ป kimtongting

@kimtongting ์ž…๋‹ˆ๋‹ค

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!