zofe / rapyd-laravel

deprecated rewritten in rapyd-livewire
MIT License
866 stars 297 forks source link

Datasource from an array, stdclass object or a json/xml RESTful API #190

Closed Maykonn closed 9 years ago

Maykonn commented 9 years ago

Is possible set datasource from an array stdclass object or a RESTful API call:

DataGrid::source('http://myapi.com/users');
DataGrid::source([]);
DataGrid::source(json_decode('[{}]'));
zofe commented 9 years ago

since it works with multidimensional arrays, it is possible to bind something like:

DataGrid::source((array)json_decode(file_get_contents('http://myapi.com/users')))

untested

Maykonn commented 9 years ago

Ok, thanks!

zofe commented 9 years ago

i think you can do:

DataGrid::source(json_decode(file_get_contents('http://myapi.com/users'),true))
Maykonn commented 9 years ago

Yes, it works fine. I write wrappers for my external API, that make requests using Guzzle:

\DataGrid::source(\External::call('/search/pagination/10,0/sort/desc?q=my-search&filters=status:!inactive|user_id:10', \Request::GET)
    ->getResponseBodyAsArray());

Now I will implement pagination, sort, etc. Thanks @zofe!

Maykonn commented 9 years ago

@zofe When I try to add a column from multidimensional array:

$grid->add('title','Title', true);
$grid->add('category.name','Category', true); 

The title column is ok, but the category.name is write as 'category.name' instead the product category name.

zofe commented 9 years ago

the dot syntax is translated as "relation.fieldname", you should find an alternative:

maybe {{ $row['category.name'] }} ?

Maykonn commented 9 years ago

Sorry, but I don't understand:

maybe {{ $row['category.name'] }} ?

I'm try to make it, but not work, I think that I don't understand =):

$grid->add("{{$row['category.name']}}",'Category', true);
zofe commented 9 years ago

try to change the problematic key in your resultset: something like:

$rows = json_decode(file_get_contents('http://myapi.com/users'),true);
$rows = array_map(function($row) {
    $cleanrow = $row;
    $cleanrow['category_name'] = $row['category.name'] ;
    return $cleanrow;
}, $rows);

then you can use $grid->add("category_name",'Category', true);

Maykonn commented 9 years ago

I will write a layer to translate the API incompatible responses to app core(a wrapper for my \External call responses, something like a Context Map from DDD). Thanks!