The ngTable feature in AngularJS fails to initiate the getData function when making an HTTP request

I am currently working on integrating ngTable with AngularJS.

After successfully installing all necessary Angular dependencies, everything seems to be functioning well.

However, I am encountering an issue where my HTTP GET request is not triggering as expected when using ngTable.

Could someone kindly point out what might be missing in my code?

Here is the code snippet in question:

angular.module('clinang', ['ngTable']).controller('pacientesCtrl', function($scope,$http,NgTableParams){
  this.tableParams = new NgTableParams({
    page: 1,
    count: 10
  }, {
    getData:  function($defer, params) {
      $http.get('/getdadospac/?oper=S', {params: {
        pageNumber:params.page() - 1,
        rangeStart:rangeStart,
        rangeStop:rangeStop}})
      .then(function(data, status) {

        params.total(data.results.total);

        $defer.resolve(data.results);
      });
    }
  });


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link rel="stylesheet"; href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7c121b51081d1e10193c4e52">[email protected]</a>/bundles/ng-table.min.css">
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b656c267f6a69676e4b3925">[email protected]</a>/bundles/ng-table.min.js"></script>
<body ng-app="clinang">
  <div ng-controller="pacientesCtrl">
    <a class='btn btnprimary' href='/getdadospac/?oper=S' >Button</a> 
    <table ng-table="vm.tableParams" class="table" show-filter="true">
      <tr ng-repeat="paciente in $data">
        <td title="'Pront'" filter="{ name: 'text'}" sortable="'pront'">
          {{paciente.pront}}</td>
        <td title="'Nome'" filter="{ age: 'number'}" sortable="'nome'">
          {{paciente.nome}}</td>
      </tr>
    </table>
  </div>
</body>

Answer №1

After creating the control, make sure to use the reload() method for it to work correctly.

Instead of using .success, remember to use .then method for $http.

I have added a debugger call in the getData function, so when you open your DevTools, you can observe that it will be executed.

angular.module('clinang', ['ngTable']).controller('pacientesCtrl', function($scope,$http,NgTableParams){
  this.tableParams = new NgTableParams({
    page: 1,
    count: 10
  }, {
    getData:  function($defer, params) {
    debugger;
      $http.get('/getdadospac/?oper=S', {params: {
        pageNumber:params.page - 1}})
      .then(function(data, status) {

        params.total(data.results.total);

        $defer.resolve(data.results);
      });
    }
  });
    this.tableParams.reload();


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link rel="stylesheet"; href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f2128623b2e2d232a0f7d617f617d">[email protected]</a>/bundles/ng-table.min.css">
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4729206a3326252b22077569776975">[email protected]</a>/bundles/ng-table.min.js"></script>
<body ng-app="clinang">
  <div ng-controller="pacientesCtrl">
    <a class='btn btnprimary' href='/getdadospac/?oper=S' >Button</a> 
    <table ng-table="vm.tableParams" class="table" show-filter="true">
      <tr ng-repeat="paciente in $data">
        <td title="'Pront'" filter="{ name: 'text'}" sortable="'pront'">
          {{paciente.pront}}</td>
        <td title="'Nome'" filter="{ age: 'number'}" sortable="'nome'">
          {{paciente.nome}}</td>
      </tr>
    </table>
  </div>
</body>

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

Inconsistency in handling express routes results in empty req.body in certain routes

I have two different paths to follow. Before each request, a method needs to be executed: app.all('*',function(req,res,next){ console.dir(req.body); // Additional operations }); When I send a POST request to my first path: $http.post ...

callback triggering state change

This particular member function is responsible for populating a folder_structure object with fabricated data asynchronously: fake(folders_: number, progress_callback_: (progress_: number) => void = (progress_: number) => null): Promise<boolean ...

Issue with making requests across origins in Vuejs using axios

Currently, I am utilizing Vuejs and axios for handling web requests. So far, I have successfully managed to perform GET, POST, and PUT operations. However, the new challenge that I am facing is uploading media to the server. The server necessitates sending ...

Where does the browser retrieve the source files for "sourcemapped" JavaScript files from?

As I begin working on an existing project built with angular JS, upon opening chrome dev tools and navigating to the "source" view, a message appears: Source map detected... This prompts me to see a link to: https://i.stack.imgur.com/RZKcq.png The fi ...

How can I position text below a ticked checkbox?

Having an issue with my HTML and jQuery code. Everything is working fine, but when I click on a checkbox, the text "FINISHED" appears below all checkboxes instead of just the one I clicked on. This is my html code: $('.label__checkbox').cli ...

Replace the value of Undefined with an empty string

I have been utilizing exif.js to extract metadata from the images I upload on a content management system (CMS). However, sometimes when an image does not contain any metadata, certain values may show up as "undefined". My goal is to replace these "undefin ...

Is there a way to achieve a transparent background while animating the second text?

I am seeking to create a unique typography animation that involves animating the second text, which is colored and consists of multiple text elements to animate. The animation should showcase each text element appearing and disappearing one after the other ...

Is it necessary for services in Domain Driven Design to have knowledge of other services, or should they be aware of multiple repositories

As I work on developing a backend application, my focus is on implementing the Domain Driven Design. However, there's a particular question I have regarding the data structure that requires some clarification. Database Configuration Users Id ( ...

Tips for customizing the appearance of dayMouseover in Fullcalendar

While working on my Angular application, I encountered an issue with the Fullcalendar plugin. Specifically, I want to dynamically change the cell color when hovering over a time slot in AgendaWeek view (e.g. 7.30am-8.00am). I am striving for something like ...

Exploring the Angular Checkbox n-Change - is it really all about 'this'?

Looking for a solution with a series of checkboxes: <input type="checkbox" ng-change="??????"> I am trying to figure out how to set $scope.mode.someOtherValue = false when the checkbox is checked. Any ideas on how to extract the checkbox value wi ...

Node causing CORS problem

As I work on setting up an app that enables cross-domain requests, I have been exploring various methods from a helpful post on How to enable cross-origin resource sharing (CORS) in the express.js framework on node.js Despite trying multiple approaches ou ...

Angular Material Dialog: Establishing focus on the initial element

Currently, I am working with Angular 1.5.x and Material Design. I have created a custom Dialog box where I want the first input field to be selected when the dialog is displayed. Do you know how I can achieve this? To better illustrate my issue, I have s ...

Using React Native to trigger a function upon receiving a notification

Can a function in javascript be executed in react native when receiving a remote notification? Will a remote notification trigger react native to run in the background? Alternatively, should this be handled with Swift/Objective-C instead? ...

Tips for verifying the input field with specific requirements in Angular 6

There is an input field that needs to validate text based on certain logic conditions: No spaces should be allowed in the formula. The operators (and,or) must be lowercase and enclosed in curly brackets {}. The number of opening '(&apos ...

Access your account through rest utilizing Restangular

Just trying to execute a simple login: var test = {login: 'testuser', password: 'asd'}; Restangular.one('/auth/login').post(test); When this code is executed, it sends a request to "auth/login/[object%20Object]" and I receiv ...

Getting child objects from an imported model in Three.js

I need help accessing a specific child mesh from a 3D model I imported that contains multiple child objects. Despite using .getObjectByName("Cylinder", true), I keep receiving undefined even though the model does have a child object with that name: https ...

Issue with Angular Bootstrap UI Typeahead functionality when implementing async loading approach

I am currently facing a minor issue with the Typeahead directive in my code. Here is what I have tried so far: $scope.getUserNames = function (input) { return $http.get(ApplicationConstants.apiUrl + 'api/Users/UserNames', { ...

The use of AngularJs with a hash in the href can disrupt scrolling functionality and alter the URL to include a combination of hash

How can I make AngularJs scroll to a specified anchor element using the href attribute with an #id? The issue When using an anchor link with a hash, like href="#recommanded", it becomes url#/recommanded instead of url#recommanded, causing it not to scrol ...

transform data into JSON format and transmit using jquery ajax

I have one object: var myobject = {first: 1, second: {test: 90}, third: [10, 20]}; and I need to convert it into a JSON string using jQuery ajax. Can someone please advise on how to achieve this? (I tried using JSON.stringify(), but it didn't work ...

Guide on designing a navigation list with unique buttons that alter the displayed content based on the selected button

As a newcomer to the world of programming, I have a basic inquiry that I hope you can assist me with. I have successfully created a navigation list, but I am struggling to make it functional. For instance, if my navlist consists of 3 buttons: Home, About, ...