简体   繁体   English

动态HTML表行已添加,填充并插入到数据库中

[英]Dynamic HTML Table rows added and filled and inserted in to database

I needed a solution , to implement adding dynamic HTML table rows and filling them with details , the rows should then be added to database on submit. 我需要一个解决方案,以实现添加动态HTML表行并用明细填充它们,然后在提交时将这些行添加到数据库中。 what is the best way to implement this in jsp.The jsp page on load get the data from the database and creates rows, i can edit the row values and it should be updated,also new rows can be inserted in front end page, which should along with updated rows (retrieved from database ),be saved in jsp. 在jsp上实现此功能的最佳方法是什么。加载时的jsp页面从数据库中获取数据并创建行,我可以编辑行值并进行更新,也可以在前端页中插入新行,应该与更新的行(从数据库检索到的)一起保存在jsp中。

  1. Store the data in the table in a javascript multi-dimensional Array object that has one extra column. 将数据存储在具有一个额外列的javascript多维Array对象中的表中。 ex., if the table is 2x2, create a dynamic arrray of size 2x3 (new Array(2,3)) 例如,如果表为2x2,则创建大小为2x3的动态数组(新Array(2,3))

  2. use the first cell of every row of the array to store the primary key of each row of the data in the table and the remaining cells of the row to store the actual data as in the html table. 使用数组每一行的第一个单元格来存储表中数据的每一行的主键,并使用该行的其余单元格来存储html表中的实际数据。

  3. when a new row is added to the html table, or any data is updated, update this javascript array accordingly. 当将新行添加到html表或更新任何数据时,请相应地更新此javascript数组。 leave the first column of newly created rows blank to indicate a new row (or fill it with a dummy value to indicate an empty row) 将新创建的行的第一列留空以指示新行(或用虚拟值填充以指示空行)

  4. Submit this data in the Array to the server code processing it when the submit button is clicked. 单击提交按钮后,将数组中的数据提交到处理它的服务器代码。 This can be done in a variety of ways (appending to the query string, creating hidden variables, etc. ) 这可以通过多种方式完成(附加到查询字符串,创建隐藏变量等)。

  5. on the server-side, process this data - update the records that have the primary key and create new records for the ones without a key. 在服务器端,处理此数据-更新具有主键的记录,并为不具有键的记录创建新记录。

  1. client side javascript. 客户端javascript。 functions like add row, remove row, move row up, move row down as required. 如添加行,删除行,上移行,下移行等功能。 Elsewhere on SO 在其他地方
  2. remember to name the controls that are added uniquely, like name1, name2, name3... Optionally store count of rows in some hidden field which gets updated and submitted along with the form. 记住要命名唯一添加的控件的名称,例如name1,name2,name3...。可以选择将行数存储在某个隐藏字段中,该字段随表格一起更新和提交。
  3. on the server side use that counter or otherwise loop through all the request variables to find name*, and insert name*, age*, gender* etc. to the database in the loop. 在服务器端,使用该计数器或以其他方式循环遍历所有请求变量以查找name *,然后在循环中将name *,age *,gender *等插入数据库中。

Unless there is a requirement to allow the user to POST the entire table in one go and be done with it (a "save all changes and close" action), I would suggest that you just update the database on the go (the "ajaxy" way): 1. When adding a row or editing a row, immediately send the row content to the server using an asynchronous javascript call for "update a single row" - this should be fairly easy to handle (see example idea below). 除非有必要允许用户一次性过帐整个表并完成该表(“保存所有更改并关闭”操作),否则我建议您只是在旅途中更新数据库(“ ajaxy”)。 “方式”:1.添加行或编辑行时,立即使用异步JavaScript调用将行内容发送到服务器,以“更新单行”-这应该很容易处理(请参见下面的示例提示)。 2. When a new row is to be added, the server should return the new row id for the client to store, so that future updates can address that row. 2.当要添加新行时,服务器应返回新行ID供客户端​​存储,以便将来的更新可以解决该行。

Implement a "save and close" does not entirely contradict this approach: you can have your "save and close" action just wait until all pending updates are committed - have a count that increment for when you start sending a request, and decrement when a response is received. 实施“保存并关闭”并不完全与这种方法相矛盾:您可以让“保存并关闭”操作仅等到所有未决的更新都提交后才开始-在开始发送请求时计数增加,而在发送请求时计数递减收到响应。 When it gets to zero, your "save" part has completed and you can close the editor. 当它为零时,“保存”部分已经完成,您可以关闭编辑器。

One way to implement this will be to attach a form to an invisible cell in each row. 一种实现方法是将表格附加到每一行中的不可见单元格上。 This form will have a hidden input element for each data item on the row. 该表单将为该行上的每个数据项提供一个隐藏的输入元素。 When the user changes values on the row, mark the row as "dirty" and a short timeout for each row (that gets reset whenever the user edits the same row) will cause the form to be submitted to an iframe. 当用户更改该行上的值时,将该行标记为“脏”,并且每行的超时时间较短(只要用户编辑同一行,该超时就会重置)将导致表单提交至iframe。 If you want to implement the "Save and close" action, just count the dirty rows. 如果要实现“保存并关闭”操作,只需计算脏行。

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

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