The content in Angular UI Grid fails to display until the browser window is adjusted

I am currently utilizing angularjs 1.5.0 along with angular ui grid 3.1.1. In my controller, I assign the gridOptions object (which is passed to the grid directive) in the following way:

$scope.gridOptions = {
      data: [{"mock2": 1, "mock1": 2}, {"mock2": 10, "mock1": 22} ]
    };

HTML:

<div ui-grid="gridOptions"></div>

The table displays correctly at this point. However, when attempting to update the data within $scope.on:

$scope.$on('update', function (event, passedFromBroadcast) {
      $scope.gridOptions.data= [{"mock2": "set", "mock1": "inside"}, {"mock2": "$scope", "mock1": "on"} ] ;
    });

Only the frame of the table is rendered. When pagination is included, it will also display pagination controls - but not the actual content itself. The rows and column headers only appear after resizing the browser window.

  1. Why does the angular-ui grid not update the table content when the data changes within $scope.on?
  2. How can I manually trigger an update for the angular ui grid table? Here are the methods I have already attempted:

    $scope.gridApi.core.handleWindowResize(); // Does not work
    $scope.gridApi.core.refresh(); // Does not work
    $timeout(function(){$scope.gridOptions.data= [{"mock2": "set", "mock1": "inside"}, {"mock2": "$scope", "mock1": "on"} ] ;}) // Does not work
    

Answer №1

One common issue occurs when the gridOptions.data length remains unchanged after an update. A suggested workaround is to clear the data and then refresh it using $timeout.

$scope.$on('update', function (event, passedFromBroadcast) {
  $scope.gridOptions.data = null
  $timeout(function(){
    $scope.gridOptions.data = [{"mock2": "set", "mock1": "inside"}, {"mock2": "$scope", "mock1": "on"} ] ;
  });
});

Answer №2

Two key issues are present in the code provided, but the main reason for the table contents only appearing after resizing the browser window is the absence of a CSS class that defines the width and height. Here is a basic example that works:

.table {
  width: 500px;
  height: 250px;
}

In the HTML:

<div class="table" ui-table="tableOptions"></div>

The other issue, as mentioned by others in this discussion:

  • When assigning fields to tableOptions (including data and columnDefs), it must be done within $timeout. Additionally, both the data and columnDefs need to be cleared before making any changes. If not, the changes may not be visible and the table contents and headers will remain unchanged (a known bug in ui-table as mentioned by @maurycy)

Answer №3

One method that has proven effective for me in the past is placing the window resize handler on $interval within the gridOptions.onRegisterApi function:

var gridResizeHandlerInterval; 

$scope.gridOptions = {
  ..., // etc.
  onRegisterApi: function(gridApi) {
    ctrl.gridApi = gridApi;

    gridResizeHandlerInterval = $interval( function() {
      $scope.gridApi.core.handleWindowResize();
    }, 10, 500); 
  }
};

Remember to properly clean up and cancel the interval when your controller is destroyed:

$scope.$on('$destroy', function() {
  $interval.cancel(gridResizeHandlerInterval);
}); 

I hope this solution proves useful to you...

Answer №4

Here is a suggestion you can try out:

$scope.$on('update', function (event, passedData) {
  $scope.gridOptions.data.length = 0;
  $timeout(function(){
    $scope.gridOptions.data = [{"value2": "updated", "value1": "changed"}, {"value2": "$scope", "value1": "broadcast"} ] ;
  });
});

I hope this solution works for you!

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

I am looking to superimpose one rectangle over another rectangle

I am looking to create something similar using CSS and TypeScript/JavaScript: Could someone please guide me on how to achieve this? My attempt with a flex container looks like this: I am new to front-end development. Can anyone point out what I might be ...

retrieve the classname by tapping into the parsed JSON

Being a newcomer to this technology, I am embarking on my first project. My task involves calling an API and receiving a large parsed JSON file. Within this extensive JSON response (which contains HTML code), I need to extract a specific class from the te ...

Manipulating global state properties within a reducer function in React: How to do it efficiently

I am looking to implement a single loader (backdrop and spinner) within my app component that can be displayed based on the value of a property in the Redux state called showLoader. This loader should be accessible throughout my entire React application. T ...

Unable to dynamically load a script located in the node_modules directory

