How can i show data using a modal when clicking a table row (using bootstrap)

前端 未结 3 1410
别跟我提以往
别跟我提以往 2020-12-12 18:58

I\'m sort of a beginner on the whole web development thing and after researching a lot i still couldn\'t get this done. Any help is very much appreciated. I\'m using latest

3条回答
  •  無奈伤痛
    2020-12-12 19:19

    The best practice is to ajax load the order information when click tr tag, and render the information html in $('#orderDetails') like this:

      $.get('the_get_order_info_url', { order_id: the_id_var }, function(data){
        $('#orderDetails').html(data);
      }, 'script')
    

    Alternatively, you can add class for each td that contains the order info, and use jQuery method $('.class').html(html_string) to insert specific order info into your #orderDetails BEFORE you show the modal, like:

      <% @restaurant.orders.each do |order| %>
      
      
        <%= order.id %>
        <%= order.customer_id %>
        <%= order.status %>
      
      <% end %>
    

    js:

    function orderModal(order_id){
      var tr = $('#order_' + order_id);
      // get the current info in html table 
      var customer_id = tr.find('.customer_id');
      var status = tr.find('.status');
    
      // U should work on lines here:
      var info_to_insert = "order: " + order_id + ", customer: " + customer_id + " and status : " + status + ".";
      $('#orderDetails').html(info_to_insert);
    
      $('#orderModal').modal({
        keyboard: true,
        backdrop: "static"
      });
    };
    

    That's it. But I strongly recommend you to learn sth about ajax on Rails. It's pretty cool and efficient.

提交回复
热议问题