Rearrange the characters within the variable before inserting them after

I'm currently dealing with an external system that generates outdated code, however, I do have access to css and javascript. The specific issue at hand involves working with a table that displays each item on a separate row, sometimes resulting in up to 30 rows. My objective is to split these 'tr' elements into groups of 6 rows, with 5 items in each row.

The initial approach was to modify the style of each 'tr' element to display as table cells, thereby arranging them horizontally. Subsequently, I planned to assign a class to every 5th row and then insert closing and opening 'tbody' and 'table' tags after each 5th 'tr' with the designated class. This procedure was intended to create individual tables for every group of 5 'tr' elements, effectively forming rows of 5 items each.

While most of the setup works as expected, I encountered an issue with the .insertAfter method. The desired behavior was to insert '<\tbody><\table class='tableBreaker'><\tbody>' after every 5th 'tr', unfortunately, it produced unexpected results by reversing the order and outputting '<\table class="tableBreaker"><\tbody><\tbody>'.

I've tried searching for any relevant information regarding how jquery handles string manipulations within the .insertAfter function, but haven't found any clear solutions yet.

Table HTML:


<!-- Table HTML content goes here -->

CSS:


/* CSS styles go here */

jQuery:


// jQuery script goes here

Answer №1

A new function is needed to transfer the specified number of rows, indicated by rowCount, to new table(s) while transferring the old table classes to the new one(s).

function rowTransfer(rowCount) {
  var oldTable = $('#mainContent_rbl_zoneList'),
    holder = oldTable.parent().eq(0),
    rows = oldTable.find('tr');
  var classes = oldTable.prop("classList");
  var temptable = $('<table>');
  $.each(classes, function(index, value) {
    temptable.addClass(value);
  });
  for (var i = 0; i < rows.length; i = i + rowCount) {
    var newTable = temptable.clone(),
      tbody = $('<tbody>');
    rows.slice(i, i + 5).appendTo(tbody);
    newTable.append(tbody).appendTo(holder);
  }
  oldTable.remove();
}
rowTransfer(5);

It is important to note that this could cause issues if there are event handlers attached to the old table, which would need to be addressed.

Additionally, the new tables are applied to the end of the parent container of the old tables.

To adjust the CSS, you can also add:

temptable.addClass("rowcontainer");

and

.rowcontainer tr {
    display: table-cell;
}

Feel free to experiment with this sample: https://jsfiddle.net/n3nqespn/

Answer №2

When working with the insertAfter function to create new tables, your main goal should be to move the rows into these new tables.

One way to achieve this is demonstrated below:

var $container = $('#mainContent_rbl_zoneList'),
    $rows = $('#mainContent_rbl_zoneList tr');

for(var i=0; i < $rows.length; i = i+5 ){
   var $newTable = $('<table>'), $tbody = $('<tbody>');
   $rows.slice(i, i+5).appendto($tbody);
   $newTable.append($tbody).appendTo($container);      

}

Please note that this code snippet serves as a basic example and may need adjustments to account for row classes.

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

What are the best practices for utilizing the JQuery hasData() method effectively?

I have created a simple front-end web application where clicking a button triggers a GET Request to the Foursquare API, and then AJAX stores some of the response data into a div in the following manner: checkinsCount[i] = data.response.venues[i].stats.che ...

Actions for jQuery's mouseenter and mouseleave events

I've implemented a jQuery script that controls the visibility of elements based on mouse events: $("#divid").mouseenter(function() { $('#divid').show(1000); }).mouseleave(function() { $('#divid').hide(1000); }); $("#hldiv" ...

Using @font-face with Rails version 2.3.8

Hello, I am having trouble including a custom font in my Rails application. I am currently using Rails 2.3.8. I have placed my font folder in the public folder and below is my CSS code. However, it seems to not be working. Can anyone provide some assistan ...

The main-panel div's width increase is causing the pages to extend beyond the window's width limit

I recently downloaded a Reactbootstrap theme and managed to integrate it with NextJS successfully. However, I am facing an issue where my <div className="main-panel"> in the Layout.js is extending slightly beyond the window. It seems like t ...

