pbauerochse / searchable-option-list

a jquery plugin for a searchable optionlist
MIT License
79 stars 54 forks source link

How to get selected values in an array #34

Closed shivarambodumalla closed 8 years ago

shivarambodumalla commented 8 years ago

HI i was trying to get the selected values in to an arry will you please help me how to get them ....

pbauerochse commented 8 years ago

Hi @shivarambodumalla ,

you could do something like this:

var sol = $('#mySelect').searchableOptionList({ 
    /* options omitted */ 
});

// later

var selectedOptionsAsArray = sol.getSelection();

This will not give you the internal data structure but the actual selected checkboxes/radios instead.

Let me know if that helped you.

Cheers

Patrick

ikramcheb commented 8 years ago

Hi, How can I get the internal values, I don't want the checkbox container thank you

pbauerochse commented 8 years ago

Hi @ikramcheb,

the internal data structure is attached to the input as sol-item data attribute so you could do this to convert the inputs to an array of the internal data representation:

var asInputs = sol.getSelection(),
    asInternalRepresentation = [];

// iterate over the selected items/dom elements
// extract the 'sol-item' data from it and store it in the other array
for (var i = 0; i < asInputs.length; i++) {
    asInternalRepresentation[i] = asInputs[i].data('sol-item');
}
ikramcheb commented 8 years ago

Hi, I got the following error TypeError: asInputs[i].data is not a function Noting that sol.getSelection() returns an array of inputs, so the problem is how to extract the 'sol-item'

pbauerochse commented 8 years ago

I thought I was returning jQuery objects but according to your error message it seems to be plain DOM elements then. The solution then is to wrap the object with jQuery again so

var asInputs = sol.getSelection(),
    asInternalRepresentation = [];

// iterate over the selected items/dom elements
// extract the 'sol-item' data from it and store it in the other array
for (var i = 0; i < asInputs.length; i++) {
    // change this
    // asInternalRepresentation[i] = asInputs[i].data('sol-item');
    // to this
    asInternalRepresentation[i] = $(asInputs[i]).data('sol-item');
}

should do the trick. Let me know if this worked.

Cheers

ikramcheb commented 8 years ago

yes thanks, just add label $(asInputs[i]).data('sol-item').label