javascript - Get different html with different buttons with AJAX -
i got menu on website
%button.dropdown-button .current-user{onclick:'showmenu()'} %img.current-user-image{src:current_user.picture_url} = current_user %i.fa.fa-bars{'aria-hidden':true} .dropdown-content .menu-option{onclick:'showfilters()'} filter transactions %i.fa.fa-paper-plane{'aria-hidden':true} .transaction-filters .filter-option transactions %i.fa.fa-square-o %a{href:'#'} .menu-option balance history %i.fa.fa-history{'aria-hidden':true} %a{href:destroy_user_session_path} .menu-option sign out %i.fa.fa-sign-out{'aria-hidden':true} and got timeline transactions
.timeline-container - @transactions.each |transaction| .transaction-container .transaction-header-container .transaction-kudos-container = "+#{transaction.amount}" %span.currency ₭ .transaction-avatar-container %div = image_tag transaction.receiver_image, class:'avatar' .transaction-body-container .transaction-content %span = "#{transaction.sender.name}:" %span.highlighted = "+#{transaction.amount} ₭" %span %span.highlighted = transaction.receiver_name_feed %span %span.highlighted = transaction.activity_name_feed %div -#%button#like-button -# %span.post-time-stamp = "#{distance_of_time_in_words(datetime.now, transaction.created_at)} ago" = paginate @transactions they both rendered on index.html.haml
so when click div .filter-option.sent want change code change
- @transactions.each |transaction| to
- @all_transactions.each |transaction| to filter out transactions of current user without reloading page.
these variables defined in controller
@transactions = transaction.order('created_at desc').page(params[:page]).per(20) @all_transactions = transaction.all_for_user(current_user) with in model method all_for_user
def self.all_for_user(user) transaction.where(sender: user).or(transaction.where(receiver: user)).order('created_at desc').page.per(20) end i tried lot of things ajax nothing seems please me. can me?
so if you'd replace @transactions list ajax need few things..
1) move @transactions block partial takes local variable.
#transactions-wrapper = render partial: 'transactions/list', locals: {transactions: @transactions} 2) create link submits route, hits controller action #js ('data-remote': true ) ( or write javascript function triggers $.ajax request: https://www.w3schools.com/jquery/ajax_ajax.asp
%a.transaction-filter{href: "/transactions/#{transaction_type}", 'data-remote': true} or ex..
%a.study{href: "/transactions/recent", 'data-remote': true} %a.study{href: "/transactions/past", 'data-remote': true} 3) define route
get '/transactions/:type' => 'transactions#filter' 4) re-assign variable based on filter, , re-render partial new data in filter.js.erb file thats in view directory -> app/views/transactions
def filter @filtered_transactions = transactions.where(type: params[:type] ).order('created_at desc') respond_to |format| format.html format.js { render :action => 'filter.js.erb', :layout => false} end end $("#transactions-wrapper").html("<%= j render(:partial => 'transactions/list'', :locals => { transactions: @filtered_transactions } ) %>"); alert('transactions have been filtered, yo') let me know if makes sense! except please don't javascript alert @ end ;)
Comments
Post a Comment