Search a user in a list of usernames in AngularJS

2019-09-10 03:05发布

问题:

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.