We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

treeview self reference database table

Hi,

i have some troubles to get the treeview working with a self refererence table, something like this :
   public partial class CATEGORIE
    {
        public int ID_CATEGORIE { get; set; }
        public string CAT_TEXT { get; set; }
        public Nullable<int> CAT_ID_PARENT { get; set; }
        public Nullable<int> CAT_LEVEL { get; set; }
    }


here is my client side code :

     jQuery.ajax({
                url: "/Produits/GetFilters",
                success: function (data) {
                      $("#treeView").ejTreeView({
                        height : "300px",
                        fields: {
                            id: "ID", text: "CAT_TEXT",
                            hasChild: true, dataSource: data.cat, expanded: "expanded",
                            child: { dataSource: data.subcat, parentId: "CAT_ID_PARENT", text: "CAT_TEXT", id: "ID_CATEGORIE" }

                        },
                        showCheckbox: true
                    });
                },
                async: false
            });


the controller return multiple json objects for other filters :
    public JsonResult GetFilters()
        {


            var categories = (from n in ctx.CATEGORIES.Where(a => a.FA_LEVEL == 1).OrderBy(a => a.FA_DESIGNATION)
                            select new
                            {
                                ID_CATEGORIE = n.ID_CATEGORIE,
                                CAT_TEXT = n.CAT_TEXT,
                                CAT_ID_PARENT = n.CAT_ID_PARENT
                            }).ToList();

            var subcategories = (from n in ctx.CATEGORIES.Where(a => a.CAT_LEVEL != 1 && a.CAT_LEVEL != 1).OrderBy(a => a.CAT_TEXT)
                            select new
                            {
                               ID_CATEGORIE = n.ID_CATEGORIE,
                                CAT_TEXT = n.CAT_TEXT,
                                CAT_ID_PARENT = n.CAT_ID_PARENT
                            }).ToList();



/*  other stuff */ 

            return Json(new { cat= categories  subcat=subcategories, ........}, JsonRequestBehavior.AllowGet);

        }

the problem is that only the first level is displayed .
my goal is to have unlimited nested levels displayed for my self reference table .

any help is welcome, thanks .

1 Reply

PR Piramanayagam Ramakrishnan Syncfusion Team November 30, 2016 12:05 PM UTC

Hi Issam, 
 
Thanks for contacting Syncfusion support. 
 
We have analyzed your query. We would like to inform you that, the child mapper fields are applicable only for remote data source. For your requirement, you need to retrieve the entire data including all child nodes (all levels) from data source and map this data to dataSource property of TreeView control. Please refer the below code example, 
 
[Script] 
 
$.ajax({ 
    url: "/Home/GetFilters", 
    type: "POST", 
    dataType: "json", 
    success: function (data) { 
        $("#tree1").ejTreeView({ 
            height: "300px", 
            fields: { 
                id: "ID_CATEGORIE", text: "CAT_TEXT", parentId: "CAT_ID_PARENT", dataSource: data, hasChild: "hasChild", expanded: "expanded", 
            }, 
            showCheckbox: true 
        }); 
    }, 
}); 
 
[controller] 
 
public JsonResult GetFilters() 
{ 
    List<CATEGORIE> treeData = GetTreeData(); 
    var categories = (from n in treeData.OrderBy(a => a.CAT_TEXT) 
                        select new 
                        { 
                            ID_CATEGORIE = n.ID_CATEGORIE, 
                            CAT_TEXT = n.CAT_TEXT, 
                            CAT_ID_PARENT = n.CAT_ID_PARENT, 
                            hasChild = n.hasChild, 
                            expanded = n.expanded 
                        }).ToList(); 
    return Json(categories, JsonRequestBehavior.AllowGet); 
} 
 
 
For your reference, we have prepared a sample based on your requirement, and it can be downloaded from the following location: Sample 
 
Please let us know whether the provided sample is helpful in achieving your requirement. If not, get back to us with more information to proceed further.

Regards,
Piramanayagam R
 


Loader.
Live Chat Icon For mobile
Up arrow icon