The select all state in AngularJS ui-grid fails to update when there is a change in data

My ui-grid has multi-select enabled, and it's generally working well. However, when the data changes, the "select" column in the header doesn't update properly to reflect the grid state. You can see this issue in action in my Plunker example. To reproduce, simply click on the select header button.

https://i.sstatic.net/AHbxr.png

Then hit the "Add Data" button.

https://i.sstatic.net/doG5f.png

After adding a new row, you'll notice that while the header still shows a check mark, the newly added row is not checked.

    var app = angular.module('app', ['ngTouch', 'ui.grid', 'ui.grid.selection']);

app.controller('MainCtrl', ['$scope', function ($scope) {
  $scope.swapData = function() {
    if ($scope.gridOpts.data === data1) {
      $scope.gridOpts.data = data2;
      $scope.gridOpts.columnDefs = columnDefs2;
    }
    else {
      $scope.gridOpts.data = data1;
      $scope.gridOpts.columnDefs = columnDefs1;
    }
  };

  $scope.addData = function() {
    var n = $scope.gridOpts.data.length + 1;
    $scope.gridOpts.data.push({
                "firstName": "New " + n,
                "lastName": "Person " + n,
                "company": "abc",
                "employed": true,
                "gender": "male"
              });
  };

  $scope.removeFirstRow = function() {
    //if($scope.gridOpts.data.length > 0){
       $scope.gridOpts.data.splice(0,1);
    //}
  };

  $scope.reset = function () {
    data1 = angular.copy(origdata1);
    data2 = angular.copy(origdata2);

    $scope.gridOpts.data = data1;
    $scope.gridOpts.columnDefs = columnDefs1;
  }

  var columnDefs1 = [
    { name: 'firstName' },
    { name: 'lastName' },
    { name: 'company' },
    { name: 'gender' }
  ];

  var data1 = [
    {
      "firstName": "Cox",
      "lastName": "Carney",
      "company": "Enormo",
      "gender": "male"
    },
    {
      "firstName": "Lorraine",
      "lastName": "Wise",
      "company": "Comveyer",
      "gender": "female"
    },
    {
      "firstName": "Nancy",
      "lastName": "Waters",
      "company": "Fuelton",
      "gender": "female"
    },
    {
      "firstName": "Misty",
      "lastName": "Oneill",
      "company": "Letpro",
      "gender": "female"
    }
  ];

  var origdata1 = angular.copy(data1);

  var columnDefs2 = [
    { name: 'firstName' },
    { name: 'lastName' },
    { name: 'company' },
    { name: 'employed' }
  ];

  var data2 = [
    {
      "firstName": "Waters",
      "lastName": "Shepherd",
      "company": "Kongene",
      "employed": true
    },
    {
      "firstName": "Hopper",
      "lastName": "Zamora",
      "company": "Acium",
      "employed": true
    },
    {
      "firstName": "Marcy",
      "lastName": "Mclean",
      "company": "Zomboid",
      "employed": true
    },
    {
      "firstName": "Tania",
      "lastName": "Cruz",
      "company": "Marqet",
      "employed": true
    },
    {
      "firstName": "Kramer",
      "lastName": "Cline",
      "company": "Parleynet",
      "employed": false
    },
    {
      "firstName": "Bond",
      "lastName": "Pickett",
      "company": "Brainquil",
      "employed": false
    }
  ];

  var origdata2 = angular.copy(data2);

  $scope.gridOpts = {
    columnDefs: columnDefs1,
    data: data1,
    enableRowSelection: true,
    enableSelectAll: true,
    selectionRowHeaderWidth: 35
  };

}]);

Answer №1

The issue has been successfully resolved, you can find the solution here: http://plnkr.co/edit/lJsWof?p=preview

This is a crucial point to note:

$scope.gridApi.selection.clearSelectedRows();

Follow these steps to check or uncheck all rows:

  1. Obtain the grid object

    $scope.gridOpts.onRegisterApi = function(gridApi) 
    {
        $scope.gridApi = gridApi;
    };
    
  2. Use their methods to clear rows

    $scope.gridApi.selection.clearSelectedRows()
    

For more information, refer to this link:

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 is the reason behind the effectiveness of this prime number verifier?

Take a look at this code snippet that effectively checks whether a number is prime: var num = parseInt(prompt("Enter a number:")); var result = "Prime"; for (var i = 2; i < num; i++) { if (num % i === 0) { result = "Not Prime"; break; } } ...

Combine two arrays and collapse one of the nested elements

I need help merging two arrays based on ID and flattening one of the objects. I haven't been able to find a solution for my specific case. This is what I'm attempting and what I've managed to achieve so far: const a = { someProperty: &a ...

