duanliangman5398 2018-05-19 22:51
浏览 39
已采纳

如何使用多个过滤器正确拥有自动完成搜索输入?

I have a context where there is a conferences table that stores the conferences. The conference table has a column "name" and also a column city".

I have an input type text and the user should be able to search for conference names but also for conferences that will happen in a specific city.

To explain better: The user enters "Ne" in the input text. So it should appear the results relative to conference names but also city names. If the user clicks in the name of a conference he should be redirected to the conference details page. If he clicks in a city should appear the conferences that have the clicked city as a value in the column "city" of the conferences table.

enter image description here

Do you know how this can be achieved? For now, is just working the part of appearing the conferences in the input search.

I created an AutocompleteController for this context:

class AutocompleteController extends Controller
{

    public function search(Request $request){
        $search = $request->term;
        $conferences = Conference::where('name', 'LIKE', '%'.$search.'%')->get();

        $data= [];
        foreach ($conferences as $key => $value){
            $data[] = ['id'=> $value->id, 'value' => $value->name . " " . $value->name];
        }
        return response($data);
    }
}

Route to above method:

Route::get('/autocomplete-search', 'AutocompleteController@search');

jQuery:

$("#search").autocomplete({
   source: "{{ URL::to('autocomplete-search') }}",
   minLength: 2,
   select:function(key, value)
   {
       console.log(value);
   }
});

Search input:

<div class="col col-md-6">
    <h4 class="text-white  text-center font-weight-bold">Search</h4>
    <form class="main-search">
        <input type="text" id="search" class="autocomplete dropdown-toggle" placeholder="Conference, Citiy, Category">
    </form>
</div>

Conferences table:

   conferences table

    id | name | city | ...

     1 |  New conference 1  | Newcastle
     2 |  New conference 2  | Newcastle
     3 |  old conference 1  | Bristol
     4 |  old conference 2  | Glasgow
  • 写回答

1条回答 默认 最新

  • dongqiang8683 2018-05-20 00:55
    关注

    You will have to search for cities and add category property to the data you are sending to your autocomplete.

    I am not entirely familiar with database access so apology if bellow is not 100% correct.

    class AutocompleteController extends Controller
    {
    
        public function search(Request $request){
            $search = $request->term;
            $conferences = Conference::where('name', 'LIKE', '%'.$search.'%')->get();
    
            $cities = Conference::where('city', 'LIKE', '%'.$search.'%')->get();
    
            $data= [];
            foreach ($conferences as $key => $value){
                $data[] = ['category'=> 'Conference', 'value' => $value->name, 'url' = 'conferenceUrl'];
            }
    
            foreach ($cities as $key => $value){
                $data[] = ['category'=> 'City', 'value' => $value->city, 'url' = 'cityUrl'];
            }
            return response($data);
        }
    }
    

    Then you will have to slightly alter autocomplete. For that you can use category autocomplete example.

    $.widget( "custom.catcomplete", $.ui.autocomplete, {
          _create: function() {
              this._super();
              this.widget().menu( "option", "items", "> :not(.ui-autocomplete-category)" );
          },
          _renderMenu: function( ul, items ) {
            var that = this,
              currentCategory = "";
            $.each( items, function( index, item ) {
              var li;
              if ( item.category != currentCategory ) {
                ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
                currentCategory = item.category;
              }
              li = that._renderItemData( ul, item );
              if ( item.category ) {
                li.attr( "aria-label", item.category + " : " + item.label );
              }
            });
          }
        });
    
    
    $("#autocomplete").catcomplete({
      source: [{
          value: "New conference abcd",
          label: "New conference abcd",
          url: "NewConferenceUrl",
          category: "Conference"
        },
        {
          value: "Newcastle",
          label: "Newcastle",
          url: "NewcastleUrl",
          category: "City"
        }
      ],
      select: function(event, ui) {
        console.log("Redirecting to " + ui.item.category + ": " + ui.item.url);
      }
    });
    .ui-autocomplete-category {
        font-weight: bold;
        padding: .2em .4em;
        margin: .8em 0 .2em;
        line-height: 1.5;
      }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
    <link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css">
    
    <div>
      <input id="autocomplete">
    </div>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan