LoginSignup
4
6

More than 5 years have passed since last update.

インクリメンタルサーチ

Posted at

インクリメンタルサーチを行う際は情報を配列から人ずつ取り出して表示させていくこととなる。
jbuilder

json.array! @users do |user|
  json.id user.id
  json.name user.name
end

controllerだと

 def index
   @users = User.where('name LIKE(?)', "%#{params[:keyword]}%").where.not(id: 
   current_user.id).limit(20)
   respond_to do |format|
     format.html
     format.json
   end
 end

jsファイルだと

  .done(function(users){
  $('#user-search-result').empty();
  if (users.length !== 0) {
  users.forEach(function(user){
  appendbuildHTML(user);
    });

forEachを使って、配列のJSONから情報をとりさしているのがみそ。

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6