Giter Club home page Giter Club logo

nova-ajax-select's Introduction

Nova Ajax Select

Latest Version on Github Total Downloads Twitter Follow

Ajax populated select fields based on the values of other fields and when they change.

nova-ajax-select

Install

composer require dillingham/nova-ajax-select

Usage

Specify a request url & optionally the parent($attribute) to watch & trigger the ajax select:

use NovaAjaxSelect\AjaxSelect;
BelongsTo::make('Company'),

AjaxSelect::make('User')
    ->get('/api/company/{company}/users')
    ->parent('company'),

Add the field for index & detail views display. AjaxSelect is for forms only

BelongsTo::make('User')->exceptOnForms(),

Request Url:

In the above example, we say company is the parent.

The {company} url parameter will equal the selected Company field value.

Response Format:

The select field expects a value & display. Map your results like so:

Route::get('api/company/{company}/users', function($company_id) {

    $company = \App\Company::find($company_id);

    return $company->users->map(function($user) {
        return [ 'value' => $user->id, 'display' => $user->name ];
    });
})->middleware(['nova']);

Make children depend on other children

City makes a request based on State, which makes a request based on Country:

Select::make('Country')
    ->options([]),

AjaxSelect::make('State')
    ->get('/api/country/{country}/states')
    ->parent('country'),

AjaxSelect::make('City')
    ->get('/api/state/{state}/cities')
    ->parent('state'),

Make multiple children depend on one parent

File & Comment will both make a request based on Project

BelongsTo::make('Project'),

AjaxSelect::make('File')
    ->get('/{project}/files')
    ->parent('project'),

AjaxSelect::make('Comment')
    ->get('/{project}/comments')
    ->parent('project'),

Author

Hi ๐Ÿ‘‹, Im Brian Dillingham, creator of this Nova package and others

Hope you find it useful. Feel free to reach out with feedback.

Follow me on twitter: @im_brian_d

nova-ajax-select's People

Contributors

dillingham 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  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  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

nova-ajax-select's Issues

Translate 'Choose an option'

First option in ajax select is 'Choose an option'. How do i translate this?

Tried a vendor translation, but this doesn't work:

/resources/vendor/nova-ajax-select/nl.json

{ "Choose an option": "Maak een keuze" }

Save resource > wrong attribute name

When i try to save a resource, it tries to save the wrong attribute name. How do i make a difference between the method client() that gets the attribute and client_id that needs to be saved?

In this example 'client' is a method on a orgizational level model. When saving this it should be client_id instead of client.

