EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: Berzy on July 23, 2014, 08:11:22 AM



Title: Combobox: add item
Post by: Berzy on July 23, 2014, 08:11:22 AM
how can I add items to a combobox?

my c#

<select class="easyui-combobox" id="FormatoCarta" name="FormatoCarta">
                            @foreach (var item in @Model.FormatiCarta)
                            {
                                String selected = String.Empty;
                                if (@Model.Defaults != null)
                                {
                                    if (item.Id.Equals(@Model.Defaults.IDFormatoCarta))
                                    {
                                        selected = "selected=selected";
                                    }
                                }
                                <option @selected value=@item.Id>@item.Nome</option>
                            }
                        </select>


my add function (not working)
$('#FormatoCarta').append('<option value=\"' + msg + '\">' + $('#NewNomeFormato').val() + '</option>');

it add the option, but the combobox don't reflect the change.

Thankyou!!


Title: Re: Combobox: add item
Post by: leela on July 23, 2014, 09:22:46 AM
You need to use 'loadData' of combobox, for the options to be set to it.

Declare combobox as
Code:
<input class="easyui-combobox" id="FormatoCarta" name="FormatoCarta" data-options="valueField:'id',textField:'name'">

Code:
<script>
var loadData = [];
</script>
@foreach (var item in @Model.FormatiCarta)
                            {
                                String selected = String.Empty;
                                if (@Model.Defaults != null)
                                {
                                    if (item.Id.Equals(@Model.Defaults.IDFormatoCarta))
                                    {
                                        selectedId = item.Id;
                                    }
                                }
<script>
loadData.push({"id":item.Id,"name":item.name});
</script>
                            }


<script>
$('#FormatoCarta').combobox({data:loadData});
$('#FormatoCarta').combobox('setValue', selectedId );
</script>