initSelection not populating select2-search-choice

2,140 views
Skip to first unread message

bahh...@gmail.com

unread,
Aug 12, 2013, 11:47:29 AM8/12/13
to sel...@googlegroups.com
I've looked around for similar cases of this but I have not found anything. Basically I have my hidden input with the values that I would like to populate my select2 multi select input with in the value attribute. The initSelection loads the correct number of select2-search-choice elements into the input but none of the names or ids are properly put into the inside div where the text should be.

Any ideas on why this may be and how to fix this?

Thanks!

*Attached is a picture of the blank select2-search-choice elements\

Untitled.png

Igor Vaynberg

unread,
Aug 12, 2013, 11:55:03 AM8/12/13
to sel...@googlegroups.com
most likely cause is that you are giving select2 objects in incorrect format. either they are missing the text attribute if you are using default renderers, or whatever custom attributes your custom renderers expect.

-igor



--
You received this message because you are subscribed to the Google Groups "select2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to select2+u...@googlegroups.com.
To post to this group, send email to sel...@googlegroups.com.
Visit this group at http://groups.google.com/group/select2.
For more options, visit https://groups.google.com/groups/opt_out.



bahh...@gmail.com

unread,
Aug 13, 2013, 9:40:06 AM8/13/13
to sel...@googlegroups.com
All I am doing is putting the values (separated by commas) in the value attribute of the hidden input and then using the initSelection function supplied on the Select2 site for multiple values:

initSelection : function (element, callback) {
var data = [];
$(element.val().split(",")).each(function () {
data.push({id: this, text: this});
});
return callback(data);
}

Are you saying that there should be a text attribute in the object? Attached is what the current objects look like when I print each one inside the .each from above.

Thanks

Untitled.tiff

bahh...@gmail.com

unread,
Aug 15, 2013, 8:26:08 AM8/15/13
to sel...@googlegroups.com
I don't think I am using any custom renderers. Any ideas on what I need to do to get this working?


On Monday, August 12, 2013 11:55:03 AM UTC-4, Igor Vaynberg wrote:

Igor Vaynberg

unread,
Aug 15, 2013, 11:48:52 PM8/15/13
to sel...@googlegroups.com
create a jsfiddle to reproduce.

-igor
Message has been deleted

bahh...@gmail.com

unread,
Aug 16, 2013, 9:37:09 AM8/16/13
to sel...@googlegroups.com

Igor Vaynberg

unread,
Aug 16, 2013, 11:08:45 AM8/16/13
to sel...@googlegroups.com
like i said in the original reply, you are not giving select2 objects. it expects {id,text} you are just giving it a string:

 data.push(this.toString());

should be

 data.push({id:this.toString(),text:this.toString()});

-igor

bahh...@gmail.com

unread,
Aug 16, 2013, 1:06:55 PM8/16/13
to sel...@googlegroups.com
That was basically what I had in my second post. But it doesn't work either, it just supplies the right number of select2-search-choices but they are still blank.

Updated jsfiddle: http://jsfiddle.net/wLV48/3/

Igor Vaynberg

unread,
Aug 16, 2013, 1:19:37 PM8/16/13
to sel...@googlegroups.com
in that fiddle you do have custom renderers:

formatResult: function(item, page) {
return item.name
},
formatSelection: function(item, page) {
return item.name
},

so in that case initSearchChoice should be returning objects with a name property...

-igor

bahh...@gmail.com

unread,
Aug 16, 2013, 7:28:40 PM8/16/13
to sel...@googlegroups.com
Yep, that was it. Wasn't sure what you meant by custom render, but I got it! Thanks!
Reply all
Reply to author
Forward
0 new messages