This particular approach is my go-to method for loading scripts dynamically. import {Injectable} from "@angular/core"; import {ScriptStore} from "./script.store"; declare var document: any; @Injectable() export class ScriptService { private scripts: an ...

Moving icon that appears when hovering over a menu button

Before diving into this, please take a moment to visit the following website to understand my goal: You'll notice there is a noticeable RED arrow positioned below the menu. What I aim to accomplish is... when I hover over a menu button, the arrow smo ...

methods for extracting header information using JavaScript

Is there a way to retrieve values from the header URL using JavaScript? Consider this scenario: www.example.com/content.html?param=value How can one extract this information upon redirecting to the page content.html? What techniques could be employed f ...

Incorporate concealed image into HTML

When I perform actions with a couple of images, I encounter delays because the browser needs to load the images. For example, if I use $('body').append(''); everything works smoothly without any delays. However, when I try using style= ...

Include a function call within a ternary operator in JSX code

I am looking to call a function within a ternary operator condition. Here is what my code looks like: {snapshot.Bid[0].Price !== 'undefined' ? `(${initialOrderInfo.snapshot.Bid[0].Price}` {renderCurrencySymb ...

React: Struggling to retrieve specific elements from an array stored in component state

For a fun side project, I've taken on the challenge of creating a miniature Pokedex app using React. import React, { Component} from 'react'; import './App.css'; import Card from './components/card/Card.component'; clas ...

conceal the "load more" option when no additional content is available for loading

I recently incorporated a "load more" button using the plugin called "easy load more" (https://wordpress.org/plugins/easy-load-more/). While the button is functioning well, it continues to appear even when there are no more posts to display. I am seeking s ...

Is there a way to execute a JavaScript file within another JavaScript file?

I'm in the process of developing a text-based game through the console, utilizing an NPM package known as "prompts" to prompt the user for input. One specific question it asks is, "What OS do you want to run?" and returns the selection as JSON data. F ...

Retrieving row and column values from an 81-element indexed array

I am working with an indexed JavaScript array that contains exactly 81 elements. These elements will form a 9x9 grid, similar to a Sudoku puzzle. Right now, I am trying to figure out the most efficient way to extract row and column values from this array. ...

What are some ways to direct users from one page to another without relying on server-side programming?

Is there a way to create a redirect page using jQuery or JavaScript? What is the process of writing client-side scripting code to redirect the browser from one page (page1) to another page (page n)? ...

Exploring the option of showcasing multiple json_encode data on a pie chart

Hey there! I'm currently utilizing Chart.js to generate a pie chart by retrieving data from the database: <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script> <script> var ctx = document.getE ...

How to host two Node.js socket.io projects on one URL

My Nodejs server is hosted on Amazon Lightsail. I have successfully connected to it and configured Apache for Nodejs. I am working with 2 Nodejs socket.io projects and I want to access both of them using a single URL. Below are the links in Nextjs: const ...

Is there a way to adjust the image dimensions when clicked and then revert it to its original size using JavaScript?

Is there a way to make an image in an HTML file change size by 50% and then toggle back to its normal size on a second click using JavaScript? I've attempted to do it similar to the onmouseover function, but it doesn't seem to be working. Any sug ...

A method to use jQuery to replace newlines with commas in user input

Input Processing Challenge <input> Whenever there is multi-line text pasted into the input field, I need to replace newlines (\r, \n, and \r\n) as well as tabs \t with commas ,. This scenario mainly occurs when users copy c ...

The $http get request in AngularJS functions properly in Chrome but unexpectedly returns null in FireFox

My MVC app utilizes AngularJS without the use of WebAPI. The AngularJS makes calls to the MVC Controllers which work perfectly in IE and Chrome. However, I have been struggling for hours to make it work in Firefox and Safari without any success. The code s ...

How can we integrate Cordova plugins into Vue-Cordova framework?

Looking to integrate Vue-Cordova with Cordova-plugin-file-opener2 to open PDF files within iOS/Android applications. In the Vue-Cordova setup, plugins related to the device are defined on the data property of the App vue instance: data: function () { ...

Enhance the timepicker output of angular-ui by converting it into a user-friendly string format

Below is the code snippet for the timepicker feature, along with some accompanying text: <timepicker ng-model="mytime" hour-step=1 minute-step=1 show-meridian="ismeridian"></timepicker> I want to add the phrase "Presently, the time is ", howe ...