我一直在尝试使用绑定到远程数据源的 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">×</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>
这是我的脚本,基本上定义了剑道数据源,并在 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>