如果初始值是ValueMapper加载的第一页中的项,则当前实现效果很好,但如果它位于列表的末尾,则当前实现不起作用。 我正在使用虚拟化,因为该列表包含数千个项目。
@(Html.Kendo().MultiSelectFor(m => m.EmployeeIds)
.Name("EmployeeIds")
.DataTextField("Value")
.DataValueField("Key")
.AutoBind(true)
.DataSource(source =>
{
source.Custom()
.ServerFiltering(false)
.ServerPaging(false)
.Transport(transport =>
{
transport.Read(MVC.Request.ActionNames.GetEmployeeList, MVC.Request.Name);
});
})
.Events(e =>
{
e.DataBound("setInitialWatchers");
})
.Placeholder("Insert a watcher...")
.Virtual(v => v.ItemHeight(26).ValueMapper("watchersMapper")))
我正在设置DataBound事件的初始值:
function setInitialWatchers() {
//to ensure only one initial bound will ocur
//as it is bounded on every page/scroll down
if (countBound == 0) {
$.ajax({
url: "@Url.Action(MVC.Request.ActionNames.GetInitialWatchers, MVC.Request.Name, new { RequestId = Model.Id })",
success: function (data) {
$("#EmployeeIds").data("kendoMultiSelect").value(data);
}
});
countBound++
}
}
function watchersMapper(options) {
$.ajax({
url: "@Url.Action(MVC.Request.ActionNames.GetEmployeeList_ValueMapper, MVC.Request.Name)",
type: "GET",
success: function (data) {
options.success(data);
}
});
在控制器中:
public virtual JsonResult GetEmployeeList(string optionLabel = null)
{
var emplpyees = Facade.EmployeeBusinessLogicFacade.GetAllKeyValue().OrderBy(e => e.Value).ToList()
;
return Json(emplpyees.TryAppendOptionLabel(optionLabel), JsonRequestBehavior.AllowGet);
}