简体   繁体   English

重新加载表格而不重新加载网页

[英]Reloading a table without reloading the webpage

I currently have a table that you can add / delete / remove all the items from but in order to actually see that you have removed an item or cleared the table I need to reload the webpage. 我目前有一个表,可以从中添加/删除/删除所有项目,但是为了真正看到已删除项目或清除了该表,我需要重新加载网页。 I was wondering if there was another way of just reloading the table instead of reloading the webpage 我想知道是否还有另一种方法就是重新加载表格而不是重新加载网页

You would have to use AJAX to get data back from the server. 您将不得不使用AJAX从服务器获取数据。

In your server, you would have a page that serves json containing relevant data, and you will parse it and update the page. 在您的服务器中,您将有一个页面,其中包含包含相关数据的json ,然后您将对其进行解析并更新该页面。

So for example, you could route mytable/update to a method in your server, and you would call it like so: 因此,例如,您可以将mytable/update路由到服务器中的方法,并按如下方式调用它:

jQuery.ajax('/mytable/update)

The callback would have the actual json . 回调将具有实际的json Be wary of CSRF 警惕CSRF

However, you mentioned that your table only has an add and delete. 但是,您提到您的表仅具有添加和删除。 Is there data coming from the server? 服务器中是否有数据? You might be able to replicate your code from the server in the client itself so you could just make the changes themselves with javascript, which would help reduce server load. 您也许可以从客户端本身的服务器中复制代码,以便仅使用javascript自己进行更改,这将有助于减少服务器负载。

For deleting a row: 对于删除行:

 $('#myTableRow').remove();

This works fine if your row has an id, such as: 如果您的行具有一个ID,例如:

<tr id="myTableRow"><td>blah</td></tr>

For adding : 对于添加:

$('#myTable > tbody:last').append('<tr>...</tr><tr>...</tr>');

And assuming tat your item is in a row :) 并假设您的物品是连续的:)

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

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