Issue with ngTable: Error retrieving data for server-side pagination

I am currently working on setting up a server-side table using ng-table. However, I am encountering some issues with the getData function. It keeps giving me errors such as $defer.resolve is not a function or params is not defined.

I noticed that I can access the params variable when I don't use $defer in the getData function.

Here's a snippet of my code:

angular.module('ngTableTest', ['ui.router', 'ngTable'])
.controller('Table', function($state, NgTableParams){
   var vm = this;

   vm.niceTable = new NgTableParams({}, {
      getData: function($defer, params) {
        $http.get('/app/getdata')
            .success(function(data) {
               params.total(data.count);
               $defer.resolve(data.data);
            });
        }
      });
});

Any insights would be greatly appreciated!

Answer №1

When using $defer, don't forget to include $q in your code. Give this a try:

angular.module('ngTableTest', ['ui.router', 'ngTable'])
.controller('Table', function($state, $q, NgTableParams){
   var vm = this;

   vm.niceTable = new NgTableParams({}, {
      getData: function(params) {
        var deferred = $q.defer();
        $http.get('/app/getdata')
            .success(function(data) {
               params.total(data.count);
               deferred.resolve(data.data);
            });
        }
      });
});

Answer №2

I encountered the same problem and managed to find a solution that worked for me:

angular.module('ngTableTest', ['ui.router', 'ngTable'])
.controller('Table', function($state, $q, NgTableParams){
   var vm = this;

   vm.niceTable = new NgTableParams({}, {

     getData: function(params) {

         var promise =  
           $http.get('/app/getdata')
            .success(function(data) {
               params.total(data.count);

               // additional data manipulation can be done here
               return $q.resolve(data); 
            });
           }
        });
        return $q.resolve(promise); 
});

It's important to note that in the current version of NgTable, the getdata method now only accepts one parameter (params), whereas it used to accept two parameters ($defer and params).

It appears that getdata must return a resolved promise. In the code provided, the promise was resolved within the success method instead of directly in the getdata method.

This solution worked for me after some trial and error. I hope that someone with more expertise can provide further insight into how $q.resolve functions and the return value of getdata functions.

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

Struggling to access a property within an object in my Angular controller

Currently, I am utilizing a factory method to extract information about a television show from a database. However, I am encountering difficulties when attempting to send only the episodes field of this object to an angular service. Despite my efforts with ...

Removing cookies after sending a beacon during the window unload event in Chrome

Here's the situation: I need to make sure that when the browser is closed or the tab is closed, the following steps are taken: Send a reliable post request to my server every time. After sending the request, delete the cookies using my synchronous fu ...

Incorporating Information into an Array as a State Object within React.js

Hey everyone, I've hit a bit of a roadblock and I could really use some assistance. I'm trying to figure out why I keep getting an error when attempting to push user input into my array. Can someone take a look at my code and offer some insight? ...

Custom JavaScript clock designed for the Lockscreen of jailbroken iPhones

I am struggling with getting my JavaScript code to display the time correctly on my HTML Lockscreen. After looking at a few examples, I noticed that others are using document.getElementById() instead of document.write() and pushing it to a div. I attempt ...

What is the best way to utilize JavaScript variables that are declared on index.html/index.jsp in Vue.js?

Just starting out with Vue.js and I recently finished developing a Vue.js app using the terminal. I then deployed it on a Java web application and noticed that everything works fine when running it as is. However, I now need to pass a csrftoken to my Vu ...

Trigger an Angular2 component function from an HTML element by simply clicking a button

I'm just starting out with TypeScript and Angular2 and encountering an issue when trying to call a component function by clicking on an HTML button. When I use the **onclick="locateHotelOnMap()"** attribute on the HTML button element, I receive this ...

Obtaining Data from an Array with Reactive Forms in Angular 4

Just starting out with Angular 4 and trying to figure out how to populate input fields with information based on the selection made in a dropdown. <select formControlName="selectCar" class="form-field"> <option value="">Choose a car&l ...

Tips for converting multiple arrays of objects into a single array set

Here is an example of an array: $scope.array1=[[Name:'Madhu'],[Name:'Vijay'],[Name:'Srinu']] I need to convert this array into a different format. I want to pass this array in the columns option of a dx datagrid. So the desi ...

Dealing with problematic hover behaviors in Cypress: A guide

I encountered an issue with Cypress hover functionality while trying to access a sub menu that appears after hovering over a main menu item. The error message I received was This element is not visible because it has CSS property: position: fixed and it&ap ...

ng-bind stops updating after entering text into input field

I am a newcomer to AngularJS and I have encountered an issue that I am struggling to resolve. Although I found a similar question on stackoverflow, the solution provided did not work for me. The problem I am facing is that when I input text into any of the ...

AngularJS struggles to set the default value for linked dropdown menus

I've successfully created a chained dropdown, but I'm struggling to set initial values for them. Here's what I have so far: <select ng-model="check" ng-options="check.name for check in checks"></select> <select ...

"Using SetState frequently results in multiple rerenders of the component

Currently, I am developing a messenger application with a main screen component that displays all messages. My goal is to make sure that whenever a user sends or receives a message, the component updates the Flatlist to show the latest sent message. To ach ...

Top scenario and illustration of utilizing clusters in nodejs

I've been exploring the concept of clusters and I'm still a bit unclear about the most effective use-case scenario for them. Can anyone provide an example to help clarify this for me? ...

Problem with $http.post() in Angular where Codeigniter is not able to receive data

I'm facing a peculiar issue with Codeigniter and Angular. My approach was to configure the controller in the following way: index is a simple Angular page with just one app and one controller get retrieves data from the database set saves data sent u ...

Can security groups be dynamically updated using a JSON file in JavaScript rather than relying on manual parameters?

Looking to enhance security group rules, I aim to have my javascript file extract data from a separate json file instead of relying on json parameters directly. After initially using json parameters for security group updates, I am now exploring the metho ...

What strategies are typically employed to prevent falling into the callback hell trap when working with error-back asynchronous functions in Node.JS?

As a new Node user, I've been practicing pure Node scripting without relying on third-party npm packages. However, I quickly ran into the issue of my code becoming nested with callbacks inside callbacks, making it difficult to follow. This can lead to ...

"Enhance user experience with ui-router: validate and modify URL parameters while transitioning between states

Within my application, I have three distinct parent states, each associated with a specific set of URL parameters. While some parameters overlap across these states, there are unique parameters for each state. For instance: $stateProvider .state(' ...

JavaScript appending checkboxes to a list not functioning as expected

I have not been using jQuery, JavaScript, and HTML to create a list of products. The task I am working on now is the ability to select multiple items from this list. Currently, the HTML list is dynamically populated with <li> elements using JavaScri ...

Can you explain the role of the %GetOptimizationStatus function within a JavaScript file executing in Node.js?

Currently, I am delving into an article that discusses optimization strategies and includes the following code snippet: //Function that contains the pattern to be inspected (using an `eval` statement) function exampleFunction() { return 3; eval(&a ...

How can I transfer a JavaScript value to PHP for storage in an SQL database?

<script> var latitudeVal = document.getElementById("latitude"); var longitudeVal = document.getElementById("longitude"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); ...