简体   繁体   English

使用javascript从动态html表保存数据

[英]Saving data from a dynamic html table using javascript

I load data from a mysql table into a HTML table. 我将数据从mysql表加载到HTML表中。 Then the user can add or remove one or more rows. 然后,用户可以添加或删除一个或多个行。 Finally I have to save the changes into the database. 最后,我必须将更改保存到数据库中。 All this have to be done using PHP and javascript. 所有这些都必须使用PHP和javascript完成。

I know this does not seem very hard. 我知道这似乎并不难。 In fact, I managed to load data from database into the HTML table and add or remove rows from it, using javascript function but I'm having the hardest time trying to save changes from the table. 实际上,我设法使用javascript函数将数据库中的数据加载到HTML表中,并从中添加或删除行,但是我最困难的时间试图从表中保存更改。 How can I delete an specific row from the table, considering that these rows don't have id's since they are dynamically added? 考虑到这些行没有ID,因为它们是动态添加的,因此如何删除表中的特定行? How can I access data from an specific row? 如何访问特定行中的数据?

I hope you guys can help me out. 我希望你们能帮助我。 This is driving me nuts! 这真让我发疯! Thank you and sorry for my English. 谢谢你,抱歉我的英语。

If you are adding them dynamically can't you give the rows id's? 如果要动态添加它们,是否不能提供行ID? Or maybe iterate through the table with javascript? 还是用JavaScript遍历表格?

var theTable = document.getElementById("theTableId");
for (var i=0, row; row=table.rows[i]; i++) {
          // Remove if deleted
}

If you are not developing for mobile or embedded platforms, you may want to considering leverage some well-developed libraries, such as DataTables and jEditable . 如果您不是针对移动平台或嵌入式平台开发的,则可能要考虑利用一些成熟的库,例如DataTablesjEditable

There is an example from DataTables , which uses jEditable to allow inline editing for the table. 一个来自DataTables的示例 ,该示例使用jEditable允许对该表进行内联编辑。 That might be something close to what you want. 那可能接近您想要的东西。

The DataTables library allows your source to be from a number of different places, eg DOM, AJAX, JavaScript arrays or server-side. DataTables库允许您的源来自许多不同的位置,例如DOM,AJAX,JavaScript数组或服务器端。 Have a look at the examples on their website . 看看他们网站上的例子

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

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