Select2 Multiple select not selecting multiple values with databind

882 views
Skip to first unread message

nishant shete

unread,
Jan 8, 2013, 4:16:00 AM1/8/13
to sel...@googlegroups.com
I am using jquery select2 multiple select,and i am binding it with database values using ajax,when I have 2 values coming from database ,its getting selected in select2 control but when i have more than two values its selecting only one value in select2 control.Any solution?
beleow is my code--

-------HTML declaration
<select name="select" id="CommodityList" class="span12 nostyle " placeholder="Select Commodities"
                                    size="0" multiple="multiple">
                                    <option value="">----</option>
                                    <optgroup label="Commodity Code/Name">
                                        <asp:Literal ID="litCommodities" runat="server"></asp:Literal>
                                    </optgroup>
                                </select>

-----------binding---
 $("#CommodityList").select2({
                formatResult: formatCommodity,
                formatSelection: formatSelectionCommodity
                });

            function formatCommodity(state) {
                if (!state.id) return state.text; // optgroup
                var parts = state.text.split("|")
                if(parts.length>0){
                    return "<span style='position:absolute;min-width:150px;margin-left:4px'>"+parts[0]+"</span><span style='position:absolute;min-width:200px;left:150px;min-height:20px;'>" + parts[1]+"</span>.";
                }
                else{
                    return "<span style='position:absolute;min-width:150px;margin-left:4px'>"+state.id+"</span><span style='position:absolute;min-width:200px;left:150px;min-height:20px;'>" + state.text+"</span>.";
                }
               
           }

           function formatSelectionCommodity(state) {
                if (!state.id) return state.text; // optgroup
                var parts = state.text.split("|")
                if(parts.length>0){
                    return parts[0];
                }
                else{
                    return state.text;
                }
           }

-----------selecting values based on values returnig from database-----------
if(pageData.CommodityCodes==null || pageData.CommodityCodes.length==0)
            {
                $("#CommodityList").select2("val","");

            }
            else
            {
                $("#CommodityList").select2("val",pageData.CommodityCodes.split(',')).trigger("change");
            }

Igor Vaynberg

unread,
Jan 8, 2013, 11:54:13 AM1/8/13
to sel...@googlegroups.com
create a jsfiddle that repros the problem with the staticaly rendered
select tag you are using.

-igor
> --
> You received this message because you are subscribed to the Google Groups
> "select2" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/select2/-/P1G3JCTWDkQJ.
> To post to this group, send email to sel...@googlegroups.com.
> To unsubscribe from this group, send email to
> select2+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/select2?hl=en.
Message has been deleted
Message has been deleted
Message has been deleted

Kabir Hossain

unread,
Apr 5, 2015, 4:03:18 AM4/5/15
to sel...@googlegroups.com

After selecting type and garage, When the user will click on stock field, ther previously selected value will be put on the clicked stock, But my code gives me the same as first and second times

Please check my fiddle where is my mistake, First one is ok, But when I select the second one, it gives me the first data as output, but why ?

Demo http://jsfiddle.net/kabircse/kzgvcsyk/
Reply all
Reply to author
Forward
0 new messages