Giter Site home page Giter Site logo

royduin / laravel-nova-field-dynamic-select Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hubertnnn/laravel-nova-field-dynamic-select

14.0 2.0 27.0 399 KB

Dynamic Select field for Laravel Nova

Vue 38.88% JavaScript 2.32% PHP 58.72% SCSS 0.07%

laravel-nova-field-dynamic-select's Introduction

Dynamic select field for Laravel Nova (maintained repo)

This field allows you to dynamically fill contents of a select based on values in other dynamic select fields.

Field is based on nova-belongs-to-dependency but instead of selecting model, you can select custom values.

Installation

composer require royduin/laravel-nova-field-dynamic-select

Usage

Class has 5 special methods on top of default Select from Laravel Nova.

  • dependsOn can take a list of other fields this one depends on. On initial page load the list will be null, so make sure to check for null before accessing.
  • default can take a list of other fields this one depends on.
  • options can be either an array or a callable.
  • forAction to indicate that the dynamic select is running in an action.
  • colors (optional) to choose which colors to use when highlighting options. Accepts an array with 4 different hexadecimal colors:
    • highlightBgColor: Color of the background when the cursor is over an option
    • highlightTextColor: Color of the text when the cursor is over an option
    • selectedHighlightBgColor: Color of the background when the cursor is over a selected option
    • selectedHighlightTextColor: Color of the text when the cursor is over a selected option

If its a callable, it will receive array with selected dependency values as first argument and should return an array of items to be shown on the select field.

Example

public function fields(Request $request)
{
    return [
        ID::make()->sortable(),

        DynamicSelect::make('Country', 'country')
            ->options(['US' => 'United States', 'UK' => 'United Kingdom'])
            ->rules('required')
            ->colors([
                'highlightBgColor' => '#0f0',
                'highlightTextColor' => '#fff',
                'selectedHighlightBgColor' => '#f00',
                'selectedHighlightTextColor' => '#fff',
            ])
        ,

        DynamicSelect::make('Provider', 'provider')
            ->options(['PR' => 'Premium', 'ST' => 'Standard'])
            ->rules('required')
        ,

        DynamicSelect::make('Product', 'product')
            ->dependsOn(['country', 'provider'])
            ->options(function($values) { 
                if($values['country'] === 'UK' && $values['provider'] === 'PR') {
                    return ['A' => 'Fast shipping', 'B' => 'Normal shipping', 'C' => 'Free shipping'];
                } else {
                    return ['A' => 'Fast shipping', 'B' => 'Normal shipping'];
                }
            })
            ->default(function ($values) {
                if (! $values) {
                    return null;
                }
                if ($values['country'] === 'UK') {
                    return [
                        'label' => 'Normal shipping',
                        'value' => 'B'
                    ];
                }
            })
            ->rules('required')
        ,
    ];
}

Example for use in Nova Actions

public function fields(Request $request)
{
    return [
        ID::make()->sortable(),

        DynamicSelect::make('Country', 'country')
            ->forAction(self::class)
            ->options(['US' => 'United States', 'UK' => 'United Kingdom'])
            ->rules('required')
        ,

        DynamicSelect::make('Provider', 'provider')
            ->forAction(self::class)
            ->options(['PR' => 'Premium', 'ST' => 'Standard'])
            ->rules('required')
        ,

        DynamicSelect::make('Product', 'product')
            ->forAction(self::class)
            ->dependsOn(['country', 'provider'])
            ->options(function($values) { 
                if($values['country'] === 'UK' && $values['provider'] === 'PR') {
                    return ['A' => 'Fast shipping', 'B' => 'Normal shipping', 'C' => 'Free shipping'];
                } else {
                    return ['A' => 'Fast shipping', 'B' => 'Normal shipping'];
                }
            })
            ->rules('required')
        ,
    ];
}

laravel-nova-field-dynamic-select's People

Contributors

bnitobzh avatar crynobone avatar fredrikmikkelsen avatar hubertnnn avatar jbahapp avatar jesperbjoernandersen avatar kayacekovic avatar naoray avatar robertonegro avatar royduin avatar subzero10 avatar ttungbmt avatar zbignevg avatar

Stargazers

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

