Add data-x and data-y to option

42 views
Skip to first unread message

m...@nagkumar.com

unread,
Nov 18, 2015, 3:49:41 PM11/18/15
to select2
Hi,
I am using select2 and initializing it using an array of objects. It has 4 values, "id", "text", "x" and "y".

items = [{
"id": 1,
"text": "text1",
"x": 10,
"y": 20
},{
"id": 2,
"text": "text2",
"x": 15,
"y": 25
}];
$("#objects").select2({
data: items,
tags: true,
maximumSelectionSize: 1,
placeholder: "Search for product",
allowClear: true,
maximumSelectionLength: 5
});

I want the "x" and "y" to be the data attributes in HTML. Like this:
<select id="objects">
<option value="1" data-x="10" data-y="20">text1</option>
<option value="2" data-x="15" data-y="25">text2</option>
</select>

Is there any way that I can achieve this?

Guilhem Fabre

unread,
Feb 16, 2016, 4:27:52 AM2/16/16
to select2, m...@nagkumar.com
Hello,

Same question here :) I need to add custom data- attributes on the options generated by Select2 after a remote load.

Thx

dan...@riipl.com

unread,
Aug 22, 2017, 6:58:31 PM8/22/17
to select2, m...@nagkumar.com
Bump. Need this too.

p.steph...@gmail.com

unread,
Sep 5, 2017, 7:58:43 PM9/5/17
to select2, m...@nagkumar.com


The only thing I can think of, from the docs, would be to use the templateResult method to customize the Option elem.

Reply all
Reply to author
Forward
0 new messages