распечатка данных из ajax

0

Итак, это первый раз, когда я использовал ajax с рельсами, Ive удалось вернуть "данные" с контроллера... как мне теперь распечатать это на странице? Я нахожу информацию о ajax онлайн, но 9 не могу найти какой-либо код так же, как мой, чтобы ссылаться?

Код изменяет порядок отображения деталей с ASC на DESC.

JS FILE

  $(".ord-dir-link").on("click", function(e){
    e.preventDefault();
      $.ajax({
        url: '/index_list',
        method: 'get',
        data: jQuery.param({
        option: this.href.split("=")[2],
        direction: $('.list-direction').val()
        }),
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        success: function(data){
          console.log(data)
        }
      })
  });

КОНТРОЛЛЕР

    def index_list
      if params[:option]
        @products = @products.order("#{params[:option]} #{params[:direction]}")
        respond_to do |format|
        format.json { render json: @products.to_json }
        end
      end
    end

И ВИД

    <div class="right-content">
      <table class="index-list">
        <tr>
          <th>Id</th>
          <th><%= link_to "Name", index_list_path(option: "product_name"), class: "ord-dir-link"%></th>
          <th><%= link_to "Type", index_list_path(option: "type_of_product"), class: "ord-dir-link" %></th>
          <th><%= link_to "Section", index_list_path(option: "section"), class: "ord-dir-link" %></th>
          <th><%= link_to "Category", index_list_path(option: "category"), class: "ord-dir-link" %></th>
          <th><%= link_to "Price", index_list_path(option: "price"), class: "ord-dir-link" %></th>
          <th><%= link_to "Discount", index_list_path(option: "discount"), class: "ord-dir-link" %></th>
          <th><%= link_to "Views", index_list_path(option: "click_counter"), class: "ord-dir-link" %></th>
          <th><%= link_to "Upvotes", index_list_path(option: "upvotes"), class: "ord-dir-link" %></th>
          <th><%= select_tag(:direction, options_for_select([ ['ASC', "ASC"], ['DESC', "DESC"]] ), {:class => 'list-direction'})  %></th>
        </tr>
        <% n = 1 %>
        <% @products.each do |x| %>
        <tr>
          <td><%=n%>.</td>
          <td><%=x.product_name%></td>
          <td><%=x.type_of_product%></td>
          <td><%=x.section%></td>
          <td><%=x.category%></td>
          <td>£<%=x.price%></td>
          <td><%=x.discount if x.discount != nil%></td>
          <td><%=x.click_counter%></td>
          <td><%=x.upvotes%></td>
          <td><%=link_to "View", product_path(x.id)%></td>
          <% n += 1 %>
          <%end%>
        </tr>
      </table>
    </div>

Мне нужно добавить все, что записано в javascript? это кажется долгим и неуклюжим способом сделать это.

  • 0
    Если вы ответите HTML, то да. Вы должны установить его на странице, где бы он ни находился, с помощью html() или append() .
  • 0
    Вы разобрались с этим?
Показать ещё 1 комментарий

1 ответ

0

Просто чтобы расширить то, что сказал Taplar, вы бы сделали что-то вроде:

$(".ord-dir-link").on("click", function(e){
  e.preventDefault();
    $.ajax({
      url: '/index_list',
      method: 'get',
      data: jQuery.param({
      option: this.href.split("=")[2],
      direction: $('.list-direction').val()
      }),
      contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
      success: function(data){
        $('#some-selector').html(data)
      }
    })
});
  • 0
    Привет @jvillian, я добавил id = 'test' в div class = "right-content", а затем выполняю $ ('# test'). Html (data), к сожалению, не имеет ничего общего с удалением div?
  • 0
    "к сожалению, делает отдельно" ... что? @johnseymour
Показать ещё 4 комментария

Ещё вопросы

Сообщество Overcoder
Наверх
Меню