简体   繁体   English

在PHP和Javascript之间进行通信

[英]Communicate between PHP and Javascript

I have a table in database that has 2 columns Name | 我在数据库中有一个具有2列的表。 Age, I display it in a HTML page. 年龄,我将其显示在HTML页面中。

I want to sort the table in HTML page based on a field when the user clicks on it. 当用户单击它时,我想基于字段对HTML页面中的表进行排序。

I have a PHP function to do the sorting based on a field. 我有一个PHP函数可以根据字段进行排序。

But after obtaining the rows in sorted order in PHP, I'm looking for ways by which I can update the HTML table without navigating away from the page. 但是在PHP中按排序顺序获取行之后,我正在寻找无需离开页面即可更新HTML表的方法。

You do not need to communicate between the client and server to do this, just sort the table on the client directly. 您不需要在客户端和服务器之间进行通信即可,只需直接在客户端上对表进行排序。

There is a jQuery plug-in for this that works quite well: 有一个jQuery插件可以很好地工作:

http://tablesorter.com/docs/ http://tablesorter.com/docs/

Here is a link to a javascript library to make your tables sortable using javascript instead of php. 这是指向javascript库的链接,以使您的表可以使用javascript而不是php进行排序。 I've used it many times, it works great. 我已经使用了很多次,效果很好。

Javascript Sortable Tables by: Stuart Langridge Javascript可排序表,作者:Stuart Langridge

Since others have covered the fact that client-side sorting would work just fine here, I'll just point you to the resource with which I've had the most sucess doing this kind of thing: Google Data Tables , part of their Visualization Library . 由于其他人已经了解了客户端排序在这里可以很好地工作的事实,因此,我仅向您指出我最成功地使用这种资源的资源: Google数据表 ,它们的可视化库的一部分。 Here are the deets on what you can do (spoiler: everything you want and more). 这是您可以做什么的代表(破坏者:您想要的一切以及更多)。

You can do sorting in javascript, without having to communicate with the server. 您可以使用javascript进行排序,而无需与服务器进行通信。 For example, this code will sort a table based on the content of the Nth column: 例如,此代码将根据第N列的内容对表进行排序:

function sortTable(table, column, skipHeader) {
  // Stick each row into an array.
  var rows = [];
  for (var i = skipHeader ? 1 : 0; i < table.rows.length; i++) {
    rows.push(table.rows[i]);
  }

  // Sort the array based on the innerText of the column'th cell in each row
  rows.sort(function(a, b){ 
    a = a.cells[column].innerText;
    b = b.cells[column].innerText;
    return a < b ? -1 : (b < a ? 1 : 0);
  });

  // Re-order the rows by removing/appending in the sort order
  for (var i = 0; i < rows.length; i++) {
    var row = rows[i];
    var container = row.parentElement;
    container.removeChild(row);
    container.appendChild(row);
  }
}

For example, to sort the first table in the document, on the first column, and skip the header row: 例如,要对文档中的第一张表进行排序,请在第一列上跳过标题行:

sortTable(document.getElementsByTagName('table')[0], 0, true);

Obviously you'll want to modify this to suit your own tastes, especially the sorting, but it's a lot simpler than having to post the data back to the server, which I think is what you're proposing. 显然,您需要修改此设置以适合您自己的口味,尤其是排序,但是比将数据发布回服务器要简单得多,我认为这是您的建议。

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

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