Validation message causing Bootstrap form inline to lose center alignment

Could someone please assist me with form inline in Bootstrap? I am trying to ensure that all inputs are of the same height (or centered), but I'm facing an issue when a validation message is displayed. DEMO: http://codepen.io/Tursky/pen/gpvgBL?editor ...

How to retrieve Rails params hash using jQuery

Can the Rails params hash be accessed in jQuery? I am looking to insert an additional key, which corresponds to a table column, into the params before submitting a form. $('#form_id').submit(function() { // perform some manipulation to r ...

Alter numerous classifications based on varying circumstances at regular intervals

This code snippet is designed to change randomly all the <div class="percentx"> elements, for example from <div class="percent31"> to <div class="percent52"> (with values between 1-100). It works smoothly. I ...

Is it possible to generate multiple modal windows with similar designs but varying content?

I am facing a challenge with 140 link items that are supposed to trigger a modal window displaying a user profile for each link. The issue is that each user profile contains unique elements such as three images, a profile picture, text paragraph, and socia ...

Add a date to my database using an HTML form

I am having trouble inserting a date into my database using PHP. Everything else inserts correctly, but the date reverts to the default 0000-00-00 as if nothing was entered. I have set the data type for the date field. Here is the code snippet: <?php ...

Bootstrap grid columns cannot properly handle overflowing content

Utilizing bootstrap version 5.2.3 I'm facing a challenge not with bootstrap itself, but rather my limited comprehension of the flex system and overflow. Displayed below is a scenario where the first div has a fixed height of 100vh. My goal is to kee ...

The outcome from the PHP file was JSON data, refrain from interpreting it as plain text

I've written PHP code in the file update_customer.php to save customer information. <?php date_default_timezone_set('Asia/Ho_Chi_Minh'); $response = array( 'status' => 0, 'message' => '', 'ty ...

Setting the height of a box-inner div to full height in Bootstrap 4: A guide

Assistance Needed: I have encountered an issue with my design while using bootstrap-4. The box-inner div works perfectly on desktop, adjusting its height according to the content entered. However, on a real iPad, the columns are not of equal height as sho ...

Disabling the scrollbar on a modal and enabling it on a custom div within the modal

Looking to create a bootstrap modal with a unique layout? Interested in having two columns that can be scrolled separately? body, html { font-size: 10px } <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_ ...

Searching for the final element within identical divs using Selenium in Python

My goal is to identify the final element within a div, display it on the console, and pause until the next one appears while utilizing selenium. Despite experimenting with for and while loops, I have not found success as they solely target the first elemen ...

What is the method for accessing a local XML file with $.ajax?

Currently, I am working on developing a Firefox extension that requires reading a local XML file. However, I have encountered an issue with using $.ajax to read the file. Here is a snippet of my code: $.ajax({ type: "GET", url: "file:/// ...

Align a component vertically in a FlexBox using Material UI

I have a React app where I created a card with specified min width and height, containing only a header. I want to add flexbox that occupies the entire left space with justify-content="center" and align-items="center". This way, when I insert a circular pr ...

header color scheme and dimensions

Hello, I've managed to get the menu working correctly on my website at www.g-evo.com/header.php. However, I'm now facing an issue with the size of the header. I'd like to shrink the grey area slightly so it aligns better with the logo, while ...

What is the best way to extract user input from a web form and utilize it to perform a calculation using jQuery?

Is it possible to retrieve user input from a web form and use it to perform calculations with jquery? I am interested in extracting a numerical value entered by a user in a web form. Upon clicking "NEXT", I intend to multiply this number by a predefined c ...

How does JQuery handle color parsing in text?

Is there a way to automatically parse colors in text and apply them to CSS? For instance, if I type #40464f, I want the color to be caught and included in some CSS styles. I am familiar with how to achieve this by adding markup: <span data-color=""> ...

The aesthetic of react-bootstrap and material ui aesthetics is distinctive and visually appealing

As I develop my web application, I have incorporated react-bootstrap for its React components based on Bootstrap. However, wanting to give my project a customized look inspired by Material design, I came across bootstrap-material-design. I am curious if I ...