views:

29

answers:

2

Hi,

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. When the form is ready to be submitted, use jQuery/Ajax to pass the array to a php function.

A: 

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:

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

New Options object: Options={ 'opt1':{}, 'opt2':{}, 'opt3':{ 2:'Record #2' } }

removing added options is as easy as:

delete Options['opt3'][2]
aularon
Initially though, there will only be one option available with a series of records to add. When the user adds, they'll group under option 1. The user then has the option to create a new grouping i.e. 2 with the same records so we could end up with 1=>array(1, 2, 3), 2=>array(1,2,3) etc. To coincide with this, each option has a publication date.
How do I go about creating the option and appending to it in the array.
Then initially You'll have `Options={}`, then you'll fill options like this: `Options['opt1']={}`, then fill inside it: `Options['opt1'][2]='Record #2';`. if publication date was the only string identifying the option, you can use it as an index instead of `'opt1'`
aularon
And can I pass this over a jQuery load method? As on assignment, the div with all records should update to remove the item added and updater the corresponding div on the right.
Also, the ID of the option selected is key of the array I'd like to group under, will Options[selectedOption]={} work? As your example is a string.
I just suggested a structure to group your data, sure you can use this structure to do whatever you want, change page elements (divs/selects), or post it back to the server as an ajax request.
aularon
A: 

I've had to do similar things with arrays in javascript and I used the splice method

This is the definition:

The splice() method adds and/or removes elements to/from an array, and returns the removed element(s).

This is where I would start.

Jeff V