Home > Net >  Laravel Livewire Autofill Input Box After Drop Down Select
Laravel Livewire Autofill Input Box After Drop Down Select

Time:10-25

Good Day Everyone. So I have a table for product contains('prod_name', 'prod_code') for my drop down select in my edit modal. When doing CRUD(Another Table), is I want the user to select 'prod_name' and it would also populate the input box for 'prod_code' to autofill. Thanks to whoever responds.

<option value="prod_name" wire:model="prod_name"></option>
            @foreach($products as $product)
            <option value="{{ $products->prod_name }}">{{ $products->prod_name}}</option>
              @endforeach
          </select>

CodePudding user response:

Am Going to give you a hint on how i completed my assignment as it is just selecting the Countryand once Country selected it populate the next field with the State corresponding to the Country selected so you can translate it in your logic. this is just for Selecting Country and State

<div class="col-md-4">              
   {!! Form::select('country', ['' => 'Select Country']  $sector,'',array('class'=>'form-control select2','name'=>'country','id'=>'country','style'=>'width:100%;'));!!}
  </div>
  <div class="col-md-4">   
      <select name="state" id="state" class="form-control select2" >
      </select>
  </div>

I used Jquery to deal with the rest Just in your script

<script type="text/javascript">
    $('#country').change(function(){
    var countryID = $(this).val();    
    if(countryID){
        $.ajax({
           type:"GET",
           url:"{{url('api/get-state-list')}}?country_id=" countryID,
           success:function(res){               
            if(res){
                $("#state").empty();
                $("#state").append('<option>Select state</option>');
                $.each(res,function(key,value){
                    $("#state").append('<option value="' key '">' value '</option>');
                });
           
            }else{
               $("#state").empty();
            }
           }
        });
    }else{
        $("#state").empty();
    }      
   });
  </script>

the Rest is just creating your route. as you see i triggered the Url api/get-state-list with country_id

so in your route

Route::get('api/get-state-list','\App\Http\Controllers\YOURCONTROLLER@getStateList');

I think there you are ready to go

  • Related