0

我有从 CGI 应用程序填充的 html 中的表数据行。我希望每一行旁边都有一个复选框,这样我就可以删除多行,就像在 gmail 中一样。

我想出了基本的文本表单,并能够将其发送到 CGI 程序以删除该行,但我不想一次输入行名称来删除单个文件。

当您可以通过复选框选择多个删除时,表单两侧(html-browser 和 C-CGI 应用程序)的代码是什么样的?某处有例子吗?(我仅限于 JS 和 HTML,但我认为 JS 无论如何都是用于验证,现在不需要。CGI 应用程序端的 C 编码。)

谢谢你。

4

2 回答 2

2

查看“AJAX”风格的 javascript。当您向服务器发出 AJAX 请求时,传递所有删除。服务器端应编码为在单个请求中接受多个删除。

于 2009-01-10T01:54:57.313 回答
2

好吧,您可以通过以下几种方式做到这一点:

1) 使所有元素具有相同的形式。将每个复选框命名为相同,但为每个复选框指定一个值,以区分它所代表的记录/ID/文件。当浏览器(如果兼容)提交表单时,CGI 应用程序应该能够将 HTTP 参数视为 POST 或 GET 提交的一部分。许多像 PHP 这样的 CGI 应用程序将同名参数组合到一个数组中。您也可以随时使用 C 自己遍历参数列表。

// Client side html
<table>
<form>
<tr><td><input type="checkbox" name="id" value="1"/></td><td>Row 1</td></tr>
<tr><td><input type="checkbox" name="id" value="2"/></td><td>Row 2</td></tr>
<tr><td><input type="checkbox" name="id" value="3"/></td><td>Row 3</td></tr>
<tr><td><input type="checkbox" name="id" value="4"/></td><td>Row 4</td></tr>
</form>
</table>

// Server side CGI, using pseudo-code
String[] ids = request.getArrayOfParametersNamed("id");
if(!empty(ids)) {
 for(id in ids) {
   DatabaseControllerModelThingWhatever.deleteById(id);
 }

 // Actually if SQL based you should use a batch statement instead of 
 // one-at-a-time deletes like above
}

// Ok the rows are deleted, either print out the page, or better yet,
// send a redirect so that a user-refresh does not try and re-delete 
// already deleted stuff and also give the user a wierd "resubmit form" warning
// Done

2) 使用 AJAX,最好是某种类型的 Javascript 库,当用户单击删除时,执行基于 ajax 的提交,提交删除检查记录的请求。同时使用 Javascript 从 HTML 表中删除行。这意味着用户的页面永远不会完全刷新,嗯,有点。

 // Client side HTML is same as before, only this time there is a DELETE button with
 // an onclick handler. Also, add a "class" or "id" to each "tr" so we can find it 
 // in the HTML table

 // Pseudo-javascript because I am lazy
 function onDeleteButtonClick() {

  // Get our ids
  var idElements = document.getElementsById("id");

  // Submit an async AJAX request (e.g. use Jquery and send ids as URL params)
  ajaxedDeleteSubmission(idElements);

  // Delete all the rows that should not be there
  for(i = 0; i < tablex.rows.length; i++) {
   // Grab the value of the "id" attribute of each table row (<tr id="?">...</tr>)
   id = tablex.rows[id].id;
   if(id in ids) {
    // Remove the row, forget how because now I just use Jquery.
    tablex.deleteRow(i);
   }
  }
 }
于 2009-01-10T02:03:18.950 回答