0

I'm trying to show the data from postgresql to slickgrid. However, in javascript for loop, I can't retrieve right values from JSP expression. How can I do it?

conn = dataSource.getConnection();
        stmt = conn.createStatement();
        rs  = stmt.executeQuery("SELECT host_name,username,key_value,updated from pass_table");
        while(rs.next()) {
            hostname.add(rs.getString(1));  
        } 
    } catch (Exception e) {
        out.println("<br /><font color='red'><strong>SQL Exception: " + e + "</strong></font><br/>");
        }
        %>


      $(function () {
       var data = [];
        for (var i = 0; i < 2; i++) {
          data[i] = {
            //title: "Task " + i
            title: "<%= hostname.get(i)%>"  <===this part
            //title: "${row.host_name}" + i
         };
        }


        grid = new Slick.Grid("#myGrid", data, columns, options);
      })
    </script>
James
  • 305
  • 8
  • 19
  • this question is possible duplicate check out this [jsp-js](http://stackoverflow.com/questions/4803906/reading-a-jsp-variable-from-javascript) i think you have the problem with `i` in this `<%= hostname.get(i)%>` code. – Hashy Nov 02 '15 at 06:46

2 Answers2

1

try this ,

$(function () {
    var data =[]; 
     <%
        for(int i = 0; i < 2; i++) {
        %>
       data[<%=i%>] ={
              title:"<%= hostname.get(i)%>"
        <%
        };
        %>
     };

     grid = new Slick.Grid("#myGrid", data, columns, options);
          })
Hashy
  • 274
  • 2
  • 10
  • this error below occured when I executed it An error occurred at line: 107 in the jsp file: /SlickGrid-master/examples/example1-simple.jsp i cannot be resolved to a type 104: <% 105: for(int i = 0; i < 2; i++) { 106: %> 107: data[<%i%>] ={ 108: title:"<%= hostname.get(i)%>" 109: <% 110: } – James Nov 02 '15 at 07:22
  • @Hyunseung sorry I forgot equal sign and semicolon. – Hashy Nov 02 '15 at 12:12
0

I replaced code as below and it works. Thank you so much Hashy!!

$(function () {
    var data =[]; 
     <%
        for(int i = 0; i < 2; i++) {
        %>
       data[<%=i%>] ={title:"<%= hostname.get(i)%>"};
     <%
     };
%>
     grid = new Slick.Grid("#myGrid", data, columns, options);
          })
James
  • 305
  • 8
  • 19