Вопрос

I'm trying to build a kendo tree menu. I can't figure out what format the data should be feed to the widget. I have tried this so far:

Model:

public class TreeModel
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string URL { get; set; }
    public int? ParentsID { get; set; }
    public bool HasChild { get; set; }
}

Controller:

public ActionResult LoadMenu()
    {
       List<TreeModel> list = new List<TreeModel> {
       new TreeModel() { ID=1, Name="Setup", URL="m.facebook.com", HasChild=true},
       new TreeModel() { ID=10, Name="Leave", URL="google.com", ParentsID=1, HasChild=false},
       new TreeModel() { ID=2, Name="EmployeeInfo", URL="m.facebook.com", HasChild=true},
       new TreeModel() { ID=11, Name="Basic Employee", URL="m.facebook.com", HasChild=false, ParentsID=2},

       };

       var nodes = (from n in list
                    where n.HasChild == true
                    select n).ToList();

       return Json(nodes, JsonRequestBehavior.AllowGet);

    }

Script on my view:

<script type="text/javascript">

    homogeneous = new kendo.data.HierarchicalDataSource({
        transport: {
            read: {
                url: "/Home/LoadMenu",
                //dataType: "json",
                type: "GET"

            }
        },
        schema: {
            model: {
                id: "ID",
                hasChildren: "HasChild"
            }
        }
    });




    $(document).ready(function () {

        $("#treeMenu").kendoTreeView({
            dataSource: homogeneous,
            dataTextField: "Name",
            dataUrlField: "URL",
            hasChildren:"ParentsID"
        });

    });


        </script>

The current output & problems are marked on the screen shot. enter image description here

please help. Thanks.

Это было полезно?

Решение

Please try with the below code snippet.

public ActionResult LoadMenu(int? id)
{
    List<TreeModel> list = new List<TreeModel> {
   new TreeModel() { ID=1, Name="Setup", URL="m.facebook.com", HasChild=true},
   new TreeModel() { ID=10, Name="Leave", URL="google.com", ParentsID=1, HasChild=false},
   new TreeModel() { ID=2, Name="EmployeeInfo", URL="m.facebook.com", HasChild=true},
   new TreeModel() { ID=11, Name="Basic Employee", URL="m.facebook.com", HasChild=false, ParentsID=2},

   };

        var nodes = (from n in list
                     where (id.HasValue ? n.ParentsID == id.Value : n.ParentsID == null)
                     select n).ToList();

        return Json(nodes, JsonRequestBehavior.AllowGet);

}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top