0

I am wondering if i can use rails tags in html using jquery to reload a table based on a new query. See below:

    <table id = "PlyrsTm2" style = float:right>
        <tr><th id="PTTitle" colspan=2>List of players on selected team</th></tr>
        <tr><th id="PTHRows">Player</th><th id="PTHRows">Team</th></tr>
        <% @pl.each do |f| %>
            <tr><td class="player"><%= f.Plyr %></td><td class="team"><%= f.Team %></td></tr>
        <% end %>
    </table>

So i know that i can do this:

$('#PlyrsTm2').html('<table id = "PlyrsTm2" style = float:right><tr><th id="PTTitle" colspan=2>List of players on selected team</th></tr><tr><th id="PTHRows">Player</th><th id="PTHRows">Team</th></tr><tr><td class="player"></td><td class="team"></td></tr></table>

but if I add the rails part to it, then it does not refresh/reload the table. I have looked all over the internet and have not found the solution. I posted a question similar to this, but no one answered and it was voted down because of not enough research(which is not true) because i only post questions to answer that i do not find so can someone help me with this, i would greatly appreciated

RE-UPDATED

it might be helpful to post the ajax and jquery that i am using:

$(document).ready(function(){
    $('#showbtn').on('click', function() {
        ids = $('#teams').val()
        IM = false
    $.ajax({
        url: "http://localhost:3000/teamplayers.json?resolution="+ids+"&import="+IM,
        type:"get",
        dataType: "json",
        cache: true,
        success:function(data){
            $('#PlyrsTm2').html(data);
                $('#PlyrsTm2').show();
            alert(data);
            alert("Loading Players...."); 
            },
        error: function(error) {
                   alert("Failed " + console.log(error) + " " + error)
                   }           
                   });
                $('#PlyrsTm2').trigger('create');


            });


});

I click a button (#showbtn) which it passes a integer value (#teams) to my controller, which is then changed and here is the table i want it to load:

'<table id = "PlyrsTm2" style = float:right><tr><th id="PTTitle" colspan=2>List of players on selected team</th></tr><tr><th id="PTHRows">Player</th><th id="PTHRows">Team</th></tr><tr><% @pl.each do |f| %><td class="player"><%= f.Plyr %></td><td class="team"><%= f.Team %></td></tr><% end %></table>

As you can see there is Rails html in it, but i do not think it actually re-excutes the rails part <& @pl.each do |f| %>

which then i see the same table that loaded when the page was first opened. So how do i get the rails to re-execute with the new info?

I can post the controller method if needed.

UPDATE Here is the controller

class TeamplayersController < ApplicationController
    before_filter :set_id
    before_action :set_id, :set_teamplayer, only: [:show, :edit, :update, :destroy]

# GET /teamplayers
# GET /teamplayers.json
  def index
   @teamplayers = Teamplayer.all
   @fteams = Fteam.all

   tid = params[:resolution]
   toimport = params[:import]

   if tid.nil?
    tid = 1
   else
    tid = tid.to_i;
   end
   @ids = tid
   @pl =  Teamplayer.joins(:live_player).where(:teamid => @ids).all << Here should refresh the table

   if toimport == "true"
    @turl = Fteam.where(:id => @ids).pluck(:TeamUrl)

    @turl = @turl[0]  

    system "rake updateTm:updateA[#{@turl},#{@ids}]"
end
end

2 Answers 2

1

i looked at your fiddle.

change this

        $('#SelectedTm').replaceWith(test2);
        },
    error: function(error) {
               alert("Failed " + console.log(error) + " " + error)
               }           
               });
$('#SelectedTm').trigger('create');

to this

    $('#SelectedTm').html(test2);
        $('#SelectedTm').trigger('create');
        },
    error: function(error) {
               alert("Failed " + console.log(error) + " " + error)
               }           
               });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this along with some maneuvering did exactly what i wanted
0

after this you have to de a trigger create like

$('#PlyrsTm2').trigger('create');

modify this

success:function(success){
            $('#PlyrsTm2').html(change1);
            alert("test 3"); 
            }

to this

success:function(data){
            $('#PlyrsTm2').html(data);
            $('#PlyrsTm2').trigger('create');
            }

9 Comments

it is reloading the same thing, let me update with what i have in my ajax/jquery
I posted where i put the trigger part, but it did not seem to do the trick
should i run the trigger inside or outside of the ajax?
Why is the trigger create not able to make use of the rails tags?
success:function(data){ $('#PlyrsTm2').html(data); alert("test 3"); }, error: function(error) { alert("Failed " + console.log(error) + " " + error) } }); $('#PlyrsTm2').trigger('create');
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.