Search a user in a list of usernames in AngularJS

不羁岁月 提交于 2020-01-06 08:49:10

问题


I am looking for something that one would probably find in hundreds of tutorials for AngularJS but I don't quite know where to look.

I want the best practice for searching a user by the user's name out of a list of ten thousands stored in the database. I am thinking about something similar to Facebook's "friend search" field. So as the user starts typing proposed results should appear. If the list was already on the client, the simple ng-filter behaviour would be enough, but I don't want to dump the whole database in a json file.

Could somebody forward me some hints how to approach this problem and where the pitfalls are? The backend is a Symfony2 application with Doctrine, if that matters...

Thank you!


回答1:


For requesting ten thousand of data in a combo like component, you should use a typehead with an asynchronous call.

See this component :

https://github.com/angular-ui/bootstrap/tree/master/src/typeahead

Your indexed data have to be indexed (lucene or equivalent) to have acceptable performances.




回答2:


I believe you are looking for Select2 component, more exactly the select2 with remote data here.

It is integrated in angular-ui

And in your controller, you can set ajax parameter as described in the select2 API description:

$scope.select2Options = { 
  ajax: {
      url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json",
      dataType: 'jsonp',
      quietMillis: 100,
      data: function (term, page) { // page is the one-based page number tracked by Select2
      return {
          q: term, //search term
          page_limit: 10, // page size
          page: page, // page number
      };
  }

The back-end implementation will be your job.



来源:https://stackoverflow.com/questions/25742393/search-a-user-in-a-list-of-usernames-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!