Ako spravne pracovat s JSONom v komponente
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
- steelbull
- Člen | 241
Chcel by som do Nette projektu implementovat KendoUI Datagrid. Datagrid som naimplementoval ako komponent a ten vykreslujem v sablone.
ALE: Neviem, ako by som mal spravne riesit JSON, keby som mal vsetko „natlacene“ v presenteri, tak by mi to ani tak nevadilo, ale ked ma byt komponent znovupouziteny, mala by byt JSON komunikacia jeho sucastou a ja ju mam v presenteri, co asi nie je OK.
class LaboratoryPositionsGridControl extends Control
{
public function render()
{
// Neviem, ako spravne poriesit ten JSON
$transport = new Kendo\Data\DataSourceTransport();
$read = new Kendo\Data\DataSourceTransportRead();
$read->url('/msa/www/my_presenter?do=loaddata')
->contentType('application/json')
->type('POST');
$transport->read($read)
->parameterMap('function(data) {
return kendo.stringify(data);
}');
$model = new Kendo\Data\DataSourceSchemaModel();
$titleField = new Kendo\Data\DataSourceSchemaModelField('title');
$titleField->type('string');
/*
.
.
.
*/
$model->addField($titleField);
$schema = new Kendo\Data\DataSourceSchema();
$schema->data('data')
->errors('errors')
->groups('groups')
->model($model)
->total('total');
$dataSource = new Kendo\Data\DataSource();
$dataSource->transport($transport)
->pageSize(100)
->serverPaging(true)
->serverSorting(true)
->serverGrouping(true)
->schema($schema);
$grid = new Kendo\UI\Grid('grid');
$titleColumn = new Kendo\UI\GridColumn();
$titleColumn->field('title')
->title('Sign')
->width(100);
$grid->addColumn($titleColumn)
->dataSource($dataSource)
->sortable(true)
->filterable(true)
->pageable($pageable)
->attr('style', 'height:600px');
echo $grid->render();
}
…a v presenteri mam toto:
public function handleLoaddata()
{
if ($this->isAjax()) {
$request = json_decode($this->getHttpRequest()->getRawBody());
// Datasource som upravil na Nette\Database
$result = new \Helpers\KendoUI\DataSourceResult($this->context);
$json = $result->read('view_lab_positions', array(
'title'), $request);
$this->payload->total = $json['total'];
$this->payload->data = $json['data'];
$this->sendPayload();
}
}
Editoval steelbull (25. 6. 2015 12:32)