GithubHelp home page GithubHelp logo

pos-lifestyle / laravel-nova-date-range-filter Goto Github PK

View Code? Open in Web Editor NEW
17.0 4.0 51.0 931 KB

A Laravel Nova date range filter.

License: MIT License

Vue 68.75% JavaScript 2.20% PHP 26.25% SCSS 2.80%

laravel-nova-date-range-filter's Introduction

Laravel Nova Date Range Filter

Packagist Packagist Version

About

This is a configurable and ready to use filter for Laravel Nova 2 based on Nova's own date filter that displays a date range picker.

Installation

To install the filter run the following command in your Laravel Nova project:

composer require pos-lifestyle/laravel-nova-date-range-filter

Usage

Simply add this filter to the filters method in your Nova resource.

use Illuminate\Http\Request;
use PosLifestyle\DateRangeFilter\DateRangeFilter;

class CustomResource extends Resource
{
    public function filters(Request $request): array
    {
        return [
            new DateRangeFilter(),
        ];
    }
}

By default, this will create a filter named "Created at" which applies the selected date range to the created_at database column.

Customization

The filter takes up to three arguments to customize it to your needs.

Parameter Type Description Default
Name String The name of the filter how it should appear in the filter dropdown list. "Created at"
Column String The name of the database column on which the selected date range should be applied to. Illuminate\Database\Eloquent\Model::CREATED_AT
Settings Array An array of settings to customize the filter. See the configuration section below for details. []

Configuration

All available settings are provided by the included Config enum. See the full example below how to use it.

Setting Type Description Default
ALLOW_INPUT Boolean Allows the user to enter a date directly into the input field. false
DATE_FORMAT String A string of characters which are used to define how the date will be displayed in the input box. The supported characters are defined in this table. "Y-m-d"
DEFAULT_DATE Array Sets the initial selected dates.

Supply an array of date strings which follow the format Y-m-d.
null
DISABLED Boolean Entirely disables the filter. false
ENABLE_TIME Boolean Enables the time picker. false
ENABLE_SECONDS Boolean Enables seconds in the time picker. false
FIRST_DAY_OF_WEEK Integer Sets the first day of the week (0 = Sunday, 1 = Monday etc.). If a custom locale is used, this setting has no effect. 0
LOCALE String Localizes the filter. Available locales can be found here. "default"
MAX_DATE String The maximum date that a user can pick to (inclusive). null
MIN_DATE String The minimum date that a user can start picking from (inclusive). null
PLACEHOLDER String The text that is shown in the empty input box. __('Choose date range')
SHORTHAND_CURRENT_MONTH Boolean Shows the month using the shorthand version (e.g. Sep instead of September). false
SHOW_MONTHS Integer The number of months that should be showed. 1
TIME24HR Boolean Displays the time picker in 24 hour mode without AM/PM selection when enabled. false
WEEK_NUMBERS Boolean Enables the display of week numbers in the calendar. false

Full Example

use Illuminate\Http\Request;
use PosLifestyle\DateRangeFilter\DateRangeFilter;
use PosLifestyle\DateRangeFilter\Enums\Config;

class CustomResource extends Resource
{
    public function filters(Request $request): array
    {
        return [
            new DateRangeFilter('Created at', 'created_at', [
                Config::ALLOW_INPUT => false,
                Config::DATE_FORMAT => 'Y-m-d',
                Config::DEFAULT_DATE => ['2019-06-01', '2019-06-30'],
                Config::DISABLED => false,
                Config::ENABLE_TIME => false,
                Config::ENABLE_SECONDS => false,
                Config::FIRST_DAY_OF_WEEK => 0,
                Config::LOCALE => 'default',
                Config::MAX_DATE => '2019-12-31',
                Config::MIN_DATE => '2019-01-01',
                Config::PLACEHOLDER => __('Choose date range'),
                Config::SHORTHAND_CURRENT_MONTH => false,
                Config::SHOW_MONTHS => 1,
                Config::TIME24HR => false,
                Config::WEEK_NUMBERS => false,
            ]),
        ];
    }
}

Screenshots

Default configuration Showing 2 months
Date range filter (default) Date range filter (2 months)

laravel-nova-date-range-filter's People

Contributors

cyruscollier avatar jcsoriano avatar jhae-de avatar niekbr avatar schroedan avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

laravel-nova-date-range-filter's Issues

Laravel 9 Compatibility

Please change laravel/framework dependency to ">=5.8" unless not compatible with Laravel 9.

Bug inserting database field name to filter class in request

Hi,

There is a bug that I do not understand why, but it inserts the database field to the class of the filter when sending over the filter to Square1\NovaMetrics\Custom*.

