简体   繁体   English

从javascript数组中添加/创建/删除项目,想法?

[英]Appending/creating/removing items from a javascript array, ideas?

I have a requirement whereby I have two panes on a page, the pane on the left holds a series of records specific to a option selected from a drop down. 我有一个要求,即我在一个页面上有两个窗格,左边的窗格包含一系列特定于从下拉列表中选择的选项的记录。 Each record has a plus sign next to it, if this is pressed it will be 'moved' to the right hand pane and displayed under the option the user selected. 每个记录旁边都有一个加号,如果按下该记录,它将被“移动”到右侧窗格中,并显示在用户选择的选项下。

Multiple records can be put into each option selected. 可以将多个记录放入所选的每个选项中。

I'm a bit unsure of the best approach to go with this. 我不太确定与此相关的最佳方法。 At first I was thinking about creating an array in Javascript and each click of plus would add the item to the array. 最初,我正在考虑使用Javascript创建数组,每次单击加号都会将该项目添加到数组中。 When the form is ready to be submitted, use jQuery/Ajax to pass the array to a php function. 准备好提交表单时,请使用jQuery / Ajax将数组传递给php函数。

I suggest having this structure: 我建议采用以下结构:

Options={
  'opt1':{},
  'opt2':{},
  'opt3':{}
}

and you have these records 你有这些记录

//following is a structure view, not code
1: Record #1
2: Record #2
3: Record #3
4: Record #4

when user chooses to attach record#2 to opt3 , you do: 当用户选择将record#2附加到opt3 ,您将执行以下操作:

Options['opt3'][2]='Record #2';

New Options object: Options={ 'opt1':{}, 'opt2':{}, 'opt3':{ 2:'Record #2' } } Options对象:Options = {'opt1':{},'opt2':{},'opt3':{2:'记录#2'}}

removing added options is as easy as: 删除添加的选项很容易:

delete Options['opt3'][2]

I've had to do similar things with arrays in javascript and I used the splice method 我不得不对javascript中的数组做类似的事情,并且我使用了splice方法

This is the definition: 这是定义:

The splice() method adds and/or removes elements to/from an array, and returns the removed element(s). splice()方法向数组中添加元素和/或从数组中删除元素,并返回删除的元素。

This is where I would start. 这就是我要开始的地方。

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

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