nemrutco / nova-global-filter

This package allows you to apply any of your existing Laravel Nova filters to your cards in Dashboards or Index and Detail view of your nova resources.
MIT License
39 stars 32 forks source link

Implementing on custom cards #18

Closed xoco70 closed 4 years ago

xoco70 commented 4 years ago

Hi,

Nice plugin, very useful.

I'm trying to use it on a custom card, based on Nova-charts

If I can send the filter into calculate() method for trends, how should I send it to a custom card.

Here is my Card definition:

class BarChart extends Card
{
    use GlobalFilterable;
    /**
     * The width of the card (1/3, 1/2, or full).
     *
     * @var string
     */
    public $width = 'full';

    public function __construct($component = null)
    {
        parent::__construct($component);
    }

    /**
     * Get the component name for the element.
     *
     * @return string
     */
    public function component()
    {
        return 'bar-chart';
    }

    public function series(array $series): self
    {
        return $this->withMeta([ 'series' => $series]);
    }

    public function type(string $type): self
    {
        return $this->withMeta([ 'type' => $type ]);
    }

    public function options(array $options): self
    {
        return $this->withMeta([ 'options' => (object) $options ]);
    }

    public function animations(array $animations): self
    {
        return $this->withMeta([ 'animations' => $animations ]);
    }

    public function title(string $title): self
    {
        return $this->withMeta([ 'title' => $title ]);
    }

    public function model(string $model): self
    {
        return $this->withMeta([ 'model' => $model ]);
    }

    public function join(string $joinTable, string $joinColumnFirst, string $joinEqual, string $joinColumnSecond): self
    {
        return $this->withMeta([ 'join' => ['joinTable' => $joinTable, 'joinColumnFirst' => $joinColumnFirst, 'joinEqual' => $joinEqual, 'joinColumnSecond' => $joinColumnSecond] ]);
    }
}
MuzafferDede commented 4 years ago

Firstable you have to let your custom card listen and react to global filter changes. I have mentioned briefly here

And than you can use GlobalFilterable trait on your custom card and access filtered model like so

    // Filter your model with existing filters
    $model = $this->globalFiltered(YourModel::class,[
      YourFilter::class 
    ]);

    // Do your thing with the filtered $model
    return $this->countByDays($request, $model);