use App\Nova\Client\Client as ClientResource;
`return AjaxSelect::make(ClientResource::singularLabel(), 'client')
    ->get(config('nova.path') . '/orglevel/distributors/{distributor}/clients')
    ->parent('distributor')
    ->rules('required'); 

Can you not set with a withMeta belongsToId?

Laravel 5.8.11
Nova 2.0.1
dillingham/nova-ajax-select 1.0.2

Thank you for the package.

Is there a way to set the selected default index on load? I'm trying to do something like the following.

AjaxSelect::make('Contact', 'contact_id')
->get('/api/builder/{builder}/contacts')
->parent('builder')
->withMeta([
'belongsToId' => $contactId,
])
->rules('required'),

Thank you for your time.

Question: can a parent attribute be optional?

In the documentation it's stated that the parent attribute could be optional.

Specify a request url & optionally the parent($attribute) to watch & trigger the ajax select:

What do you mean with optional? If i don't use the parent attribute, it doesn't seem to work.

Using 'nova' middleware causes 401

Using the nova middleware is causing a 401:

Route::group(['middleware' => ['nova'], 'namespace' => 'Api'], function() {
    Route::get('city/{city}/neighborhoods', 'CityNeighborhoodController@show');
});

I am successfully logged in on Nova v2.11.1

When I take out the middleware it works correctly. I do not have any policies for the models or other auto for the controller.

Select cleared when loading with existing value

Nice work with the package.

Nova: 1.3.2
dillingham/nova-ajax-select: 1.0.1

When the control is loaded with an initial value (such as on an update form), the initial value is cleared when the options are loaded.

This appears to be happening in the updateOptions() call, where this.value is always set to null. I think a solution would be to only set the value to null if it does not exist in the updated options.

This would have the added benefit of retaining the value if it appears in both the new and old options, currently it is always cleared.

Searchable results

Thank you for the awesome package!

Just a feature request: would be great if we could add a ->searchable to the results so that we can easily find stuff from big results.

Component does not Render

I followed the instructions on how to use and install the component. It is not rendering at all or throwing any errors. So I don't know what I need to do in order to get it to work.

Parent BelongsTo searchable does not work

I'm trying to setup a relationship between a league and a competition_matchday, for it fails when BelongsTo is set as searchable. I'm guessing this is because searchable is not a regular dropdown.

BelongsTo::make( 'League', 'league', League::class )
  ->nullable()
  ->searchable(),
AjaxSelect::make( 'Matchday', 'competition_matchday' )
  ->nullable()
  ->get( '/api/league/{league}/matchdays' )
  ->parent( 'league' ), 

Request: feedback when no options are available

If no options are returned, the user gets no response.

For example: Say i have a company with several departments. One department is missing workers. When i select that department, nothing happens.

Would be nice if the text 'Choose an option' changes to 'No options'.

Argument 3 error after 2.0.3 upgrade

Thanks for the awesome package! Very much appreciated.

After the most recent Nova upgrade, I get this error:

local.ERROR: Argument 3 passed to Laravel\Nova\Fields\Field::__construct() must be callable or null, string given, called in/vendor/laravel/nova/src/Element.php on line 46 {"userId":5,"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Argument 3 passed to Laravel\Nova\Fields\Field::__construct() must be callable or null, string given, called in /vendor/laravel/nova/src/Element.php on line 46 at /vendor/laravel/nova/src/Fields/Field.php:144

Here's my code which did work before:

AjaxSelect::make('Location', 'brandLocation', 'App\Nova\Brands\Locations\BrandLocation')->onlyOnForms()
                ->get('/api/nova/brands/{brand}/locations')
                ->parent('brand'),

Any assistance would be greatly appreciated. Maybe I'm doing something wrong?

Support for edit of existing resource?

When i try to edit an existing resource, i expect the value to be pre-selected. However, this is not the case. I'm missing something, but what am i doing wrong?

use App\Nova\Client\Client as ClientResource;

return \NovaAjaxSelect\AjaxSelect::make(ClientResource::singularLabel(), 'client') ->get(config('nova.path') . '/orglevel/distributors/{distributor}/clients') ->parent('distributor') ->rules('required');

Add support [showCreateRelationButton]

Hi, thanks for great package.

It would be perfect if you add a feature to create a new Resource by using showCreateRelationButton in AjaxSelect input.

Allow multiple parents

I am using nova-ajax-select and I have a field that depends on two parent fields, another parent field can be added in some way. Currently it only allows me to do it for one, I already modified the route and it works correctly I send the data as the nova-ajax-select interprets the problem is in the field in the resource that is only taking the value of a parent.

How do you pre-populate this values on edit form?

I have dependency with Field A -> Field B -> Field C. I am storing only Field C in database. With AjaxSelect when creating the record was quite straight forward. How should I approach this when editing the form? Would like to to prefill all the fields, based on Field C.

Thanks,
Tanmay

Request: autoselect single option

Would be great if one option is returned and the field is required, this option is auto-selected. I would consider this better UX.

Cannot read properties of undefined (reading '_c')

I have a js issue:

runtime-core.esm-bundler.js:226 TypeError: Cannot read properties of undefined (reading '_c')
    at Proxy.render (nova-ajax-select:1:156677)
    at Fn (runtime-core.esm-bundler.js:888:44)
    at we.fn (runtime-core.esm-bundler.js:5600:57)
    at we.run (reactivity.esm-bundler.js:185:25)
    at R.e.update (runtime-core.esm-bundler.js:5714:56)
    at R (runtime-core.esm-bundler.js:5728:9)
    at j (runtime-core.esm-bundler.js:5510:9)
    at N (runtime-core.esm-bundler.js:5468:17)
    at g (runtime-core.esm-bundler.js:5058:21)
    at E (runtime-core.esm-bundler.js:5254:13)
runtime-core.esm-bundler.js:226 TypeError: Cannot read properties of undefined (reading 'filter')
    at Proxy.watchedComponents (nova-ajax-select:1:2112)
    at we.run (reactivity.esm-bundler.js:185:25)
    at get value [as value] (reactivity.esm-bundler.js:1147:39)
    at Object.get [as watchedComponents] (runtime-core.esm-bundler.js:3436:30)
    at Object.get (runtime-core.esm-bundler.js:3109:24)
    at Proxy.mounted (nova-ajax-select:1:1779)
    at nn (runtime-core.esm-bundler.js:155:22)
    at rn (runtime-core.esm-bundler.js:164:21)
    at Lr.t.__weh.t.__weh (runtime-core.esm-bundler.js:2682:29)
    at xn (runtime-core.esm-bundler.js:339:32)

My API returns:

[
    {
        "value": 2,
        "display": "2023-09-01 00:00:00 -> 2023-11-30 00:00:00"
    },
    {
        "value": 4,
        "display": "2023-02-01 00:00:00 -> 2023-06-30 00:00:00"
    }
]

multiple item selected

Thank you for your awesome package.

Can we add a multiple config to selected multiple options?

3 or more dependend selects > not resetting correctly

I've got multiple selects depending on eachother. Simplified example:

client
location
department
user

User depends on department, department on location and location on client.

Bug

  1. Select a (dependend) value in each select
  2. Change the selected option in client (first select)
  3. Location is properly resetted and department disabled.
  4. User however still has the old values

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.