如何使用远程数据源将初始值设置为Kendo UI MultiSelect

时间:2014-04-17 11:41:37

标签: javascript jquery asp.net-mvc kendo-multiselect

我一直在尝试使用绑定到远程数据源的Kendo UI MultiSelect实现编辑方案,并使用预先选择的值。当设置为本地数据源时,它工作正常,但在具有2k选项的列表上非常慢(主要原因是我使用远程数据源)。 如何将选定值添加到MultiSelect绑定到远程数据源时? 如何将通过模态添加的新项目添加到MultiSelect所选值? 这是我的HTML

<div class="form-group">
    @Html.LabelFor(model => model.People, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @*@Html.DropDownListFor(model => model.People, Model.PeopleDropDown, new { multiple = "multiple", data_multiselect = "true", data_charges = "true" })*@
        <select id="People" multiple="multiple" name="People">

            @foreach (var c in Model.Involvement.InvolvementCharges)
            {
                <option value="@c.ChargeId">@c.Charge.Description</option>
            }

        </select>
        @Html.ValidationMessageFor(model => model.People)
    </div>
</div>

我正在使用此模式将新人添加到列表中,我也无法将新值设置为所选列表,与本地数据一起正常工作。 (请参阅本地数据Kendo ui Multi Select Remove Selected element using value

<div id="modifyModal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title"></h4>
            </div>
            <div class="modal-body"></div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button id="submitForm" type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div>
    </div>
</div>

这是我的脚本,基本上定义了kendo数据源,并在MultiSelect中使用它,以及一些处理模态div的代码。

<script>
    $(function () {

        var peopleUrl = '@Url.Action("PeopleJson", "DropDowns")';

        var peopleDataSource = new kendo.data.DataSource({
            serverPaging: true,
            serverSorting: true,
            serverFiltering: true,
            pageSize: 10,
            transport: {
                read: {
                    type: 'post',
                    dataType: 'json',
                    url: peopleUrl
                }
            },
            schema: {
                data: 'Data'
            }
        });

        $('#People').kendoMultiSelect({
            minLength: 3,
            filter: 'contains',
            placeholder: 'Start typing charge...',
            dataValueField: "Key",
            dataTextField: "Value",
            autoBind: false,
            dataSource: peopleDataSource
        });

        $('#submitForm').click(function (e) {
            e.preventDefault();
            $.post('@Url.Action("Create", "People")', $('#CreatePersonForm').serialize(),
                function (data, status, xhr) {
                    if (status) {
                        $("#modifyModal").modal("hide");
                        //var multiSelect = $('#People');
                        //multiSelect.data("kendoMultiSelect").dataSource.add({ value: data.PersonId, text: data.Name });
                        //multiSelect.data("kendoMultiSelect").dataSource.sort({ field: "text", dir: "asc" });
                        //var add = [data.PersonId];
                        //var values = multiSelect.data("kendoMultiSelect").value().slice();
                        //var merge = $.merge(values, add);
                        //multiSelect.data("kendoMultiSelect").dataSource.filter({});
                        //multiSelect.data("kendoMultiSelect").value($.unique(merge));
                    }
                });
        });

    });
</script>

0 个答案:

没有答案