简体   繁体   English

使用AJAX或jQuery更新HTML表

[英]Using AJAX or jQuery to update an HTML table

From what I have read here on AJAX, it is for updating a page without having to refresh it. 从我已阅读这里的AJAX,它是用于更新页面,而无需刷新。 I have read further , and notice that (at least) the JavaScript part of it has you sending data to another page. 我已进一步阅读,并注意到(至少)它的JavaScript部分已将您发送数据到另一个页面。

From my knowledge and experience on jQuery, it is basically a powerful library for JavaScript. 根据我对jQuery的了解和经验,它基本上是一个功能强大的JavaScript库。

What I am trying to do 我要做什么

I am trying to have buttons that allow users to add and delete from the table. 我正在尝试使用允许用户在表中添加和删除的按钮。 I already know that I am most likely going to be using AJAX for loading the table contents (as it is not just that I am trying to have loaded, and a dropdown menu is going to determine the table that gets displayed). 我已经知道我很可能将使用AJAX加载表内容(因为这不仅是我要加载的内容,而且下拉菜单将确定要显示的表)。 My question is, when the user clicks the buttons to insert/delete table rows themselves, should I have jQuery handle that in-place, or should I send the table to a document via AJAX to be manipulated and sent back? 我的问题是,当用户单击按钮自己插入/删除表格行时,我应该让jQuery就地处理该表格,还是应该通过AJAX将表格发送至文档以进行操作并发回? (I am trying to get that nice no-reload feature on my page.) (我正在尝试在页面上获得不错的无刷新功能。)

If you have just simple button, that removes an row from the table, for example: 如果只有一个简单的按钮,则会从表中删除一行,例如:

<tr id="table_row_id">
  <td>some info</td>
  <td><button class="remove-row">Remove</button></td>
</tr>

Yes you need jQuery listener for click event on that button. 是的,您需要jQuery侦听器用于该按钮上的click事件。 For example: 例如:

// This could easily be a call to .click()
$('.remove-row').on('click', function() {
  var idToRemove = $(this).parent().attr('id');
  $.ajax({
    method: "POST",
    data: {
      'action': 'remove',
      'id': idToRemove
    },
    success: function(response) {
      // here you can check if the element is removed from the server
      // based on the response that the server returns.
      // And if it is, you can remove it from the dom.
      $('#' + idToRemove).remove();
    },
    error: function(error) {
      // Here you can show an user message, "Sorry cannot remove element."
    } //...
  });
});

All you need to realize is that, this is async programming and you just defining callbacks. 您需要意识到的是,这是异步编程,您只需要定义回调即可。 When the server has done the work and the response is received, the callback is called. 服务器完成工作并收到响应后,将调用回调。

So you can reload the table, based on user interact or interval of time. 因此,您可以根据用户交互或时间间隔重新加载表。

I recommend: 我建议:

  • Do not send HTML to the server, just send some json data, Same for the server - Do not respond with HTML or hardly to escape symbols, just respond with a string 'done' or json {"removed": "someID"}. 不发送HTML到服务器,仅发送一些json数据,与服务器相同-不使用HTML响应或几乎不转义符号,仅使用字符串'done'或json {“ removed”:“ someID”}响应。 HTML parsers on different languages are different, you need escaping for the HTML, You will have problems with JSON parsers if you sending json data with HTML inside. 不同语言的HTML解析器有所不同,您需要对HTML进行转义,如果在内部发送带有HTML的JSON数据,则JSON解析器会遇到问题。 So just send JSON with information what exactly you request is trying to do? 因此,仅发送包含信息的JSON就是您的请求到底想做什么? which element you trying to read/create/update/delete? 您尝试读取/创建/更新/删除哪个元素?
  • If you create/update/delete some item from the DB / Server which item has an ID, everytime return the ID back from the server to the client. 如果您从数据库/服务器中创建/更新/删除某个项目,该项目具有ID,则每次将ID从服务器返回给客户端。 Most of times it's better to handle exactly what item you are removed. 在大多数情况下,最好准确地处理要删除的项目。
  • Read about CRUD operations. 阅读有关CRUD操作的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM