0

I am sending a id to controller from dropdown(oncgange function). It is working fine. Now in controller after querying from database result is stored in arrylist. i have given returntype arrylist. is it ok or there is any error. please help. i just want to display another dropdown based selected value of first dropdown. Suppose i selected Fruit , so i want to populate this in jsp.

<option value="mango">Mango</option>
<option value="pineapple">Pineapple</option>

jsp

         <script>
            function getScode()
            {
                var code = document.getElementById('code'),
                    code = code.value;
                jQuery.ajax({
                    type: 'POST',
                    url: 'scode.htm',
                    data: {
                        code: code
                    },
                    success: function (html) {
                        $('#scode_data').html(html).show();
                    }
                });
                return false;
            }
        </script>

          Select Genre
            <select name="code" id="code"  onchange="getScode();">        
            <option value="1">Fruit</option>
            <option value="2">Game</option>              
            </select>
          Under that
         <div id="scode_data"></div>

controller

@RequestMapping(value="/scode", method = RequestMethod.POST)
    public @ResponseBody ArrayList getScode(ModelMap modelMap, @RequestParam(value = "code", required = false) String code_val, HttpServletRequest request, HttpServletResponse response) 
throws ClassNotFoundException { 

        SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource = new dbconnection.connection().getConnection(dataSource);
        JdbcTemplate jt = new JdbcTemplate(dataSource);

        String sql = "SELECT * FROM cat WHERE fcode='"+code_val+"'";
        srs = jt.queryForRowSet(sql);
        while (srs.next()) {
            pojo obj2 = new pojo();
            obj2.setScode(srs.getString("fcode"));
            obj2.setSname(srs.getString("fname"));
            arraylist.add(obj2);
        }

         return arraylist;
    }

Image link of what i am getting

Suraj Roy
  • 51
  • 2
  • 12
  • you'll have send the json version of the arraylist. And in the success function parse that json which you have received and append it to the another – Pallav Jha Mar 11 '16 at 09:51
  • i am now able to get data via ajax. But it shows ... arraylist data like .... ** model.pojo@14e79b55model.pojo@46162020 ** – Suraj Roy Mar 11 '16 at 12:01
  • Check what are you getting on success in the browser console. `success: function (html) { console.log(html); $('#scode_data').html(html).show(); }` You should be getting array of object. Post the screenshot of that. – Sanjay Rawat Mar 11 '16 at 20:22
  • 1
    @SurajRoy As you have not converted the list to json you're getting the toString representation of the same. http://stackoverflow.com/questions/20801248/convert-arraylist-into-json This will help you – Pallav Jha Mar 12 '16 at 15:36
  • @SanjayRawat can not post screenshot ,,not have enough score – Suraj Roy Mar 14 '16 at 06:06
  • @SanjayRawat added an image link – Suraj Roy Mar 14 '16 at 06:09
  • As Obi Wan said, you haven't converted list to json. Try that and if you still have issue I'll try to answer. – Sanjay Rawat Mar 14 '16 at 06:17
  • fine,, now it is doing some work ,, now its showing this = [{"scode":"1","sname":"mango"},{"scode":"2","sname":"pineapple"}] @SanjayRawat – Suraj Roy Mar 14 '16 at 06:41
  • i want to show this = @SanjayRawat – Suraj Roy Mar 14 '16 at 06:45
  • fine,, now it is doing some work ,, now its showing this = [{"scode":"1","sname":"mango"},{"scode":"2","sname":"pineapple"}] i want to show this = @ObiWan-PallavJha – Suraj Roy Mar 14 '16 at 07:04

1 Answers1

0

Now you've to convert the json string into option nodes. Below code would do the work:

success : function(result) {
    var parsed = JSON.parse(result);

            var b = "";
            for(var x = 0; x<parsed.length ; x++){
                b = "<option value = '"+parsed["scode"]>+"'>"
                b = parsed["sname"] + "</option>";
                $("#__id of your select_").append(b);
                b = "";
            }
Pallav Jha
  • 3,409
  • 3
  • 29
  • 52