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

Export to Excel

Open samankassou opened this issue 4 years ago • 3 comments

Is it possible to use the query result to make actions like export to PDF or Excel? How to manage the filtered result for doing some extra action?

samankassou avatar Jul 13 '21 10:07 samankassou

Hi @samankassou, actually it is, there is query property on every view that you can access to, it is mostly for internal purposes, but as all the actions get the view instance as a param, they have access to this property.

public function handle($model, View $view)
{
    dd($view->query);        
}

The downside is that it only has the paginated results.

Gustavinho avatar Jul 13 '21 14:07 Gustavinho

Yes @Gustavinho I saw it but this $view is accessible in an action, it means that we can perform only actions related to a row. I know that we can register it in the bulkActions method but we have another problem, the user needs to select some rows before firing this action and more the data is paginated, this means that we can only have access to the 'visible' data like you mentioned it. I have tried to solve the problem, but I know that it is not the 'good' way to this but it worked! these are the steps:

  1. I make a button that emit an exportToExcel livewire event
  2. In my TableView, I added listener for this event
  3. In the method that handle this event, I use the getRenderData() bult-in method to get the LengthAwarePaginator array
  4. I use the items key of the paginator to get the items and do my logic to export it to excel;

The problem is that I have only the first page of the paginated result I added an 'items per page' dropdown that user can set for the number of elements he wants to show before export them but It is not a good solution I think

samankassou avatar Jul 14 '21 20:07 samankassou

Hi yes you're right, what I mentioned is for individual actions, and using a bulk action would make the user to select some rows before.

About what you're doing, I just would change the numbers 3 and 4, since you can have access to the $this->query property, it has the current query, the downside is that it is the paginated query as you saw.

I can do some internal refactors to have access to the query without pagination so we can use the whole data.

Either way, I think there is some room for a new type of feature, something like "table actions"

Gustavinho avatar Jul 16 '21 15:07 Gustavinho