lhoppe lhoppe - 1 year ago 106
Javascript Question

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

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 rails version and bootstrap to make things prettier.

I have a page with a table which contains orders from a restaurant. If you click a row i want it to show the order details in a modal window using bootstrap. I have managed to open the modal via onclick+javascript function but it looks kinda messy and i still have no clue how to load the content div of the modal with the order info. Here's the code i have for this:


<table class="table table-striped"
<% @restaurant.orders.each do |order| %>
<tr onclick="orderModal(<%= order.id %>);">
<td><%= order.id %></td>
<td><%= order.customer_id %></td>
<td><%= order.status %></td>
<% end %>

<div id="orderModal" class="modal hide fade" role="dialog"
aria-labelledby="orderModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
<div id="orderDetails"class="modal-body"></div>

<div id="orderItems" class="modal-body"></div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>


function orderModal(order_id){
keyboard: true,
backdrop: "static"

Answer Source

1 think you can do is to get rid of all those onclick attributes and do it the right way with bootstrap. You dont need to open them manually you can specify the trigger and even subscribe to events before the modal opens so that you can do your operations and populate data in it.

I am just going to show as a static example which you can accomodate in your real world.

on each of your tr's add a data attribute for id. i.e data-id with the corresponding id value and specify a data-target which is a selector you specify so that when clicked on it bootstrap will select that element as modal dialog and show it. And then you need to add another attribute data-toggle=modal to make this a trigger for modal.

  <tr data-toggle="modal" data-id="1" data-target="#orderModal">
  <tr data-toggle="modal" data-id="2" data-target="#orderModal">
  <tr data-toggle="modal" data-id="3" data-target="#orderModal">

and now in the javascript just set up the modal just once and event listen to its events so you can do your work.

        keyboard: true,
        backdrop: "static",

    }).on('show', function(){ //subscribe to show method
          var getIdFromRow = $(event.target).closest('tr').data('id'); //get the id from tr
        //make your ajax call populate items or what even you need
        $(this).find('#orderDetails').html($('<b> Order Id selected: ' + getIdFromRow  + '</b>'))


Do not use inline click attributes any more use event bindings instead with vanilla js way or using jquery

An alternative way as well here.

Demo2 or Demo3

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download