首页 > 解决方案 > 带下拉菜单的自动字段数据

问题描述

在 Laravel 中,我有“提款人名称”字段,它通过自动完成显示给我,我希望“提款人 id”的值将取决于“提款人名称”字段

例如:对于所有“mac”,通过下拉菜单向我显示他所有的提款机 ID

在我的刀片中:

   $(document).ready(function(){

   $('#client_name').keyup(function(){ 
       var query = $(this).val();
       if(query != '')
       {
        var _token = $('input[name="_token"]').val();
        $.ajax({
         url:"{{ route('autocomplete.fetchwithdrawer') }}",
         method:"POST",
         data:{query:query, _token:_token},
         success:function(data){
          $('#client_namelist').fadeIn();  
          $('#client_namelist').html(data);
         }
        });
       }
   });


   $('#client_namelist').on('click', 'li', function(){  
        $('#client_name').val($(this).text());

        $('#client_namelist').fadeOut();  
    });  

在我的控制器中:

 function fetchwithdrawer(Request $request)
    {
        if (Gate::denies('manager')){  
            if (Gate::denies('worker')) {
                abort(403,"Are you a hacker or what?");} }
     if($request->get('query'))
     {
      $query = $request->get('query');
      echo($query);
      $data = DB::table('customers')
        ->where('client_name', 'LIKE', "%{$query}%")
        ->get();
        $output1 = '';

      $output1 = '<ul class="dropdown-menu" style="display:block; position:relative">';
      foreach($data as $row)
      {
       $output1 .= '
       <li style="cursor:pointer;color:blue">'.$row->client_name.'</li>
       ';
      }
      $output1 .= '</ul>';
      if(count($data))
      echo $output1;
        else
            return ' Chosen But No Result Found, add new customer';
     }

TNX

标签: laravelautofield

解决方案


推荐阅读