platform icon indicating copy to clipboard operation
platform copied to clipboard

Listenened field clears.

Open jrushworth opened this issue 2 years ago • 4 comments

Trying to implement a Dependent Dropdown. Seems to work fine except for a couple of issues

  1. Need to initialise dependent dropdown first time into screen based on current district_id. I can probably work that out

  2. BUT, immediately after selecting a district, the district dropdown clears!!! The Location dropdown is populated correctly with the locations for the selected district, but the District dropdown is clear! Been hitting my head against this for a while now. What is happening

Please help.

<--relevant code below-->

DistrictListener.php

class DistrictListener extends Listener
{
    /**
     * List of field names for which values will be listened.
     *
     * @var string[]
     */
    protected $targets = ['event.district_id'];

    /**
     * What screen method should be called
     * as a source for an asynchronous request.
     *
     * The name of the method must
     * begin with the prefix "async"
     *
     * @var string
     */
    protected $asyncMethod = 'asyncFromDistrict';

    /**
     * @return Layout[]
     */
    protected function layouts(): iterable
    {
        // dump($this->query->get('location'));
        return [
            Layout::rows([
                Select::make('event.district_id')
                ->title('District')
                ->fromModel(District::class, 'district_name')
                ->empty('--everywhere--'),

                Select::make('event_id.location_id')
                ->title('Location')
                ->options($this->query->get('location')?$this->query->get('location'):[]),
   }

EventEditScreen.php

   public function asyncFromDistrict($district_id) 
    {
        return [
            'location' =>   Location::where('district_id',$district_id)->pluck('description','id')->toArray()
        ];
    }

jrushworth avatar Jul 18 '22 09:07 jrushworth

Can your provide the version of Orchid you are using please

astersnake avatar Aug 17 '22 00:08 astersnake

I will close this for now as inactive.

astersnake avatar Aug 17 '22 16:08 astersnake

Definitely not inactive. Sorry, been away for a few days. Using latest version of Orchid, latest laravel. Don't have development machine with me so can't quote version numbers sorry but first thing I did when I first had this problem was to make sure I was on latest versions.

jrushworth avatar Aug 17 '22 21:08 jrushworth

you have to set the values in listener layout not to lose them. So, you have to listen for the values for all you put in your listener class.

for ex : if you put fields a, b, c in your Listener Layout, then you should listen fields a,b,c and set their own value inside listener class.

macit-emre avatar Sep 04 '22 20:09 macit-emre