This is a migrated thread and some comments may be shown as answers.

HierarchicalDataSource, get parent fields in parameters from child url function

3 Answers 187 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Narjisse
Top achievements
Rank 1
Narjisse asked on 30 May 2017, 08:32 AM

Hi,

Is it possible to access in child url function, the parent fields (I want the type to make a condition for the url) ?
Thank you in advance.  
Here a part of the source code

let matieresSource: kendo.data.HierarchicalDataSourceOptions = {
                transport: {
                    read: {
                        url: (params) => {
                            this.$log.info("params", params);
                            return this.apiEndpoint + "/v1/parcours/" + params.id + "/matieres/edito/tree";
                        },
                        type: "GET",
                        dataType: "json",
                        beforeSend: function (req) {
 
                            let authData = localStorage.getItem('authData');
                            let auth = "Bearer " + JSON.parse(authData).access_token;
 
                            req.setRequestHeader('Authorization', auth);
                        }
                    },
                    parameterMap: (data: kendo.data.DataSourceTransportParameterMapData, type: string) => {
                        // Remove query string in the URL
                        data = null;
 
                        return data;
                    }
                },
                schema: {
                    model: {
                        fields: {
                            id: "id",
                            name: "name",
                            type: "type",
                            baseElement: "baseElement",
                            isDirty: "isDirty",
                            hasDirty: "hasDirty",
                            nbChildren: "nbChildren"
                        },
                        hasChildren: (data: any) => {
                            return data.nbChildren > 0 ? true : false;
                        },
                        children: modulesSource
                    }
                }
            };
 
            let parcoursSource: kendo.data.HierarchicalDataSourceOptions = {
                transport: {
                    read: {
                        url: (params) => {
                            return this.apiEndpoint + "/v1/parcours/edito/tree";
                        },
                        type: "GET",
                        dataType: "json",
                        beforeSend: function (req) {
 
                            let authData = localStorage.getItem('authData');
                            let auth = "Bearer " + JSON.parse(authData).access_token;
 
                            req.setRequestHeader('Authorization', auth);
                        }
                    },
                    parameterMap: (data: kendo.data.DataSourceTransportParameterMapData, type: string) => {
 
                        return data;
                    }
                },
                schema: {
                    model: {
                        fields: {
                            id: "id",
                            idLms: "idLms",
                            name: "name",
                            type: "type",
                            baseElement: "baseElement",
                            isDirty: "isDirty",
                            nbChildren: "nbChildren"
                        },
                        hasChildren: (data: any) => {
                            return data.nbChildren > 0 ? true : false;
                        },
                        children: matieresSource
                    }
                }
            };

3 Answers, 1 is accepted

Sort by
0
Nencho
Telerik team
answered on 01 Jun 2017, 08:39 AM
Hello Narjisse,

I am afraid that the requested functionality is not feasible. Could you share a bit more about your scenario, so we could eventually suggest you an alternative implementation for your scenario?

Regards,
Nencho
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Narjisse
Top achievements
Rank 1
answered on 01 Jun 2017, 08:55 AM

Hello Nencho,

Thank you for your reply. I solved my problem by modifying the url of the children of a node (with  specific type) during the databound.
I thought there was a solution already integrated with kendo to change the url of children according to the parent when declaring the dataSource :( 
Here is the solution I found : 

/**
 * Fonction événement
 * @param e kenod.ui.TreeViewDataBoundEvent
 */
private onDataBound(e: kendo.ui.TreeViewDataBoundEvent):void {
 
            let item: IElementNode = e.sender.dataItem(e.node) as IElementNode;
            let data: kendo.data.ObservableArray = null;
 
            if (item !== undefined && item !== null) {
 
                // Récupération des enfants
                data = item.children.data();
 
                // En fonction du type de l'item qu'on "expand", on vérifie ses fils
                switch (item.type) {
                    case "parcours":
                        this.changeChildrenSource(data);
                        this.expandItemsAndSelectItem("matiere", data);
                        break;
 
                    case "matiere":
                        this.expandItemsAndSelectItem("module", data);
                        break;
                       .....
}
 
/**
  * Fonction pour modifier l'url des enfants si besoin
  * @param data
*/
private changeChildrenSource(data: kendo.data.ObservableArray):void {
 
            if(data) {
                // On vérifie le type de l'élément actuel pour modifier la source de ses enfants
                data.forEach((element: IElementNode) => {
 
                    switch (element.type) {
                        case "competence":
                            element.children.options.transport.read.url = (params) => {
                                return this.apiEndpoint + "/v1/competences/" + element.id + "/evaluations/edito/tree";
                            }
                            break;
                     
                        default:
                            break;
                    
                });
            }
}
I solved my problem by modifying the url of the children of a node during the databound
I solved my problem by modifying the url of the children of a node during the databound
I solved my problem by modifying the url of the children of a node during the databound
I solved my problem by modifying the url of the children of a node during the databound
I solved my problem by modifying the url of the children of a node during the databound
I solved my problem by modifying the url of the children of a node during the databound
I solved my problem by modifying the url of the children of a node during the databound
I solved my problem by modifying the url of the children of a node during the databound
I thought there was a solution already integrated with kendo to change the url of children according to the parent when declaring the dataSource
0
Nencho
Telerik team
answered on 02 Jun 2017, 10:01 AM
Hello Narjisse,

Thank you for sharing your solution with the community!

Regards,
Nencho
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data (charts) and form elements.
Tags
TreeView
Asked by
Narjisse
Top achievements
Rank 1
Answers by
Nencho
Telerik team
Narjisse
Top achievements
Rank 1
Share this question
or