Akanksha
Akanksha

Reputation: 25

Edit button on a dynamic table in jsp servlet application

I am working on a JSP Servlet project in which I have created a table on the JSP page. This table is getting populated from an arraylist that is the resultset of a query. So I am basically running a query, storing into an arraylist and displaying the values from that arraylist. How should I edit the fields in the table such that I can delete all information about that field from the table and create a new entry entered by the user.

The code for JSP page to run the query and display the table is as follows:

//run the query
try{
    out.println("<html>");
    out.println("<table id='tbl'class= 'cls' style='display;' border=''><tr><th>Tag</th><th>Severity</th><th>Threshold</th><th>ID</th><th id= 'del'>Delete</th> </tr>");
    Iterator<String> li = alarmarr.iterator();   
    int flagy=1;
    int temp = 1;

    for (int f=0; f<=count; f++){
        out.println("<tr>");
        while(li.hasNext() && temp <=numberOfColumns){
            temp++;
            String temporaryvalue = (String)li.next();
            out.println("<td><input type='text' value='"+temporaryvalue+"' /></td>");

            if(temp ==5 && flagy==1){
                out.println("<td> <a href='editalarm?id="+temporaryvalue+"'>Edit</a></td>");
                out.println("<td id = 'delalarms' > <a  id='delalarms' href='deletealarm?id="+temporaryvalue+"'>Delete</a></td>");
                flagy = 0;  
            }
        }
        temp = 1;
        flagy = 1;
        out.println("</tr>");

    }
    out.println("</table></div>");
    out.println("</center></div></body></html>"); 

}
catch(Exception e)
{
    System.out.println(e);
}

Upvotes: 2

Views: 1647

Answers (2)

Vinoth Krishnan
Vinoth Krishnan

Reputation: 2949

Try Ajax folk,

function myFunction() { 

  if(confirm("Are you sure you want to delete this?")){
      var activeRecord= '${id}';  //or get id from click function

     //Make sure it is having the value here.
     //alert(activeRecord); or console.log(activeRecord);

     $.ajax({
       type: "POST",
       url: "servletURL",
       data: {"id": activeRecord},   
       dataType: 'json',
       success: function(data){
           // use data for populate form
       }
     }); 
   }
   else{
      return false;
   }
} 

Upvotes: 0

Liam
Liam

Reputation: 2837

You should update the alarmarr and re-render the table. For example to add a new table row you need to add the item to the alarmarr object while to delete a row you need to remove from the list.

Also I am not a huge fan of using JSPs scriptlets to render HTML. Please use JSTL atleast.

Upvotes: 1

Related Questions