Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
60
IgTreeGrid angular2 loadOnDemand sample
posted

Hi All!

I use igniteui igTreeGrid component (enableRemoteLoadOnDemand: true) with angular2.

import { Component } from '@angular/core';
import { RoutingComponent } from '../navigation/route.decorator';

declare var $: any;

const dataUrl = '/nodes.json';

@Component({          
    templateUrl: `<ig-tree-grid [(widgetId)]='id' [options]="tgridOptions"></ig-tree-grid>`
})
@RoutingComponent()
export class UsersTreeView {

    private id: string = 'tgrid';
    private tgridOptions: any;

    constructor() {
        
        this.tgridOptions = {                        
            autoCommit: true,            
            autoGenerateColumns: false,
            dataSource: [],
            dataSourceUrl: dataUrl,                        
            enableRemoteLoadOnDemand: true,
            features: [
                { name: 'Resizing' },
                //{ name: 'RowSelectors', rowSelectorNumberingMode: 'sequential' },
                //{ name: 'Selection' }
            ],
            initialExpandDepth: 0,
            primaryKey: "id",
            childDataKey: "objects",
            renderExpansionIndicatorColumn: true,
            height: "400px",
            width: "100%",
            columns: [
                { key: "id", headerText: "id" },
                { key: "name", headerText: "name" },
                { key: "description", headerText: "description" }                
            ]
        };
    }
}

And here's nodes.json structure

[
  {
    "id": 0,
    "name": "Project Plan",
    "description": "6/2/2014",
    "objects": []
  },
  {
    "id": 1,
    "name": "Project Plan",
    "description": "6/2/2014",
    "objects": [
      {
        "id": 3,
        "objects": [ { "id":  4} ],
        "name": "3"
      }
    ]
  }
]


1. First node ({
    "id": 0,
    "name": "Project Plan",
    "description": "6/2/2014",
    "objects": []
  })

is not expanding instead of havingobjects(this is childkey) field array.

2. If treegrid options doesn't have dataSource (I'm using RemoteLoadOnDemand mode) property component gets error -Cannot read property 'length' of undefined

What settings I have lost to include into igTreeGrid to resolve such behaviour?

Thanks.

Parents Reply
  • 3995
    Verified Answer
    Offline posted in reply to Roman

    Hello Roman,

    You can specify requestDataCallback  and use your custom request to get the data and the additional parameters you want to send.

    You could specify this callback into the rowExpanding event.

    For example:

    rowExpanding: function() {

    if (ui.owner.dataSource.settings.treeDS.requestDataCallback === null ||
     ui.owner.dataSource.settings.treeDS.requestDataCallback === undefined) {
     ui.owner.dataSource.settings.treeDS.requestDataCallback = function (record, expand, callbackArgs) {
      if (!record) {
       return;
      }
      var opts, me = ui.owner.dataSource, url, path, params, func, s = ui.owner.dataSource.settings.treeDS,
       args = {
        record: record,
        callbackArgs: callbackArgs,
        expand: expand
       };
      path = ui.owner.dataSource.getPathBy(record);
      params = ui.owner.dataSource._encodeUrl();
      params.expand = expand;
      url = s.dataSourceUrl + "?" + ui.owner.dataSource._encodeUrlPath(path, record[ s.propertyDataLevel ]);
      opts = {
       type: "GET",
       url: url,
       data: params,
       success: function (data, textStatus, jqXHR) {
        var func = s.requestDataErrorCallback,
         noCancel = true;
        if ($.type(func) === "function") {
         noCancel = func(args, data, textStatus, jqXHR);
        }
        if (noCancel) {
         me._requestDataSuccess(args, data, textStatus, jqXHR);
        }
       },
       error: function (jqXHR, textStatus, errorThrown) {
        var func = s.requestDataErrorCallback;
        if ($.type(func) === "function") {
         func(args, jqXHR, textStatus, errorThrown);
        }
       }
      };
      $.ajax(opts);
     }
    }

    }

    You can add your custom parameters to the params variable

Children