Coder Social home page Coder Social logo

nova-column-filter's Introduction

Column Filter for Laravel Nova

A Laravel Nova column querier filter for Nova > v1.1.8

Prior to Nova v.1.3.2, use the 0.2.1 version.

Prior to Nova v.1.1.8, use the 0.1.1 version.

Demo

Demo

Installation

Run this command in your Laravel Nova project:

composer require philperusse/nova-column-filter

Usage

Create a filter with artisan

 $ php artisan nova:filter UserColumnFilter 

Extend your filter class with the ColumnFilter class instead of Nova's base Filter class and add the columns on which you want to filter on in the options

use \philperusse\Filters\ColumnFilter as Filter;

class ColumnFilter extends Filter
{
  
   /**
    * Apply the filter to the given query.
    *
    * @param  \Illuminate\Http\Request  $request
    * @param  \Illuminate\Database\Eloquent\Builder  $query
    * @param  mixed  $value
    * @return \Illuminate\Database\Eloquent\Builder
   */	 
  public function apply( Request $request, $query, $value )
  {
     $args = collect($value)->values()->filter(); //Remove any empty keys.
     if($args->isEmpty())
          return $query;
		  
      return $query->where(...$args->all());
  }
  
  /**
   * Get the filter's available options.
   *
   * @param  \Illuminate\Http\Request  $request
   * @return array
   */
  public function options( Request $request ) : array
    {
        return array_merge(parent::options($request), [
            'columns' => [
                'name'      => 'Name',
                'age'       => 'Age',
            ]
        ]);
    }
}

Customization

You can customize the operator list by overriding the operators key in the filters options.

use philperusse\Filters\ColumnFilter as Filter;

class ColumnFilter extends Filter
{
    public function options( Request $request ) 
    {
        return array_merge(parent::options($request), [
            'columns' => [
                'name'      => 'Name',
                'age'       => 'Age',
            ],
            'operators' => [
                '='     => '=',
                '>'     => '>',
                '>='    => '≥',
                '<'     => '&lt;',
                '<='    => '&le;',
            ]
        ]);
    }

    protected function componentName()
    {
        return 'column-filter';
    }
}

Contributions

All contributions are welcomed. Please send a PR

Authors

The filter is loosely-based on 64Robots's Date Filter and its custom filter selector component

License

This package is open-sourced software licensed under the MIT Licence

nova-column-filter's People

Contributors

ahmedkandel avatar brandonsurowiec avatar philperusse avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

nova-column-filter's Issues

Multiple instances

I figured this would be really useful if we every time you add a filtering condition, a new row is added to add even more conditions.

Let's say you want to filter users by country AND city.

I'm happy to make a PR if this is something you'll be willing to merge.

Any ideas on how to do it?

Empty drop-down lists

Hi Philippe,

The package was working correctly but later on found that the columns and operators drop-down are empty:

screenshot_1

Maybe the reason is a Nova update currently I have the latest version v1.3.2

here is the filter code:

namespace App\Nova\Filters;

use Illuminate\Http\Request;
use philperusse\Filters\ColumnFilter as Filter;

class UserColumnFilter extends Filter
{
    /**
     * Set the displayable name of the filter.
     *
     * @return string
     */
    public function name()
    {
        return __('User Query');
    }

    /**
     * Apply the filter to the given query.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @param  mixed  $value
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function apply(Request $request, $query, $value)
    {
        $args = collect($value)->values()->filter(); //Remove any empty keys.
        if($args->isEmpty()) return $query;
        if($args[1] == 'LIKE') $args[2] = '%' . $args[2]. '%'; //Add the surrounding % if needed;

        return $query->where(...$args);
    }

    /**
     * Get the filter's available options.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function options(Request $request)
    {
        return array_merge(parent::options($request), [
            'columns' => [
                'id' => 'ID',
                'code' => 'Code',
                'name' => 'Name',
            ],
            'operators' => [
                'LIKE' => 'Contains',
                '=' => '&equals;',
            ]
        ]);
    }
}

Thanks.

Like operator?

Is there any way to use this to do a "like" operator, I know the value would have to be wrapped in '%' values as well. It would be awesome if this was possible. I don't understand how the filters work quite well enough to figure out how to tweak this to work, although I did look around and try. :) If it's possible please let me know!!

Nova 1.3.2 dependency causing composer install issues

It seems since I install nova by using a local path, it ends up being a "dev" version, so composer is not finding anything to satisfy the dependency on "laravel/nova" : ">=1.3.2".

Problem 1
- The requested package laravel/nova ^2.0 exists as laravel/nova[dev-develop] but these are rejected by your constraint.

Problem 2
- philperusse/nova-column-filter v0.3.0 requires laravel/nova >=1.3.2 -> no matching package found.
- philperusse/nova-column-filter v0.3.0 requires laravel/nova >=1.3.2 -> no matching package found.
- Installation request for philperusse/nova-column-filter ^0.3.0 -> satisfiable by philperusse/nova-column-filter[v0.3.0].

Any idea on how to get around this?

Cannot call abstract method parent::options() error with Nova 3.5 latest dev-master

I am experiencing the same issue #4

image

Here is the full class

<?php

namespace App\Nova\Filters;

use Illuminate\Http\Request;
use Laravel\Nova\Filters\Filter;

class ClientColumnFilter extends Filter
{
    /**
     * The filter's component.
     *
     * @var string
     */
    public $component = 'select-filter';

    /**
     * Apply the filter to the given query.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @param  mixed  $value
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function apply(Request $request, $query, $value)
    {
        $args = collect($value)->values()->filter(); //Remove any empty keys.
        if ($args->isEmpty()) {
            return $query;
        }

        return $query->where(...$args->all());
    }

    /**
     * Get the filter's available options.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function options(Request $request)
    {
        return array_merge(parent::options($request), [
            'columns' => [
                'fname' => 'First Name',
                'lname' => 'Last Name',
            ],
        ]);
    }
}

Column as text input

I'm trying to use this Filter with a json column using MySQL 5.7 features. I do not know beforehand which attributes are going to be present. So I'd rather have:

  • input (column)
  • select (operator)
  • input (value)

instead of:

  • select (column)
  • select (operator)
  • input (value)

I managed to change that behavior simply editing the vendor folder, but I don't know how to manage that kind of override from userland in Laravel Nova.

Is there any way to override a filter Vue template?

And, also, will you be willing to merge a PR that introduced a feature like this?

How to increase window of Nova Filter pane

Hey there,

First of all, thank you for this nice addition to the Filter tool, makes it MUCH more usable.

I am trying to find a way to increase the size of the window as the field where column row value is pretty small, just trying to make it a bit easier on the eyes.

Thanks!

Unexpected token u in JSON at position 0

vendor.js?id=52886433f04b87b2c184:1 SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse ()
at a.initializeFilterValuesFromQueryString (vendor.js?id=52886433f04b87b2c184:1)
at a. (app.js?id=00a62d05e5a9a1a62b85:1)
at Ne.I3G/.Ne.run (vendor.js?id=52886433f04b87b2c184:1)
at Se (vendor.js?id=52886433f04b87b2c184:1)
at Array. (vendor.js?id=52886433f04b87b2c184:1)
at MessagePort.Gt (vendor.js?id=52886433f04b87b2c184:1)

Default select values

Very useful plugin. Thank you for that ;)

Is there by chance anyway to set default values for the columns and operators select fields ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.