0

I have api response data stucture like below. My data is dynamic and may be change. Structure of data has children. It may has any children in children that times of that is not determined.The last child has a leaf and its value is true but the leaf of others that are parents is true.

[
            {
                "Id": 16456,
                "ParentId": null,
                "Title": "Tehran",
                "MapLayerGroupTypeId": "1",
                "leaf": false,
                "children": [
                    {
                        "Id": 16457,
                        "ParentId": "16456",
                        "Title": "water",
                        "MapLayerGroupTypeId": "1",
                        "leaf": false,
                        "children": [
                            {
                                "Id": 22334,
                                "MapLayerGroupId": "16457",
                                "Title": "water passages",
                                "leaf": true
                            }
                        ]
                    },
                    {
                        "Id": 16458,
                        "ParentId": "16456",
                        "Title": "base",
                        "MapLayerGroupTypeId": "1",
                        "leaf": true,
                        "children": []
                    },
                ]
            },
            {
                "Id": 16462,
                "ParentId": null,
                "Title": "",
                "MapLayerGroupTypeId": "1",
                "leaf": false,
                "children": [
                    {
                        "Id": 16463,
                        "ParentId": "16462",
                        "Title": "title2",
                        "MapLayerGroupTypeId": "1",
                        "leaf": true,
                        "children": []
                    },
                    {
                        "Id": 16464,
                        "ParentId": "16462",
                        "Title": "title2",
                        "MapLayerGroupTypeId": "1",
                        "leaf": true,
                        "children": []
                    },
                    {
                        "Id": 16465,
                        "ParentId": "16462",
                        "Title": "title3",
                        "MapLayerGroupTypeId": "1",
                        "leaf": true,
                        "children": []
                    }
                ]
            }
        ]

I wanna display it in tree view using ExtJs.

0

1 Answer 1

1

I write codes like below and It works:

Ext.define('Gis.view.MapLayer.MapLayerTreePanelPublic', {
    extend: 'Ext.tree.Panel',
    xtype: 'public_gis_tree_panel',
    controller: 'public_gis_map_layer',

    renderConfig: {
        gisMap : null
    },

    title: 'Geospatial Layers',
    baseToolbar: false,
    displayField: 'Title',
    rootVisible: false,
    checkPropagation: 'both',
    reference: 'publicGisTreeMap',

    tools: [
        {
            type: 'expand',
            tooltip: 'Open All',
            callback: 'onExpandLayerTree'
        },
        {
            type: 'collapse',
            tooltip: 'Close All',
            callback: 'onCollapseLayerTree'
        },

    ],

    initComponent: function () {
        let me = this;

        me.loadData();
        me.callParent();
    },
    listeners: {
        checkchange: 'onTreeCheckChanged',
        itemclick: 'onTreeItemClick',
        itemcontextmenu: 'showContextMenu'
    },

    loadData: function () {
        let me = this;

        Ext.Ajax.request({
            url: BASE.GIS_MODULE_URL + 'map-layer-group/index',
            method: 'POST',
            jsonData: {
                typeCode: "public"
            },
            success: function (response) {
                let result = Ext.decode(response.responseText);
                if (result.success) {
                    const setNodeChecked = (nodes) => {
                        return nodes.map(node => {
                            if (node.leaf) {
                                node.checked = false; // Set initial checked state to false
                            } else if (node.children) {
                                node.children = setNodeChecked(node.children);
                            }
                            return node;
                        });
                    };

                    let processedData = setNodeChecked(result.data.items);
                    let store = Ext.create('Ext.data.TreeStore', {
                        root: {
                            expanded: true,
                            children: processedData
                        },
                        fields: [{
                            name: 'iconCls',
                            convert: function (v, rec) {
                                if (!rec.data.leaf) {
                                    return 'x-fa fa-map';
                                }
                                if (rec.get('StylePreview')) {
                                    let styleElement = document.createElement('style');
                                    styleElement.type = 'text/css';
                                    styleElement.innerHTML = `
                        .x-tree-icon${rec.get('Id')} {
                            background-image: url("data:image/png;base64, ${rec.get('StylePreview')}") !important;
                            background-position: center !important;
                            background-size: cover !important;
                        }`;
                                    document.head.appendChild(styleElement);
                                    return `x-tree-icon${rec.get('Id')}`; // Icon for leaf nodes with a preview
                                }
                            }
                        }],
                    });
                    // Set store after data is processed
                    me.setStore(store);
                }
            },
        });
    },

});


Ext.define('Gis.view.MapLayer.MapLayerTreePanelPublicController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.public_gis_map_layer',

    init: function (view) {
        let me = this;
    },

    onCollapseLayerTree: function (tree) {
        tree.collapseAll();
    },

    onExpandLayerTree: function (tree) {
        tree.expandAll();
    },

    onTreeItemClick: function (tree, record) {
        let me = this;
        const layer = record.getData()
        if (layer.checked === true) {
            me.gisMap.setSelectedLayer(layer);
        } else {
            me.gisMap.setSelectedLayer(null);
        }
    },

    onTreeCheckChanged: function (node, checked, e, eOpts) {
        let me = this;
        let view = me.getView();

        if (node.data.leaf === false) {
            return;
        }

        let gisMap = view.gisMap;
        let tree = node.getOwnerTree();
        let checkedLayer = me.getCheckedLayer(tree);

        if (node.data.checked) {
            gisMap.addWmsLayer(node.data);
        } else {
            gisMap.removeWmsLayer(node.data);
        }

        gisMap.getController().getWmsLegend(checkedLayer.map(layer => layer.LayerName));
        console.log(`Node "${node.get('Title')}" checked: ${checked}`);

    },

    getCheckedLayer: function (tree) {
        let store = tree.getStore();
        let checkedRecords = store.queryBy(function (record) {
            return record.get('checked') === true && record.get('leaf') === true;
        });
        let checkedLayer = checkedRecords.items.map(layer => layer.data);

        return checkedLayer;
    },

});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.