@(Html.Kendo().TreeView()
.Name("CollectionsheetTree")
.DataSource(dataSource => dataSource
.Read(read => read
.Action("_getProjectCollectionsheetTree2", "Project", new { projectId = _projectId }) )
)
.HtmlAttributes(new { style = "height: 200px; overflow: auto; " })
.DragAndDrop(!Model.IsReadOnly)
)
I put the code in the $(document).ready as suggetsed by the Kendo documentation:
$(document).ready(function () {
var treeView = $("#CollectionsheetTree").data("kendoTreeView");
treeView.find(".k-in").each(function() {
var element = $(this),
data = element.find('> .data');
if (data.length) {
//element.css('background-color', '#aabbcc' );
element.addClass("treeviewNode");
data.remove();
}
});
});
But the execution fails over on this line:
treeView.find(".k-in").each(function() {
The error message says:
Microsoft JScript runtime error: Unable to get value of the property 'find': object is null or undefined
I think it is because when the $(document).ready is executed the treeview is still being constructed.
On the Telerik treeview control there is a OnDataBound event that can be used to ensure that a function is run only after the treeview has been fully initialised.
How can I ensure that the Kendo treeview has been initialised before calling the styling function on it?
For completeness, here is the controller method:
public ActionResult _getProjectCollectionsheetTree2(kendoTreeviewItem node, int? projectId)
{
var nodes = new List<kendoTreeviewItem>();
List<DofSurvey> list;
_dofSurvey.EnrolInUnitOfWork(_unitOfWork);
int collectionSheetId;
int.TryParse(node.id, out collectionSheetId);
if (node.id == null)
{
list = _dofSurvey.FindBy(f => f.ParentSurveyId == null & f.ProjectId == projectId, "Survey").ToList();
}
else
{
list = _dofSurvey.FindBy(f => f.ParentSurveyId == collectionSheetId & f.ProjectId == projectId, "Survey").ToList();
}
list.ForEach(cs =>
{
var d = new kendoTreeviewItem
{
id = cs.SurveyId.ToString(),
text = cs.Survey.Title + "<span class='data'></span>",
hasChildren = cs.ChildSurveys.Any()
};
nodes.Add(d);
});
return Json(nodes, JsonRequestBehavior.AllowGet);
}