Watchers

 avatar  avatar

laravel-nova-field-dynamic-select's Issues

it doesn't install

No publishable resources for tag [laravel-assets]. and I can't find the folder of the package

`PS C:\Users\Manaf\Desktop\New folder\nofa> composer require royduin/laravel-nova-field-dynamic-select
Using version ^1.8 for royduin/laravel-nova-field-dynamic-select
./composer.json has been updated
Running composer update royduin/laravel-nova-field-dynamic-select
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals

  • Locking royduin/laravel-nova-field-dynamic-select (1.8.4)
    Writing lock file
    Installing dependencies from lock file (including require-dev)
    Package operations: 1 install, 0 updates, 0 removals
  • Downloading royduin/laravel-nova-field-dynamic-select (1.8.4)
  • Installing royduin/laravel-nova-field-dynamic-select (1.8.4): Extracting archive
    Package moontoast/math is abandoned, you should avoid using it. Use brick/math instead.
    Generating optimized autoload files

Illuminate\Foundation\ComposerScripts::postAutoloadDump
@php artisan package:discover --ansi
Discovered Package: digital-creative/clickable-table-row
Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: fruitcake/laravel-cors
Discovered Package: laravel/nova
Discovered Package: laravel/sail
Discovered Package: laravel/tinker
Discovered Package: laravel/ui
Discovered Package: manmohanjit/nova-belongs-to-dependency
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Discovered Package: orlyapps/nova-belongsto-depend
Discovered Package: royduin/laravel-nova-field-dynamic-select
Discovered Package: webparking/nova-belongs-to-dependency
Package manifest generated successfully.
83 packages you are using are looking for funding.
Use the composer fund command to find out more!
@php artisan vendor:publish --tag=laravel-assets --ansi
No publishable resources for tag [laravel-assets].
Publishing complete.`

Pivot Table select?

Any idea how to populate the Select from the Vendor Selected?

I am using a pivot table from Vendor Resource:

        `BelongsToMany::make(__('Vendors'), 'vendors', 'App\Nova\Vendor')
            ->fields( new VendorLocationFields()),`

and Fields file function:
`public function __invoke($request, $relatedModel)
{
$dist_centers = \App\Models\DistributionCenter::all()->pluck('name', 'id');

    return [
        Text::make('Account Number')
        Text::make('Vendor Username'),
        Select::make('Distribution Center','distribution_center_id')
            ->displayUsingLabels()
            ->options($dist_centers)
            ,
    ];
}`

Namespace not found?

Hi,

Whenever I download the package and put in the namespace for the dynamicselect I get this error:
image

Namespace im using is : use Royduin\LaravelNovaFieldDynamicSelect\DynamicSelect;

Nova version: 2.9.4

Commit 97484d3 broke something

Hello,

We've been using the package for some time and noticed the addition of Nova Actions changed how OptionsController::index() handles options and since then our code always run into

local.ERROR: Class name must be a valid object or a string {"userId":2,"exception":"[object] (Error(code: 0): Class name must be a valid object or a string at /srv/vendor/royduin/laravel-nova-field-dynamic-select/src/Http/Controllers/OptionsController.php:97)

Meaning $request->has('action') in line 16 is somehow filled when it shouldn't.

For now, we commented out this if so our code can run as before and we have no idea why the request has action set since we do not use it.

Not sure how to help debug, but if given pointers I'd be glad to help figure out what's going on.

Does not work in Action context

I'm currently trying to replace the MorphTo field in the action context, since that doesn't work, but for some reason, this fails as well.

The error message I get is: Call to a member function getOptions() on null

Nova Action Modal overflow-hidden clipping the DynamicSelect field

Using the DynamicSelect as a field in an Nova Action.

When you click to see the selections, the list gets clipped by the modal since its set to overflow-hidden by Nova. There's no way to modify the modal settings. And no way to modify the Vue.Multiselect structure.

I had to add in FormField.vue:

.multiselect__content-wrapper{
        position: relative;
    }

But this makes the modal expand and retract on dropdown list. Not optimal.

It is a known issue on for vue-multiselect shentao/vue-multiselect#723 but seems the project seems abandoned.

I wish there were a better way to fix this...

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.