简体   繁体   English

如何使用JS,HTML,PHP创建动态“添加数据集”功能?

[英]How to make a dynamic “Add Dataset” functionality using JS, HTML, PHP?

Use Case 用例

A user needs to enter several sets of data. 用户需要输入多组数据。 Each data is a set of 6 numbers. 每个数据是一组6个数字。 Typically there are 10 to 15 such sets but can be up to 40. User needs ability to add new sets dynamically or specify number of sets ahead of time. 通常有10到15个这样的集合,但最多可以达到40个。用户需要能够动态添加新集合或提前指定集合数量。 Also needs ability to edit data in sets afterwards, remove some sets and possibly add some sets. 还需要能够在之后编辑集合中的数据,删除一些集合并可能添加一些集合。 And ability to save the entire data to server. 并能够将整个数据保存到服务器。 In other words, dynamic excel-like functionality but on the web. 换句话说,动态类似于Excel的功能,但在网络上。

I say set to indicate that it can be a row or a column of 6 numbers. 我说设置表示它可以是6行数的行或列。

Question

Is there an existing module I can use or is there anything I can implement to make it easier? 是否有我可以使用的现有模块,或者我可以实现哪些模块以使其更容易? My goals are 我的目标是

  • keep HTML simple 保持HTML简单
  • use native web stack implementation whenever possible (GET, POST, FORM, DIV, TABLE) 尽可能使用本机Web堆栈实现(GET,POST,FORM,DIV,TABLE)
  • but use the tools available to web developers to make life easy (JS, JQUERY, SESSION, AJAX, PHP back-end data generation, etc) 但是使用Web开发人员可用的工具来简化生活(JS,JQUERY,SESSION,AJAX,PHP后端数据生成等)

What I have tried: 我尝试过的:

I've decided to use columns for each set of data but that got me into some trouble -- I have to use HTML tricks like tabindex to keep form navigation consistent. 我已决定为每组数据使用列,但这让我遇到了麻烦 - 我必须使用像tabindex这样的HTML技巧来保持表单导航的一致性。 I have no way to add new sets, I am not sure how to delete sets. 我无法添加新的设置,我不知道如何删除设置。 I can certainly figure that out eventually but I think I have started off in the wrong direction. 我当然可以最终解决这个问题,但我认为我已经开始走错了方向。 I would like to back up and design a different interface one that allows me to keep things simple while reaching the use case goals above. 我想备份并设计一个不同的界面,让我在实现上述用例目标的同时保持简单。 My try is below.. 我的尝试在下面..

 //some JS functionality to keep in mind as an example var form = document.forms.my_form, elem = form.elements; elem.my_button.onclick = function() { alert('Selected Column: ' + elem.design.value); }; 
 <form name="my_form"> <table> <tbody> <tr> <th>Condition</th> <th></th> <th>Case 1</th> <th>Case 2</th> <th>Case 3</th> <th>Case 4</th> </tr> <tr> <td>Quantity 1</td> <td class="c">-</td> <td class="c"> <input tabindex="1" name="case[]" size="3" type="text"> </td> <td class="c"> <input tabindex="7" name="case[]" size="3" type="text"> </td> <td class="c"> <input tabindex="13" name="case[]" size="3" type="text"> </td> <td class="c"> <input tabindex="19" name="case[]" size="3" type="text"> </td> </tr> <tr> <td>Pressure</td> <td class="c">-</td> <td class="c"> <input tabindex="2" name="case[]" size="3" type="text"> </td> <td class="c"> <input tabindex="8" name="case[]" size="3" type="text"> </td> <td class="c"> <input tabindex="14" name="case[]" size="3" type="text"> </td> <td class="c"> <input tabindex="20" name="case[]" size="3" type="text"> </td> </tr> <tr> <td>Vacuum</td> <td class="c">-</td> <td class="c"> <input tabindex="3" name="case[]" size="3" type="text"> </td> <td class="c"> <input tabindex="9" name="case[]" size="3" type="text"> </td> <td class="c"> <input tabindex="15" name="case[]" size="3" type="text"> </td> <td class="c"> <input tabindex="21" name="case[]" size="3" type="text"> </td> </tr> <tr> <td>Quantity 4</td> <td class="c">-</td> <td class="c"> <input tabindex="4" name="case[]" size="3" type="text"> </td> <td class="c"> <input tabindex="10" name="case[]" size="3" type="text"> </td> <td class="c"> <input tabindex="16" name="case[]" size="3" type="text"> </td> <td class="c"> <input tabindex="22" name="case[]" size="3" type="text"> </td> </tr> <tr> <td>Temp</td> <td class="c">-</td> <td class="c"> <input tabindex="5" name="case[]" size="3" type="text"> </td> <td class="c"> <input tabindex="11" name="case[]" size="3" type="text"> </td> <td class="c"> <input tabindex="17" name="case[]" size="3" type="text"> </td> <td class="c"> <input tabindex="23" name="case[]" size="3" type="text"> </td> </tr> <tr> <td>Solids</td> <td class="c">-</td> <td class="c"> <input tabindex="6" name="case[]" size="3" type="text"> </td> <td class="c"> <input tabindex="12" name="case[]" size="3" type="text"> </td> <td class="c"> <input tabindex="18" name="case[]" size="3" type="text"> </td> <td class="c"> <input tabindex="24" name="case[]" size="3" type="text"> </td> </tr> <tr> <td></td> <td>Main</td> <td class="c"> <input name="design" value="1" checked="checked" type="radio"> </td> <td class="c"> <input name="design" value="2" type="radio"> </td> <td class="c"> <input name="design" value="3" type="radio"> </td> <td class="c"> <input name="design" value="4" type="radio"> </td> </tr> <tr> <td colspan="6"> <input type="button" name="my_button" value="get column number" /> </td> </tr> </tbody> </table> </form> 

