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

Can't get grid to display JSON rows when using transport (does work when using data)

1 Answer 47 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Phillip
Top achievements
Rank 1
Phillip asked on 22 May 2019, 08:39 AM

I've been trying to use the PHP wrapper for UI and get the grid to display my JSON data. If I set $dataSource->data($json) where $json is the result from my call it works, but when I set it to $dataSource->transport($transport) where $transport contains my read transport I get no rows to display in the grid.

 

I'm using the CodeIgniter framework and have the require statements as part of my autoload, so they are there along with the requirements in the header.

 

Here is my code:

<?php

//$json = json_decode(file_get_contents(site_url('data/sys_settings/read')),true);
$transport = new \Kendo\Data\DataSourceTransport();

$create = new \Kendo\Data\DataSourceTransportCreate();
$create->url(site_url('data/sys_settings/create'))
     ->contentType('application/json')
     ->type('POST');

$read = new \Kendo\Data\DataSourceTransportRead();
$read->url(site_url('data/sys_settings/read'));

$update = new \Kendo\Data\DataSourceTransportUpdate();
$update->url(site_url('data/sys_settings/update'))
     ->contentType('application/json')
     ->type('POST');

$destroy = new \Kendo\Data\DataSourceTransportDestroy();
$destroy->url(site_url('data/sys_settings/destroy'))
     ->contentType('application/json')
     ->type('POST');

$transport->read($read)
       ->parameterMap(new \Kendo\JavaScriptFunction('function(data) {
              return kendo.stringify(data)
          }'));


$idField = new \Kendo\Data\DataSourceSchemaModelField('id');
$idField->type('number')
->editable(false)
->nullable(false);

$repId = new \Kendo\Data\DataSourceSchemaModelField('rep_id');
$repId->type('number')
->editable(false)
->nullable(false);

$col1Field = new \Kendo\Data\DataSourceSchemaModelField('key');
$col1Field->type('string')
->editable(false)
->nullable(false);

$col2Field = new \Kendo\Data\DataSourceSchemaModelField('val');
$col2Field->type('string')
->editable(true)
->nullable(true);

$model = new \Kendo\Data\DataSourceSchemaModel();
$model->addField($idField)
->addField($repId)
    ->addField($col1Field)
    ->addField($col2Field);

$schema = new \Kendo\Data\DataSourceSchema();
$schema->data('data')
       ->errors('errors')
       ->model($model)
       ->total('total');



$dataSource = new \Kendo\Data\DataSource();
$dataSource->transport($transport)
           ->pageSize(20)
->serverFiltering(true)
->type('odata')
           ->schema($schema);



$col1 = new \Kendo\UI\GridColumn();
$col1->field('key')
          ->width(120)
          ->title('Settings');

$col2 = new \Kendo\UI\GridColumn();
$col2->field('val')
          ->width(120)
          ->title('Value');

$command = new \Kendo\UI\GridColumn();
$command->addCommandItem('edit')
        ->addCommandItem('destroy')
        ->title('&nbsp;')
        ->width(250);

$grid = new \Kendo\UI\Grid('grid');
$grid->addColumn($col1,$col2,$command)
->dataSource($dataSource)
->addToolbarItem(new \Kendo\UI\GridToolbarItem('create'))
->editable('inline')
->sortable(true);


echo $grid->render();

 

 

 

here is my JSON being sent from the URL:

{"data":[{"id":"1","rep_id":"0","key":"default_country","val":"USA"},{"id":"2","rep_id":"0","key":"default_state","val":"CA"},{"id":"4","rep_id":"0","key":"default_account_type","val":"Customer"},{"id":"5","rep_id":"0","key":"default_title","val":null}],"total":4,"errors":null}

 

 

 

I'm not sure what I'm doing wrong, but I can't get it to work via the transport.  Any help would be greatly appreciated.

1 Answer, 1 is accepted

Sort by
0
Phillip
Top achievements
Rank 1
answered on 22 May 2019, 09:19 AM

I figured out my problem!  My read object needed dataType set.  

$read = new \Kendo\Data\DataSourceTransportRead();
$read->url(site_url('data/sys_settings/read'))
->dataType('json');

 

After adding the dataType('json'), the grid displayed the data as expected.

Tags
Data Source
Asked by
Phillip
Top achievements
Rank 1
Answers by
Phillip
Top achievements
Rank 1
Share this question
or