rapyd-laravel icon indicating copy to clipboard operation
rapyd-laravel copied to clipboard

DataGrid buildCSV not respecting filter

Open csogilvie opened this issue 8 years ago • 7 comments

If I have a datagrid which is filtered to a specific subset of records, then I call 'buildCSV' at the end of the method, then the CSV contains the unfiltered results.

I would expect it to produce just the unfiltered results?

$filter = \DataFilter::source(new Member);
$filter->add('membership_id', 'Membership ID', 'number');
$filter->add('firstName', 'First Name', 'text');
$filter->add('lastName', 'Last Name', 'text');
$filter->submit('search');
$filter->reset('reset');

$grid = \DataGrid::source($filter);
$grid->add('membership_id','Member ID', true)->cell( function( $value, $row) {return sprintf('<a href="%s">%s</a>', url('member/show', [$value]), $value);});
$grid->add('firstName', 'First Name', true);
$grid->add('lastName', 'Last Name', true);

// Returns all the records in the database
return $grid->buildCSV('export_articles', 'Y-m-d.His');

// Returns a paginated list of the filter
//$grid->paginate(20);
//return view('member.list', compact('filter', 'grid'));

csogilvie avatar Jul 17 '16 06:07 csogilvie

You probably need to manually call the build method:

// Returns all the records in the database
$grid->build();
return $grid->buildCSV('export_articles', 'Y-m-d.His');

Let me know if it works for you.

tacone avatar Jul 19 '16 14:07 tacone

Hi @tacone, I'm afraid that didn't seem to make much difference - it still shows me the full data.

csogilvie avatar Jul 20 '16 22:07 csogilvie

If I try ->build or ->buildCSV, I get a sub-class of \Illuminate\Database\Eloquent\Model and if I try the "output' method, I get a sub-class of \Illuminate\Database\Eloquent\Builder instead in Zofe\Rapyd\DataSet's build method.

I don't know this code well enough to debug further though.

csogilvie avatar Jul 20 '16 22:07 csogilvie

you've to do $filter->build() on the filter before $grid->buildCSV() on the grid

zofe avatar Jul 21 '16 08:07 zofe

@zofe That works - but I find it odd that building it for a view would work but building it for a CSV requires extra steps? I still consider that to be an issue, personally.

csogilvie avatar Jul 21 '16 11:07 csogilvie

if (some condition) {
            return $grid->buildCSV('export_articles', 'Y-m-d.His');
}
return view ..

"some condition" can be .. a qs param or an uri node..

zofe avatar Jul 22 '16 09:07 zofe

I think I similar issue. This method work for my case.

$url = new Zofe\Rapyd\Url(); $grid->link($url->append('export',1)->get(),"Export to Excel", "TR");

https://github.com/zofe/rapyd-laravel/issues/312

CHOMNANP avatar Oct 03 '16 06:10 CHOMNANP