Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select 2 add additional parameters to select options

I need additional parameters for <option></option> tag that generates by select2 from ajax data.

My ajax params for select2(coffee):

ajax:
  url: '/moysklad_warehouse_items/find_warehouse_item'
  dataType: 'json'
  delay: 500
  data: (query) ->
    { q: query.term }
  processResults: (data) ->
    { results: data }

Resulted data structure:

[{id: 1, text: 'one', price: 100, weight: 10},
 {id: w, text: 'two', price: 200, weight: 12}]

By default select2 provide only id and text parameters and select looks like this:

<select>
  <option value="10" selected="selected">one</option>
  <option value="11" selected="selected">one</option>
</select>

I want to add additional data(price and weight), like, that parameters take part in calculations and only text tag if insufficiently

<option value="11" selected="selected" price="12" weight="100">one</option>

If its impossible for <option></option> tag, it would be normal if I put, for example, <span></span> inside <option> and add parameters to <span>

like image 215
Igor Biryukov Avatar asked Feb 13 '26 10:02

Igor Biryukov


1 Answers

I can get access for my additional parameters by $('select').select2('data') and get object with all parameters

like image 127
Igor Biryukov Avatar answered Feb 15 '26 00:02

Igor Biryukov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!