【问题标题】:Edit button on a dynamic table in jsp servlet applicationjsp servlet应用程序中动态表上的编辑按钮
【发布时间】:2015-10-18 08:56:10
【问题描述】:

我正在处理一个 JSP Servlet 项目,在该项目中我在 JSP 页面上创建了一个表。该表是从作为查询结果集的数组列表中填充的。所以我基本上是在运行一个查询,存储到一个数组列表中并显示该数组列表中的值。我应该如何编辑表中的字段,以便我可以从表中删除有关该字段的所有信息并创建用户输入的新条目。

JSP页面运行查询并显示表格的代码如下:

//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);
}

【问题讨论】:

  • 另外,我将 id 值发送到 deletealarm servlet 的方式是通过“href='deletealarm?id”发送它。我应该如何弹出删除链接,仍然可以将 id 发送到 servlet。

标签: java jsp servlets arraylist


【解决方案1】:

您应该更新alarmarr 并重新渲染表格。例如,要添加新的表格行,您需要将项目添加到 alarmarr 对象,而要删除需要从列表中删除的行。

另外,我不太喜欢使用 JSP 脚本来呈现 HTML。请至少使用 JSTL。

【讨论】:

    【解决方案2】:

    试试Ajaxfolk,

    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;
       }
    } 
    

    【讨论】:

    • 感谢您的解决方案,但在单击取消时,我的条目被删除。我认为 return false 不起作用。
    • 你必须使用像onclick="return myFunction(id)"这样的函数。因此它将重新调整该值。
    猜你喜欢
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多