动态相关菜单-值不变

时间:2019-01-29 09:18:15

标签: jquery ajax laravel

我正在尝试使用ajax设置动态相关菜单。选择选项是通过数据库填充的。有与此相关的三个表 1.客户 2.公司 3. company_customer

客户与公司之间有着多对多的关系

选择客户后,相关公司会出现

我正在使用Laravel 5.5

路线

Route::get('', 'CustomerController@list');
Route::post('fetch','CustomerController@fetch')->name('dynamicdependent.fetch');

CustomerController

<?php

namespace App\Http\Controllers;
use App\Customer;
use App\Company;

use Illuminate\Http\Request;

class CustomerController extends Controller
{

    public function list()
    {
        $customers = Customer::all();
        $companies = Company::all();

        return view('list', compact('customers', 'companies'));
    }


    public function fetch(Request $request)
    {

        $select = $request->get('select');
        $value = $request->get('value');
        $dependent = $request->get('dependent');
        $data = DB::table('company_customer')
        ->where($select, $value)
        ->groupBy($dependent)
        ->get();
        $output = '<option value="">Select '.ucfirst($dependent).'</option>';
        foreach($data as $row)
        {
        $output .= '<option value="'.$row->$dependent.'">'.$row->$dependent.'</option>';
        }
        echo $output;
        return view('fetch');
    }

}

list.blade.php

<!doctype html>
<html lang="en">
  <head>
    <title>Title</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    {{-- Jquery Data Table css--}}
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">

    {{-- Jquery --}}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    {{-- Jquery Data Table js--}}
    <script charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>

  </head>
  <body>
    <div class="container">
        <div class="row">
            <div class="col-12">
                <div class="form-group">
                  <label for="customer">Customer</label>
                  <select class="form-control" name="customer" id="customer" data-dependent="company">
                    @foreach ($customers as $customer)
                        <option value="{{$customer->id}}">{{ $customer->customer }}</option>
                    @endforeach
                  </select>
                </div> 
                <div class="form-group">
                  <label for="company">Company</label>
                  <select class="form-control" name="company" id="company">
                    @foreach ($companies as $company)
                        <option value="{{$company->id}}">{{ $company->company }}</option>
                    @endforeach
                  </select>
                </div>
            </div>
            {{ csrf_field() }}
        </div>
    </div>
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->

  </body>


  <script>
        $(document).ready(function(){



         $('.dynamic').change(function(){
          if($(this).val() != '')
          {
           var select = $(this).attr("id");
           var value = $(this).val();
           var dependent = $(this).data('dependent');
           var _token = $('input[name="_token"]').val();
           $.ajax({
            url:"{{ route('dynamicdependent.fetch') }}",
            method:"POST",
            data:{select:select, value:value, _token:_token, dependent:dependent},
            success:function(result)
            {
             $('#'+dependent).html(result);
            }

           })
          }
         });

         $('#customer').change(function(){
          $('#company').val('');          
         });


        });
        </script>
</html>

1 个答案:

答案 0 :(得分:0)

在您的路线中,获取记录的路线为

Route::get('fetch','CustomerController@fetch')->name('dynamicdependent.fetch');

与在list.blade.php脚本中提取记录的位置一样,您正在使用以下方式调用该路由

method:"POST"

确保在路由文件中将路由更改为post或在脚本中将其更改为get。让我们知道这是否解决了。