Solved by hardcoding the filter to:

            if ($filter->class === 'PosLifestyle\DateRangeFilter\DateRangeFilter_created_at') { // BUG, WHY???
                $filter->class = 'PosLifestyle\DateRangeFilter\DateRangeFilter';
            }

font awesome is not loaded

Hi,

nice package, only problem is, font awesome ist not loading, console gives 404

best regards
Denis

Bad UX when user selects only 1 date after previous range selected

When a user already has a range selected, and then attempts to change the range, but clicks off the datepicker before picking the "end date" for the range, the field in the filter just shows the placeholder and the originally selected date range is lost and unknowable after that point.

Ideally it would put back the old selected range so that even if the user doesn't pick a new range, it still lines up with the data in the view.

PSR-4 compliance

Composer 2.x won't autoload part of the package because the enums folder is not capitalized:

Class PosLifestyle\DateRangeFilter\Enums\Config located in ./vendor/pos-lifestyle/laravel-nova-date-range-filter/src/enums/Config.php does not comply with psr-4 autoloading standard. Skipping.

Nova 4 support

Hi, created an initial nova-4 branch, its not fully so i'm waiting with the PR. Putting this here so everyone can try.

https://github.com/sp4r74cus/laravel-nova-date-range-filter/tree/nova-4

  • Nova 4 requires PHP 8 and Laravel 8 so this could not replace the Nova 3 filter.
  • Nova 4 filters automatically focus the first filter, which cause the calendar to open (annoying and out of position) so i added a hidden input to steal the focus.
  • 2 months options breaks the responsive layout on smaller screens.

Screen Shot 2022-04-06 at 18 07 03

Date column name is unqualified by default

This is mostly a docs issue, but it might be nice to have an automated way of handling it. If you have a set of resources generated from some custom query in Nova, it's possible that the date column that this plugin uses might be ambiguous because it is not qualified with the table name. For example if you add the filter like this:

new DateRangeFilter('Updated', Model::UPDATED_AT),

and you have more than one table with an updated_at field in the selection, you'll get an integrity constraint exception. The workaround is simple: prefix the column name with a table qualifier when you add the filter:

new DateRangeFilter('Updated', 'posts.' . Model::UPDATED_AT),

I've not yet figured out a way to get hold of this table name automatically because we are in a static situation, but the table name for a model is dynamic, and we can't do something like self::$model->table to get hold of it.

Unable to chain methods for configuration options

Unlike the rest of nova fields, this filter allows configuration only from the constructor. This makes it difficult to modify the behavior of say an extended child class to add additional features that don't really fit. While it can be done, it's kind of ugly as you have to modify the internal config array.

Would you be open to changes that allow configuration via chainable methods instead of (or in addition to) the constructor config?

For example, instead of setting the default date range via the config constructor param, it could be called instead like:

(new DateRangeFilter())
     ->withDefaultRange('90 days ago, 'today')

That example takes it a step further and allows variable date ranging as well based on carbon parsing, but you get the idea for allowing configuration in a bit clearer way.

Can't upgrade to 1.3.3 because of package priority

I am getting the following error, when trying to require (need to upgrade to php8):

 require pos-lifestyle/laravel-nova-date-range-filter 1.3.3 -> satisfiable by pos-lifestyle/laravel-nova-date-range-filter[1.3.3] from composer repo (https://repo.packagist.org) but pos-lifestyle/laravel-nova-date-range-filter[dev-upgrade-to-laravel-8, dev-master, dev-bugfix/psr-4, 1.0.0, ..., 1.3.0] from vcs repo (github https://github.com/niekbr/laravel-nova-date-range-filter) has higher repository priority. The packages with higher priority do not match your constraint and are therefore not installable. See https://getcomposer.org/repoprio for details and assistance

Can you please change the priority?

Is that works with php8?

I supposed there is a typo in composer, ad php version should be >=7.1 rather then ^7.1 - it dosn't work with php8 now :)

Support of Laravel 8

Maybe just remove your laravel row in the require of your composer.json. Its not necessary I think.

[BUG] Filter reset behaviour

  1. Resetting all Nova filters doesn't clear the display value.
  2. Pressing the clear button in the input doesn't trigger Nova filter reset so filter icon remains blue as if still selected
  3. Font awesome not working

Display time on both months

When below configuration is set the time should display on both months not a single time for both months

Config::SHOW_MONTHS => 2,
Config::ENABLE_TIME => true,

Select which timezone to use

There is a problem when the Laravel configuration uses UTC by default and the frontend displays data in some other timezone.

As a general rule, using UTC in the backend is fine due to a whole lot of integrations in the monitoring system, but the lack of option to adjust the filter accordingly causes problems when the records being shown include dates outside the selected range.

To be clear, if we consider all date/time in UTC, there is no bug! The problem resides in the fact all data is being displayed in UTC-4 and the filter selects data outside the intended range...

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.