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

Kendo DataSource, AngularJS - problem with databinding

3 Answers 527 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 13 Jun 2014, 07:45 AM
I want to fill a grid with a complex json returned from a webservice. My json contains two things:

 - data: array with the records that will fill the grid
 - columns: array with the config(layout) of the grid

I have successfully filled the grid with the "data" by specifying the schema.data.

My problem is with the grid config (layout). I get the columns array on the `requestEnd` event of the datasource and i add it to the `customersSource` (datasource) so i can access it in the gridOptions. 

The problem is that even though when i log the `customersSource` object i see that the cols array i added, is there and is filled with the proper data the `$scope.mainGridOptions.columns` isn't set to `customersSource.cols`.

I think that this may have to do with the fact that `customersSource.cols` is set asynchronously but shouldn't angular take care of this with it's databinding?

Also i have read in Data source vs. Angular that i may have to set something as Observable but i am confused of what to do exactly.

How can i fix this?

Here is my code
var customersSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: "http://....",
                dataType: "json"
            }
        },
        schema: {
            data: "data"
        },
        requestEnd: function (e) {
            this.cols = e.response.columns;
        }
    });
 
$scope.mainGridOptions = {
        dataSource: customersSource, // OK
        columns: customersDataSource.cols, // undefined - uses default
        height: 500,
        scrollable: true,
        selectable: true
    };


Here is my JSON
{
  "data": [
    {
      "id": 0,
      "firstname": "Dalton",
      "lastname": "Holden",
      "gender": "male",
      "email": "daltonholden@tellifly.com",
      "phone": "871-407-2973",
      "address": "22 National Drive, Brenton, Louisiana",
      "birthday": "21/04/1965",
      "currency": "GBP"
    },
    {
      "id": 1,
      "firstname": "Allyson",
      "lastname": "Odom",
      "gender": "female",
      "email": "allysonodom@tellifly.com",
      "phone": "922-548-2725",
      "address": "44 Quincy Street, Thynedale, Georgia",
      "birthday": "28/08/1961",
      "currency": "CHF"
    },
    {
      "id": 2,
      "firstname": "Sweet",
      "lastname": "Branch",
      "gender": "male",
      "email": "sweetbranch@tellifly.com",
      "phone": "880-593-2244",
      "address": "81 Fenimore Street, Veguita, Missouri",
      "birthday": "08/08/1953",
      "currency": "AUD"
    }
  ],
 
  "columns": [
    {
      "field": "firstname",
      "title": "Frist Name",
      "width": 200,
      "attributes": {
        "class": "",
        "style": "text-align: left;"
      },
      "headerAttributes": {
        "class": "table-header-cell",
        "style": "text-align: left;"
      }
    },
    {
      "field": "lastname",
      "title": "Last Name",
      "attributes": {
        "class": "",
        "style": "text-align: left;"
      },
      "headerAttributes": {
        "class": "table-header-cell",
        "style": "text-align: left;"
      }
    },
    {
      "field": "gender",
      "title": "Gender",
      "attributes": {
        "class": "",
        "style": "text-align: left;"
      },
      "headerAttributes": {
        "class": "table-header-cell",
        "style": "text-align: left;"
      }
    },
    {
      "field": "email",
      "title": "e-mail",
      "attributes": {
        "class": "",
        "style": "text-align: left;"
      },
      "headerAttributes": {
        "class": "table-header-cell",
        "style": "text-align: left;"
      }
    },
    {
      "field": "phone",
      "title": "Phone Number",
      "attributes": {
        "class": "",
        "style": "text-align: right;"
      },
      "headerAttributes": {
        "class": "table-header-cell",
        "style": "text-align: right;"
      }
    },
    {
      "field": "address",
      "title": "Address",
      "attributes": {
        "class": "",
        "style": "text-align: left;"
      },
      "headerAttributes": {
        "class": "table-header-cell",
        "style": "text-align: left;"
      }
    },
    {
      "field": "birthday",
      "title": "Birthday",
      "attributes": {
        "class": "",
        "style": "text-align: center;"
      },
      "headerAttributes": {
        "class": "table-header-cell",
        "style": "text-align: center;"
      }
    },
    {
      "field": "currency",
      "title": "Currency",
      "attributes": {
        "class": "",
        "style": "text-align: center;"
      },
      "headerAttributes": {
        "class": "table-header-cell",
        "style": "text-align: center;"
      }
    }
  ]
}

I created a plunker of my testing project. As you can see i can fill the grid but i have a problem with the mainGridOptions.columns. Any help will be much appreciated!
http://plnkr.co/edit/5pjFQGkgTivqVkxsFBse

3 Answers, 1 is accepted

Sort by
0
Mihai
Telerik team
answered on 17 Jun 2014, 07:38 AM
Hi,

The Grid does not support defining columns after the widget is constructed.  For this case a quick workaround is to use the newly added k-ng-delay attribute, which can postpone widget initialization until the data is available.

I updated your plunker with a working example: http://plnkr.co/edit/g15JGq3YM7hG9w47CVHx?p=preview

It declares k-ng-delay="mainGridOptions" in the HTML, and in the controller, when the data comes in it will fill the $scope.mainGridOptions.

Hope this helps.

Regards,
Mihai
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Himanshu Shah
Top achievements
Rank 1
answered on 27 Jun 2014, 06:34 PM
Hi, 

Is angular-kendo.js included with plunker product release version? I cannot find it on gitHub. If not, when do you think it will be released? 

Himanshu
0
Mihai
Telerik team
answered on 30 Jun 2014, 10:41 AM
Hi,

I'm sorry, I don't quite understand the question.

If you mean to include angular-kendo.js in the list of libraries available on plnkr.co, I don't think that will happen because Angular-Kendo bindings will be built-in Kendo UI in the next Kendo version (already in Beta), and the angular-kendo.js repository will be deprecated.

Regards,
Mihai
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Data Source
Asked by
John
Top achievements
Rank 1
Answers by
Mihai
Telerik team
Himanshu Shah
Top achievements
Rank 1
Share this question
or