Challenges arise with dynamic tables (Foundation 5) within Rails application

I have tables set up in my Rails 4 App with the ZURB Foundation 5 framework.

The problem lies with the mobile version of the table. It works fine on browsers and tablets, but on mobile phones, it shows the first column twice before scrolling through the rest of the columns. I can't figure out how to remove this duplicated first column. Any ideas?

Table Code:

<table class="responsive">
<thead>
<tr>
<th width="150">TEST TEST 1</th>
<th width="150">TEST TEST 2</th>
<th width="150">TEST TEST 3</th>
<th width="150">TEST TEST 4</th>
<th width="150">TEST TEST 5</th>
<th width="150">TEST TEST 6</th>

</tr>
</thead>
<tbody>
<tr>
  <td>ANSWER 1</td>
  <td>ANSWER 2</td>
  <td>ANSWER 3</td>
  <td>ANSWER 4</td>
  <td>ANSWER 5</td>
  <td>ANSWER 6</td>

</tr>
</tbody>

My App Layout.html.erb has the following in the header:

  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <title><%= content_for?(:title) ? yield(:title) : "PatrolPro R.M.S - Demo" %></title>

    <%= stylesheet_link_tag    "application" %>
    <%= javascript_include_tag "vendor/modernizr" %>
    <%= javascript_include_tag "application", 'data-turbolinks-track' => true %>
    <%= csrf_meta_tags %>
    <%= stylesheet_link_tag "responsive-tables" %> -- Added according to Foundation
    <%= javascript_include_tag "responsive-tables" %> -- Added according to Foundation
  </head>

Answer №1

Dealing with a similar issue, I managed to find a workaround that worked for me. The root of the problem was Turbolinks triggering the JS code multiple times. To address this, I made adjustments to the responsive_tables.js file as shown below. A key element to note is the use of the global variable switched, which you can customize to avoid conflicts with other scripts on your website:

var switched = false;

$(document).ready(function() {

  var updateTables = function() {
    if (($(window).width() < 767) && !switched ){
      switched = true;
      $("table.responsive").each(function(i, element) {
        splitTable($(element));
      });
      return true;
    }
    else if (switched && ($(window).width() > 767)) {
      switched = false;
      $("table.responsive").each(function(i, element) {
        unsplitTable($(element));
      });
    }
  };

  $(window).load(updateTables);

});


$(window).bind('page:change', function() {

  switched = false;

  var updateTables = function() {
    if (($(window).width() < 767) && !switched ){
      switched = true;
      $("table.responsive").each(function(i, element) {
        splitTable($(element));
        console.log('here');
      });
      return true;
    }
    else if (switched && ($(window).width() > 767)) {
      switched = false;
      $("table.responsive").each(function(i, element) {
        unsplitTable($(element));
      });
    }
  };

  if (!switched) {
    updateTables();
  }

});


function splitTable(original)
{
  original.wrap("<div class='table-wrapper' />");

  var copy = original.clone();
  copy.find("td:not(:first-child), th:not(:first-child)").css("display", "none");
  copy.removeClass("responsive");

  original.closest(".table-wrapper").append(copy);
  copy.wrap("<div class='pinned' />");
  original.wrap("<div class='scrollable' />");

  setCellHeights(original, copy);
}

function unsplitTable(original) {
  original.closest(".table-wrapper").find(".pinned").remove();
  original.unwrap();
  original.unwrap();
}

