Pass a whole html table back to server and replace the database table?
I met a problem like this. Here is the scenario: if I have a database table A, which contains several columns, for example. O开发者_StackOverflow中文版nce the server (PHP script) pass the database to web client, it's rendered into a HTML table. Now, here is some requirements for the client side. The user will be able to add/delete several rows from the HTML table, in addition, they will also be able the change the row order. If we could finish the client side with the help from some jquery plugin (for example: http://www.isocra.com/2008/02/table-drag-and-drop-jquery-plugin/). Now, my question is, how could I pass this information back to server? Because there are new rows and deleted rows, the original table row order (like the plugin tableDnD.serialize function is not sufficient) is not enough.
Here is my solution: pass the whole HTML table back to server, and then replace the whole database table with the information. In that case, how to pass a potentially big HTML table back to server? For this question, I found the answer like this: Iterate through HTML table using jQuery, converting the data in the table into JSON Basically,convert the whole HTML table into json format and pass it to server side to replace the whole table.
Is there a better solution than that? Any suggestions would be appreciated!
IMHO, if you are building a JavaScript-only solution, there is no need to pass the whole HTML table back to the server.
- When loading the table initially, set the
id
attribute to the unique identifier of the row from the SQL table. - When changing data (remove or add a row or change row contents), save the ids into an array.
- When submitting data once changed, submit only the rows which were changed.
Of course, you may need to tweak this a bit, but it's just a general idea. For example, one tweak to do is to add the ids of removed rows to a separate array. You can then pass it separately to remove the matching rows from the database.
精彩评论