Problems with my current direction 我目前的方向存在问题

  1. can't add new sets 无法添加新集
  2. deleting a set is difficult 删除一组很难

And while it can be done the manipulations needed to change table to make above work are just not feasible (will make things too complicated) and so I seek new direction. 虽然可以做到改变表格以进行上述工作所需的操作是不可行的(会使事情变得太复杂),因此我寻求新的方向。 One that will make it simple on the front end, and on the back end. 一个可以简化前端和后端的一个。

What I mean by simple HTML is the end result (after multiple add/delete/edit operations on the sets) should be a simple form that can be submitted via standard HTML or JS mechanisms. 我的意思是简单的HTML是最终结果(在集合上多次添加/删除/编辑操作之后)应该是一个可以通过标准HTML或JS机制提交的简单form And that form must be kept consistent between the operations as well. 并且该形式也必须在操作之间保持一致。

I would do the following: 我会做以下事情:

Design: I would use <div> elements to create your table. 设计:我会使用<div>元素来创建你的表。 table structure is visually nice for such tables, but they are hard to work with when it comes to dynamic actions and responsiveness. table结构对于这样的表在视觉上很好,但是当涉及到动态操作和响应时,它们很难处理。

Actions: 操作:

  • Add - to add new sets, I would add a link to the top of your table that would say something like "add set", which when clicked(in Javascript of course), you will count the current sets, get the last number, and create your input in a manner where the name has an array with the key that matches the current set like so: <input tabindex="1" name="case['+ count +']" size="3" type="text"> where count is the total number of your current sets plus 1. 添加 - 添加新集,我会在表格的顶部添加一个链接,例如“添加集”,当点击时(当然是在Javascript中),您将计算当前集,获取最后一个数字,并以一种方式创建input ,其中名称具有与当前集匹配的键的数组,如下所示: <input tabindex="1" name="case['+ count +']" size="3" type="text">其中count是当前集合的总数加1。

  • Delete - every set would have a delete icon underneath every column which will set a flag to have it deleted from the database when submitted. 删除 - 每个列的每个列下面都有一个删除图标,它将设置一个标志,以便在提交时从数据库中删除它。 Visually I would gray out the set, to show that it is "marked for deletion" (this action should be able to be undone). 在视觉上,我会将该组灰显,以显示它“标记为删除”(此操作应该可以撤消)。

  • Submission - when you are ready to submit, you may do this either through AJAX or just simply through a POST request. 提交 - 当您准备提交时,您可以通过AJAX或仅通过POST请求来执行此操作。 Your post data would have any new sets or old sets with the new information. 您的帖子数据将包含任何新集或旧集与新信息。 Each set would have a flag that either says update , eidt , new .. you can then use these flags to take the proper action in your backend. 每个集合都有一个标志,表示updateeidtnew ..然后你可以使用这些标志在你的后端采取适当的行动。

Hope this helps. 希望这可以帮助。

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

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