I want to be able to click on a treeview node and show the products for the selected category within the grid, need some advice.
I have a kendo grid marked as follows:-
viewModel = kendo.observable({ gridSource: productdata });
script>
var viewModel = null;
$(document).ready(function() {
var treeview;
$("#vertical").kendoSplitter({
orientation: "vertical",
panes: [
{ collapsible: true, size: "50%" },
{ collapsible: true, size: "50%" }
]
});
$("#horizontal").kendoSplitter({
panes: [
{ collapsible: true, size: "35%" },
{ collapsible: true, size: "65%" }
]
});
treeview = $("#treeview").kendoTreeView({
template: kendo.template($("#treeview-template").html()),
dragAndDrop: false,
dataSource: [@(Html.Raw(Model.Json))],
select: onSelect
}).data("kendoTreeView");
var productdata = @(Html.Raw(Model.JsonProducts));
viewModel = kendo.observable({
gridSource: productdata
});
$("#grid").kendoGrid({
dataSource: {
data: viewModel.gridSource,
pageSize: 10
},
autoBind:true,
selectable: "multiple, row",
groupable: false,
scrollable: true,
sortable: true,
pageable: false,
drop: droptargetOnDrop,
columns: [
{
field: "id",
width: 50,
title: "Id"
},
{
field: "categoryid",
width: 250,
title: "Category Id"
}, {
field: "productname",
width: 250,
title: "Product Name"
}
]
});
$("#grid").kendoDraggable({
filter: "tr",
hint: function() {
var g = $("#grid").data("kendoGrid");
return g.select().clone();
}
});
$("#treeview").kendoDropTarget({
dragAndDrop: true,
drop: droptargetOnDrop
});
});
function onSelect(e) {
var url = "/home/UpdateListingByCategory";
var selectedCatId = $(e.node.children[0]).find("input").val();
$.ajax({
type: "POST",
url: url,
data: { categoryId: selectedCatId },
success: function(data) {
viewModel.gridSource = JSON.parse(data);
alert(viewModel.gridSource);
},
error: function() {
alert("error");
}
});
}
function droptargetOnDrop(e) {
var ss = e.srcElement.parentElement.children[1];
var url = "/home/update";
var list = new Array();
for (var i=0; i < e.draggable.hint.length; i++) {
var id = e.draggable.hint[i].firstChild.innerHTML;
list.push(id);
}
$.ajax({
type: "POST",
url: url,
data: { categoryId: $(ss).val(), productIds: list.join() },
success: function(data) {
viewModel.gridSource = JSON.parse(data);
},
error: function() {
alert("error");
}
});
kendo.bind($("#grid"), viewModel);
}
</script>