Multiple tables on page not working (Rails)

Multiple tables on page not working (Rails)

nhorob67nhorob67 Posts: 1Questions: 1Answers: 0
edited June 2015 in Free community support

The second table isn't showing up w/ DataTables formatting. Here's my code.

<% @contract_groups.each do |contract_group, contracts| %>
        <% unless contracts.empty? %>
          <% contract_type = ContractType.find(contract_group) %>
          <h3><%= contract_type.description %></h3>
          <table class = "contracts">
            <thead>
              <tr>
                <% if contract_type.description == "Forward Contract" %>
                  <th>Open order</th>
                  <th>Bushels</th>
                  <th>Delivery location</th>
                  <th>Futures price</th>
                  <th>Basis</th>
                  <th>Cash Price</th>
                  <th>Contract number</th>
                  <th>Delivery period</th>
                  <th>Year</th>
                  <th>User</th>
                  <th colspan="3"></th>
                <% end %>
                <% if contract_type.description == "Hedge-To-Arrive" %>
                  <th>Open order</th>
                  <th>Bushels</th>
                  <th>Delivery location</th>
                  <th>Futures month</th>
                  <th>Futures price</th>
                  <th>Basis</th>
                  <th>Est. Cash Price</th>
                  <th>Contract number</th>
                  <th>Long short</th>
                  <th>Contracts</th>
                  <th>Strike price</th>
                  <th>Gain loss</th>
                  <th>Delivery period</th>
                  <th>Year</th>
                  <th>User</th>
                  <th colspan="3"></th>
                <% end %>
              </tr>
            </thead>
            <tbody>
              <% contracts.each do |contract| %>
                <tr>
                  <% if contract_type.description == "Forward Contract" %>
                    <td><%= contract.open_order %></td>
                    <td><%= contract.bushels %></td>
                    <td><%= contract.delivery_location %></td>
                    <td><%= number_to_currency(contract.futures_price) %></td>
                    <td><%= number_to_currency(contract.basis) %></td>
                    <td><%= number_to_currency(contract.basis + contract.futures_price) %></td>
                    <td><%= contract.contract_number %></td>
                    <td><%= contract.delivery_period %></td>
                    <td><%= contract.year %></td>
                    <td><%= contract.user_id %></td>
                    <td><%= link_to 'Show', contract %></td>
                    <td><%= link_to 'Edit', edit_contract_path(contract) %></td>
                    <td><%= link_to 'Destroy', contract, method: :delete, data: { confirm: 'Are you sure?' } %></td>
                  <% end %>
                  <% if contract_type.description == "Hedge-To-Arrive" %>
                    <td><%= contract.open_order %></td>
                    <td><%= contract.bushels %></td>
                    <td><%= contract.delivery_location %></td>
                    <td><%= contract.futures_month %></td>
                    <td><%= contract.futures_price %></td>
                    <td><%= contract.basis %></td>
                    <td><%= number_to_currency(contract.basis + contract.futures_price) %></td>
                    <td><%= contract.contract_number %></td>
                    <td><%= contract.long_short %></td>
                    <td><%= contract.contracts %></td>
                    <td><%= contract.strike_price %></td>
                    <td><%= contract.gain_loss %></td>
                    <td><%= contract.delivery_period %></td>
                    <td><%= contract.year %></td>
                    <td><%= contract.user_id %></td>
                    <td><%= link_to 'Show', contract %></td>
                    <td><%= link_to 'Edit', edit_contract_path(contract) %></td>
                    <td><%= link_to 'Destroy', contract, method: :delete, data: { confirm: 'Are you sure?' } %></td>
                  <% end %>
                </tr>
              <% end %>
            </tbody>
          </table>
        <% end %>
      <% end %>

      <br>

      <%= link_to 'New Contract', new_contract_path %>
  </div>
</div>

<% content_for :scripts do %>
  <script type="text/javascript">
    $(document).ready(function() {
      $('table.contracts').DataTable( {
        responsive: true
      });
    } );
  </script>
<% end %>

Answers

  • rob morningrob morning Posts: 30Questions: 7Answers: 1

    Can't you only create a table using it's id - not by class ?

  • matthttammatthttam Posts: 40Questions: 2Answers: 0

    Your HTML isn't formatted where datatables can initialize two tables. I do not see two

    <

    table> tags in this list. Try breaking this up so there is

    then another

    Then after document.ready do :

    $('#firsttable').dataTable({ responsive: true });
    $('#secondtable').dataTable({ responsive: true });
    

    I hope this helps.

This discussion has been closed.