Newly added rows to the table after filtering remain visible

I am currently using the DataTable API and jQuery to create a table by fetching rows from the server. However, I encounter an issue when I click a button to load additional rows and append them to the end of the table. The problem arises when I try to hide certain columns: the initial rows lose the hidden columns, but the ones loaded later are not affected by the filters. How can I resolve this issue? Upon further investigation, I discovered that the filtering function retrieves all rows, not just the initial ones.

var data = $(this).dataTable().toArray();
  if ($this.prop("checked") === false) {
    data.forEach(function (element) {
       $(element).DataTable().columns().column(column).visible(false);
    });
} else {
  data.forEach(function (element) {
    $(element).DataTable().columns().column(column).visible(true);
  });
}

Adding the remaining rows

var tbody = $("#tbody-" + $this.attr("data-parent-id"));
var dt = tbody.parents(".bizon-datatable").DataTable();
$(result).filter("tr").each(function (i, v) {
  dt.row.add($(v));
});

tbody.append(result);

Answer №1

It's important to remember that each operates on the existing set of items when the selector is run. If there are initially 10 rows, each will only apply to those 10 rows and any additional rows added later will not be affected by each.

The same principle applies to filter. It only works on elements returned by the selector at the time it is executed, not on any future elements added. The selector only picks DOM elements that match at the time of execution and does not extend to any new DOM elements added later. Therefore, you need to re-select DOM elements after adding new ones if you want filters or conditions to apply to all current DOM elements.

To solve this issue, you will have to re-apply the filter after adding rows or re-run the each if new rows have been added.

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

Sharing information through a jQuery post method

Utilizing jQuery $.post('target.php', data, function(result) { window.open('target.php'); }); Within target.php var_dump($_POST); Output: array(0) { } The code above is placed in the Ajax success function where I intend to ope ...

Unable to input text into text fields or interact with buttons by clicking or hovering in a React form

I am new to React and currently working on a login page that asks for both a username and password. While I haven't implemented much logic yet, I have been exploring online resources to learn more. It appears that I should be able to interact with the ...

Is there a way to retrieve all div elements within a specific div using webdriver and selenium?

As a newcomer to the world of web scraping, I am currently working on scraping a website in order to extract all elements with the data-feature-id=homepage/story. My ultimate goal is to access a subelement within each of the divs contained in the parent el ...

Having difficulty converting an object into an iterable using an arrow function

Currently immersed in learning JavaScript, I successfully created a class and now find myself faced with the task of making it iterable. The Group class represents a collection of values, akin to a Set, equipped with methods for adding, removing, and che ...

The onTableChange function in Mui-Datatable does not seem to be functioning properly when attempting to

Utilizing mui-datatable, I came across an official example on codesandbox where setState can be used on the tableState. You can check out the example here: https://codesandbox.io/s/trusting-jackson-k6t7ot?file=/examples/on-table-init/index.js handleTableIn ...

Using JavaScript and jQuery to compare two JSON objects, then storing the result in a separate object

I am currently working on an API call that provides an updated JSON object, as well as a static JSON object file. My goal is to compare a specific value in the objects for teams with the same name. For example, if Team John had 22 members in the old file ...

React code to automatically scroll to the top of the page when the user clicks the

Whenever the URL changes or the page is reloaded in my project, I need it to scroll back to the top. While most scenarios work fine, I'm encountering an issue with the browser's back button. Despite a change in the pathname, the page fails to scr ...

What is the best way to dynamically adjust the width of multiple divisions in Angular?

I am currently working on an angular project to create a sorting visualizer. My goal is to generate a visual representation of an array consisting of random numbers displayed as bars using divisions. Each bar's width will correspond to the value of th ...

Deciphering unidentified Json data

Having some trouble with an error in my note taker app built using expressjs. Everything was working fine until I tried to save a new note and it's throwing this error: SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse () Here&apos ...

Wordpress-inspired navigation on the left

Currently, I am in search of a JavaScript library, preferably built with jQuery, that can replicate the functionality of the left menu bar in WordPress when a user is logged in. This menu features images with text, the ability to expand and collapse, a ho ...

Investigating Event Bubbling in Chrome's JavaScript

I created an HTML document that contains several div elements: <div id='divTop'> <div id='divChild'></div> </div> At the end of the document, I added the following JavaScript code: var top = document.getEl ...

Steps for adding custom text/symbols from a button on the textAngular toolbar

My goal is to include a button on the toolbar that allows users to insert © into the textangular editor (). However, I am struggling to grasp how to add functionality to my registered button. The examples provided by textangular for custom functionali ...

PHP line breaks are not functioning as expected

I am having trouble formatting the email with line breaks to display as: Name: Email: Message: I attempted adding \r\n but it had no effect. Then I tried including $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n ...

Can a condition be incorporated in a gulpfile to execute a task depending on the size of a file?

Currently, I am utilizing gulp for image compression. However, my requirement is to only compress images exceeding 200kb in size. Can JavaScript be used to loop through a directory and selectively run the minification process on files larger than 200kb? ...

What is the process for inserting a scroll bar within a div element?

   I have recently created a webpage using some divs, along with a bit of CSS and JavaScript. I am struggling to figure out how to add a scrollbar to one of my divs. The code is not overly complex, as it includes both CSS and JavaScript. <html> & ...

Leverage CSS styling for database values within a PHP framework

How can I style MYSQL database values in HTML using CSS? My vegetarian database row has 'Y' for yes and 'N' for no. I want to apply different styles to these values using CSS as I display them on my PHP-based page. if ($result1->num_ ...

Customize the Bootstrap Navbar to display background only on the right side and not across the entire width

I am utilizing Bootstrap's navbar, specifically using the dropdown toggler icon on the right side. I am looking to have a background that only covers the right portion of the width and not the entire width. Any suggestions or insights would be greatly ...

Different techniques to access values in a JSON array without relying on specific index positions

After making a request to an API, I received a JSON array with various attributes. One of the attributes is "State" and my task is to extract all the "EventName"(s) where State: "PR". The user will select the state from a dropdown menu. In order to achiev ...

What could be causing my fixed header to disappear in Safari 5?

I am experiencing a strange issue with my fixed header on Safari 5 for iPad. The header disappears once it scrolls down to a certain point, even though it works fine in IE, Firefox, Chrome, and the latest version of Safari. Has anyone else encountered this ...

Unable to connect to Angular's datepicker module

I decided to use the Angular DatePicker library (https://github.com/alongubkin/angular-datepicker) in my Ionic app because I wanted to avoid including jQuery just for the calendar feature. In my controller, I have set the default date to today's date ...