function setCellHeights(original, copy) {
  var tr = original.find('tr'),
      tr_copy = copy.find('tr'),
      heights = [];

  tr.each(function (index) {
    var self = $(this),
        tx = self.find('th, td');

    tx.each(function () {
      var height = $(this).outerHeight(true);
      heights[index] = heights[index] || 0;
      if (height > heights[index]) heights[index] = height;
    });

  });

  tr_copy.each(function (index) {
    $(this).height(heights[index]);
  });
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When utilizing the HTML select element, the <option> tag may only display the initial letter of the chosen option

Here is a snippet of my PHP code: <?php if($_POST['post'] == 'Post') { $cat = $_POST['cat']; $update = "UPDATE table SET category='$cat' WHERE id = '$id' "; $result = mys ...

Maintain the webpage content even after submitting a form with a file attachment on the same page

I am in the process of creating a secure webpage exclusively for our members. This page will allow members to access their personal data stored in the database and upload files as needed. One concern I have is how to return to the member page with all the ...

Adjust the size of flex items to match the size of their container

I'm attempting to design a two-column layout using a flex parent with a height of 100vh. The first column consists of a div containing a heading and a paragraph, while the second column contains a div with an image. I want the flex parent's heigh ...

Expanding a CSS div when the content wraps on smaller screens

When viewing the menu on standard screens, everything looks good. However, on smaller screens, the menu items start to wrap which is fine. The only issue is that the containing div #nav does not automatically enlarge along with it. I would like for the div ...

Lock the "tr" element dynamically at the bottom of the table

Is there a way to keep a specific tr at the bottom of a table using VUE or Js? I have a Vue component that dynamically adds table rows based on an API call. However, I need a specific tr to always be at the bottom, and whenever a new tr is added, it shoul ...

Utilizing jQuery to retrieve the chosen option and then set it as the selected

I created a form that automatically populates shipping fields with the same information as billing fields if the user wants to use identical details. However, I am encountering an issue with the title section - there is a select options list including titl ...

What is a simple way to hide the menu when the user clicks anywhere on the screen?

I've encountered an issue with my code that involves the following functionalities: 1: When a user clicks a button, it toggles the drop-down menu on or off. 2: Clicking anywhere within the webslides element should close the menu if it's displayed ...

Scroll horizontally within the div before scrolling down the page

After reviewing other questions, it's important to note that the scroll I am looking for is horizontal, not vertical. My goal is to have a div on a page automatically start scrolling when it reaches the center or becomes visible, and then allow the pa ...

Accessing the value of a field using JavaScript/jQuery

Welcome to my page! I am working on obtaining the current Start and Finish date values. I plan to add a button for users to click, which will then retrieve all dates and display them somewhere on the page. But for now, I just need a way to access the date ...

The JavaScript code for changing the text to a new line is not functioning correctly

I am attempting to convert a txt file into JSON format using JavaScript. When I input the txt in a single line like this, it works perfectly: <html> <body> <p id="demo"></p> <script> const txt = '{"nam ...

What causes an element to gain an additional 1px of internal height when a border is applied?

Have you ever wondered why the apparent visual height of an element inside a border increases by 1 when a border is placed around it? The reported offsetHeight remains the same, equal to the desired height plus the border widths. This issue becomes especi ...

How to style the videojs chapter menu with CSS to extend the box to the full length of the line

I am currently working on customizing the videojs CSS for the ChapterButton menu in order to make it expand to accommodate varying line lengths without wrapping. Even though I can set it to a fixed width, I require it to be able to adjust to different line ...

Adding attributes to parent DOM elements of a component in Angular2: A Step-by-Step Guide

I'm working with the following code: ... <div class="container"> <div class="fancy"> <fancybutton></fancybutton> </div> <button (click)="addAttribute()">Remove</button> <button (click)="remAttr ...

Challenges with displaying css/bootstrap classes within angular2

Experiencing difficulties with CSS/Bootstrap integration when displayed in an angular2 component. When attempting to display the CSS and HTML content in the Index.html file (with proper CSS and JS references), everything functions correctly. However, once ...

What steps are needed to transform an HTML string into functional HTML code that will be visible on the front end of a website?

When using a v-for loop to iterate through items, the {{ item.data }} contains HTML elements with defined values. For example: The value of {{ item.data }} is a string "<'qr-code value="this has specific infos that is already created for this spec ...

Instructions on changing row color with button click and reverting back to original color upon subsequent click

When the button is pressed, I'd like to change the color. When the same button is pressed again, I want it to revert back to its original color. Currently, when the button is clicked, all rows change color. Sample Code Snippet: <table> & ...

Using jQuery to assign each name the value of the group of radio buttons

For instance: In the database, there are currently five names available (possibly more): ANGGIE, BOB, CHUCK, DEAN, EAST. Here is the HTML code snippet: <form name = "test"> <?php $num++; ?> <td> [each name in the database] </td> ...

Is it possible to align an entire column to the right in the Material UI Data Grid?

I'm currently exploring Material UI and grappling with a specific challenge. Is there a method within Material UI to create a Data Grid with two columns, one left-aligned and the other right-aligned? I've managed to align the headers as desired, ...

Utilize JQuery to interact with div elements

Can anyone please advise me on how to access a DIV within a TD element? <table border="0" cellpadding="0" cellspacing="0" width="95%"> <tr> <td valign="top" style="width:25%"> <img src="../NewsImages/8201043771 ...

What is the best way to include a swf video in a list of hyperlinks?

My current code displays an image inside a box, and I am looking to replace the image with either a swf video or .AVI video. I have the object video code to play a swf video, but I need the video to be played within the box where the image is currently l ...