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

Many foreign keys in one grid...

0 Answers 81 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Fedon
Top achievements
Rank 1
Fedon asked on 24 Sep 2012, 05:40 PM
The Grid:
$('#' + idGrid).kendoGrid({
  dataSource: dataSource,
  height: 400,
  toolbar: [{name: "create", text:"Create County"}],
 columns: [
      { field:"_id", title: "ID", width: "50px" },
      { field: "_status", values: status, title:"Status", width: "100px", editor: status_dropdown },
      { field: "_country_id", title: "Country", values: countries, width: "50px", editor: country_dropdown },
      { field: "_county_id", title: "County", values: counties, width: "50px", editor: county_dropdown },
      { field: "code", title: "Code", width: "150px" },
      { field: "name", title: "Name", width: "150px"},
      { field: "localname", title: "Localname", width: "150px"},
      { command: ["edit", "destroy"], title: " ", width: "200px" }],
      editable: "popup",
      sortable: {
       mode: "multiple",
       allowUnsort: true
      },
      filterable: true,
      resizable: true,
      groupable: true,
      pageable: {
    pageSize: 30,
    refresh: true
      }
});

the fields "_status", "_country_id", "_county_id" have integer values in database, but we want to show string values to the user. So we use the "values" parameter that is an array. In the case of _status for example we use:

var status = [{
  "value": 0,
  "text": "Disabled"
  }, {
  "value": 1,
  "text": "Enabled"
}];

Everything works fine...

The other two, however are arrays that are created from datasources that are populated from the server. That means the action is asynchronous. So if you try creating datasource->array->grid asynchronously the array might not be populated so the grid shows integer values instead.

If i have one such datasource i use the "change" event of the datasource to create the array and then the grid. For example:
var countiesData;
     var counties = new Array();
     var countiesDataSource = new kendo.data.DataSource({
       transport: {
         read: {
           url: "../order_new/county.php"
         }
       },
        schema: {
           data: "data"
         },
         change: function(e) {
                  countiesData = this.data();
                  console.log(countiesData);
                  var counties = new Array();
                  for (var i = 0; i < countiesData.length; i++) {
                    var dataItem = countiesData[i];
     
                    counties.push({
                      "value": parseInt(dataItem._id),
                      "text": dataItem.name
                    });
                  }
                 //here goes the previous code of grid creation
}

This works just fine... But what should i do now that i have two (or maybe more) foreign keys that need integer->string modification?
Just to know, i have tried creating the second datasource inside the change event of the first and then creating the grid inside the change event of the second. Doesn't work! And is far from elegant solution. Any ideas telerik team and users?

No answers yet. Maybe you can help?

Tags
Grid
Asked by
Fedon
Top achievements
Rank 1
Share this question
or