What is the best way to track all method calls in a node.js application without cluttering the code with debug statements

How can I automatically log the user_id and method name of every method called within a javascript class without adding logger statements to each method? This would allow me to easily track and grep for individual user activity like in the example below: ...

Sort through the JSON response data and swap out certain HTML tags

I need to pull information from a database and have it displayed in a div container with each piece of data on a separate line. Here is the data stored in the database: column name - prod_desc data - Camera :25MP | Memory :32GB | Battery: 3400mAh | Ram ...

What steps can I take to transform these two fixed images into an array of multiple images?

After coming across this Pen a while ago, I had it on my to-do list and finally decided to work on it. However, my coding knowledge is not as sharp as it used to be, and I'm currently struggling with transforming it from two hardcoded images to a dyna ...

Storing leaflet marker in Django model for safekeeping

I am working on developing a web application using Django where users can create markers on a map. Currently, I have a JavaScript script that allows users to add markers by clicking on a Leaflet map. window.onload = function () { element = document.get ...

Substitute the nested object's value with a variable structure

I have an object structured like this: const obj = {a: {x: 0, y: 0}} but it can also be structured like this: const obj = {a: {x: 0, y: 0}, b: {x: 10, y: 3}, abcd: {x: -1, y: 0}} This means that the obj can contain multiple keys with variable key names. ...

Facing issues with jQuery and Bootstrap: ERR_CONNECTION_RESET

My jQuery and Bootstrap are causing issues and showing the following error message: GET net::ERR_CONNECTION_RESET GET net::ERR_CONNECTION_RESET Imports: jQuery: <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> ...

Creating a type definition for a TypeScript InfiniteScroll component in React

I am currently in the process of developing an InfiniteScroll component to be used like this: import { getData } from 'api'; import { InfiniteScroll } from 'components'; export const App = () => ( <InfiniteScroll fetcher={page ...

I'm encountering a 404 error with Jquery ajax, indicating that the requested resource was not

Check out the code snippet below: <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="description" content=""> <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/ ...

Troubleshooting: jQuery $.post not functioning as expected in certain circumstances

I'm currently experimenting with inserting data into a MySQL database using AJAX and jQuery's $.post method. To test this functionality, I have created the following setup: In my index.html file (which already includes jQuery for various other f ...

How can I ensure that my Vue components do not interfere with each other's data when they are

Scenario Consider the following vue component: <template> <div> <slot>{{ title }}</slot> <ul> <li v-for="label in labels" :key="label"> <input type="checkbox ...

Tips for extracting a segment from an Object within an Array

I am looking to extract the datetime value from each element in the array below: [<time pubdate class="dt-updated" datetime="2015-07-09T11:50:32+0000" title="Time posted: 09 Jul 2015, 11:50:32 (UTC)" aria-label="Posted on 09 Jul">09 Jul</time> ...

Redraw only a specific offspring

Currently, I am in the process of constructing a dynamic form and my component hierarchy looks like this:- App Caseform DynamicFormBuilder Context.Provider Patients Patient key = "patient_1" ComponentCrea ...

The title of the view in the Ionic framework is not displayed

I am new to the ionic framework and I am fetching data from a SQLite database. Initially, I am able to retrieve the data successfully. For example, here is a sample of my data result: {title: "Test Title", content: "<p>Test hello</p>"} Below ...

Can input be masked by using an alternative placeholder?

Having no labels in my form, I want to utilize a different placeholder using Angular and Angular-UI-Utils-Mask: <div ng-controller="myController"> <input type="text" ng-model="date" ui-mask="99/99/9999" placeholder="Bi ...

Is there a problem with Angular2 using TypeScript?

Currently, I am in the process of setting up my machine for Angular development by following the guidelines provided on https://angular.io/docs/ts/latest/quickstart.html As I proceeded to run "npm start" to launch my site, I encountered an issue with the ...

JavaScript scrollable selection menu with a favorite option button

I am looking to enhance a standard select element by adding a favorite button that can be toggled on and off for each option. Utilizing Bootstrap for styling, I want to create a user interface element that allows for smooth scrolling through the options an ...

Combining Update and Select Operations in Knex JS in a Single Query

With Knex JS - Can all elements from the row being updated be retrieved in a single query that combines both update and select operations? Currently, only the id of the updated row is returned by the update operation. let query = await knex('items&a ...

Loading JQuery dynamically from an externally linked JavaScript file

Our clients can easily add a short script to their website to load our application. This script asynchronously loads the JavaScript that triggers the application on the client's page, typically as a pop-up. The launch code for the application requires ...