我想要在我的Kendo UI网格的其中一列中使用kendomultiselect。
<div id="grid"></div>
<script>
$(document).ready(function () {
dataSource = new kendo.data.DataSource({
transport: {
read: {
url: '@Url.Action("GetCustomers", "Customer")',
dataType: "json"
},
update: {
url: '@Url.Action("SaveCustomer", "Customer")',
dataType: "json",
type: "POST"
},
destroy: {
url: '@Url.Action("RemoveCustomer", "Customer")',
dataType: "json",
type: "POST"
},
create: {
url: '@Url.Action("CreateCustomer", "Customer")',
dataType: "json",
type: "POST"
},
parameterMap: function (options, operation) {
if (operation !== "read") {
return options;
}
}
},
pageSize: 20,
schema: {
model: {
id: "CustomerId",
fields: {
CustomerId: { editable: false },
CustomerName: { validation: { required: true } },
CountryCode: { validation: { required: true } },
CustomerERPId: { validation: { required: true } },
Suppliers: { validation: { required: true } },
}
}
}
});
$("#grid").kendoGrid({
dataSource: dataSource,
navigatable: true,
pageable: true,
height: 550,
toolbar: ["create"],
columns: [
{
field: "CustomerName",
title: "CustomerName",
width: 200
}, {
field: "CountryCode",
title: "CountryCode",
width: 50
},
{
field: "CustomerERPId",
title: "CustomerERPId",
width: 100
},
{
field: "Suppliers",
title: "Suppliers",
width: 200,
editor: supplierMultiSelect, template: kendo.template($("#supplierTemplate").html())
},
{ command: ["edit", "destroy"], title: " ", width: "200px" }
],
editable: "inline",
});
});
function supplierMultiSelect(container, options) {
$('<input data-text-field="SupplierName" data-value-field="SupplierId" data-bind="value:' + options.field + '"/>')
.appendTo(container)
.kendoMultiSelect({
autoBind: true,
dataSource: {
type: "json",
transport: {
read: {
url: '@Url.Action("GetSuppliers", "Customer")',
dataType: "json"
}
}
},
});
}
</script>
这是结果! 因为你可以在我想要更新行时使用多选项。但问题是,当它不在“编辑模式”时,不会填充网格中的值。
编辑:
我添加了一个模板:
<script type="text/kendo" id="supplierTemplate">
<ul>
#for(var i = 0; i< data.length; i++){#
<li>#:data[i].SupplierName#</li>
#}#
</ul>
</script>
但是现在它不会正确地将数据绑定到我的动作方法!
答案 0 :(得分:2)
您的模板
"#=Suppliers.SupplierName#"
不起作用,因为供应商是一系列元素。 所以你需要这样的东西:
"#= Suppliers.map(function(el) { return el.SupplierName; }).join(', ') #"