출처: http://november11tech.tistory.com/88 [Mr.november11] json으로 받아온 값 select 박스에 add 하기 :: 사월은 봄이다.

일단, 자바 쪽 코드


String selectedData = request.getParameter("selectedData");

  

  HashMap<String, Object> map = new HashMap<String, Object>();

  map.put("selectedData", selectedData);

  

  List<ChildDataVO> childList = new ArrayList<ChildDataVO>() ;

  childList = homeSrv.getChildDataList(map);

  

  HashMap<String, Object> mapData = new HashMap<String, Object>();

  

  mapData.put("childList", childList);

  

  JSONObject jso = new JSONObject();

  jso.put("result", mapData);

  

  try {

   response.setContentType("application/json");

   response.setCharacterEncoding("UTF-8"); // 이 부분은 jsp 에서 한글이 깨져서 넣어주었다.

   PrintWriter out = response.getWriter();

   out.println(jso);

   out.close();

  } catch (IOException e) {

   e.printStackTrace();

  }



List로 DB 에서 받아온 값들을 HashMap<String, Object> 에 넣는다.

그리고 이걸 json Object에 넣어!

그러면 이 값이 화면 단으로 넘어가겠지 ?




이제, 화면단 소스(js)

$J.ajax({

       type: "post",

       url: url,

       data : 'selectedData='+param

       ,contentType: "application/x-www-form-urlencoded; charset=UTF-8"

       ,success : function(data){

        console.log('sucess');

        $J("#childDataList").find('option').remove();

        

        var result  = new Array();

        result = data.result.childList ;

        console.log(result[0].dataId);

        $J.each(result, function(i,result){

         $J('#childDataList').append($J("<option/>", { value: result.dataId, text: result.dataName }));

        });

       }

      }).done(function(data){

       

      });


보면 list로 넘어온 object를 스크립트에서 array를 선언해 넣는다.

그 뒤. .each() 함수를 이용했는데, 아직 잘 이해는 안간다.

저기서 value 값엔 data의 id를 text엔 name을 넣은 것이다.



헷갈려....=ㅂ=


'개발 > JQuery' 카테고리의 다른 글

iframe으로 삽입한 화면에서 부모에 접근하기  (0) 2018.02.13
ajax, post, get  (0) 2018.02.13
jquery function  (0) 2018.02.13
siblings  (0) 2018.02.13
selectbox  (0) 2018.02.13

+ Recent posts