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

Tips for stopping ajax requests from automatically following redirects in jQuery

When utilizing the jQuery ajax functions to connect with a web service, I encounter an issue where the server redirects the response to a page with a 200 status code instead of providing a proper status code indicating an error. Unfortunately, I am unable ...

Tips for controlling input length using ng-pattern

Currently, I am working on validating phone numbers through ng-pattern. While researching online resources, I came across the following pattern: ng-pattern="/^\+?\d{0,1}?\d{1,10}$" I would appreciate it if someone could explain the signif ...

What's causing Angular to not display my CSS properly?

I am encountering an issue with the angular2-seed application. It seems unable to render my css when I place it in the index.html. index.html <!DOCTYPE html> <html lang="en"> <head> <base href="<%= APP_BASE %>"> < ...

Numbering Tables Effectively in ReactJS

Currently working on coding a table in ReactJS and MUI, my goal is to number the No. column from 1 to the length of the array. This snippet shows my code: <TableBody> {quizes.map((quiz, index) => ( <TableRow key={quiz._id}> ...

Ways to troubleshoot and resolve the jQuery error with the message "TypeError: 'click' called"

I am currently developing a project for managing Minecraft servers, focusing on a configuration panel. I have set up a form that users need to fill out in order to configure the settings and send the values using Ajax. However, I encountered an error: Type ...

Looking to create a drag-and-drop form designer within a web browser - Is AngularJS the optimal framework for this task?

I am in need of developing a web form designer that can be used directly in a browser. Similar to tools like Zoho Creator, proto.io, protoshare, gomockingbird, and lucidcharts. The design will include a tool palette, canvas, and properties box for easy con ...

Having trouble retrieving data from the database when passing input to a mongoose query using an href tag in Node.js

Welcome to My Schema const AutomationSchema=new mongoose.Schema( {EventName:String, EventDate:String, EventLocation:String, EventDetails:String } ) The Events Model const EventsModel=new mongoose.model("Events",AutomationSchema ...

Transforming a JSON object property value from an array into a string using JavaScript

I am facing an issue with an API call I am using, as it is sending objects with a single property that contains an array value (seen in the keys property in the response below). However, I need to work with nested arrays in order to utilize the outputted v ...

Setting up angular-cli project for rc5- Step by step guide

Trying to integrate angular-cli with angular 2 rc5 but facing challenges: The issue of 'Promise' not being found After attempting to install 'npm install -g angular-cli@webpack', typings did not get installed, resulting in WebStorm un ...

"The JavaScript code that functions perfectly in the browser console, but fails to execute when running in the actual

I'm encountering an issue with a simple piece of JavaScript code that seems to only work when executed in the browser console: <script> $(".hopscotch-close").click(function () { alert("Hi"); Cookies.set("tourState", "closed" ...

Laravel validation successfully validates Vanilla AJAX request, but the controller does not receive the values

Currently, I am utilizing AJAX (vanilla JS) to send a form to a Laravel 5.5 controller for searching the Amazon products API. The AJAX is sending the correct keywords and category inputs, but the controller is not receiving them. Even though the request p ...

Discovering ways to access deeply nested JSON data in Vue JS

i am dealing with JSON data that includes payment information. I am facing issues retrieving the paid_amount and date for both cash_payment and installment_payment. { "response": [ { "status": "sold", "price": "1000 ...

What is the process for converting a buffer into an image file?

I am struggling with retrieving images stored in GridFS and converting them into a buffer using the code I have written. However, I'm unable to find proper documentation on how to render an image from this buffer within the MEAN stack environment. My ...

Having trouble invoking a service method within an Angular factory (with Ionic)?

When using Ionic v1 and attempting to invoke a function within a factory using this or self, I am encountering an error message: Uncaught TypeError: Object #<Object> has no method 'connectArd' Here is the code snippet: angular.module(&ap ...

Is there a way to track when the Angular DTOptionsBuilder ajax call is complete and trigger a callback function?

Working with angular datatables, I have the following code: beforeSend:

success callback causes the table on the page not to populate with the data. How can I implement a callback that triggers once the ajax is done without interfering with the nor ...

What is the maximum number of JSON responses that can be handled by AJAX?

Upon entering the site, I am attempting to receive a JSON as an AJAX response. However, I am curious if there is a limit to the size of the object I can retrieve - whether through a GET or POST request? $http({ method: 'POST', url: &apos ...

Detect errors in the `valueChanges` subscription of Firestore and attempt a retry if an error occurs

My Angular app utilizes Firestore for storing data. I have a service set up to retrieve data in the following way: fetchCollectionColors(name) { this.db.collectionGroup('collection-colors', ref => ref.where('product', '==&ap ...

Modify the content of a div by clicking on a word or link using Javascript

I'm attempting to create a clickable word (page 2) that acts as a link to display different div content. When the user selects option 2 and clicks the next button, they should be able to click the "Page 2" text to show the content with id="test1" (Cho ...

The Angular Factory service is accurately retrieving data, but unfortunately, it is not being displayed on the user interface

Here is a link to the complete source code angular .module('app') .factory('Friends', ['$http',function($http){ return { get: function(){ return $http.get('api/friends.json') .t ...

Utilizing jQuery to send AJAX requests and display the results on separate lines within a textarea

My form includes a text area where users can enter keywords, one per line. I would like to implement the following functionality: upon clicking a button, an ajax request will be sent to the server to retrieve the result for each keyword entered. The resul ...