0

I need to refresh a list after an update and I've tried all the solutions and none worked for me, I'm a newbie to Angular.

My code is:

index.html

<div ng-controller="customersCrtl">
<div class="container">
<br/>
<blockquote><h4>Registro de Ingreso</h4></blockquote>
<br/>
<div class="row">
    <div class="col-md-2">Limite de Pagina:
        <select ng-model="entryLimit" class="form-control">
            <option>10</option>
            <option>50</option>
            <option>100</option>
            <option>250</option>
            <option>500</option>
        </select>
    </div>
    <div class="col-md-3">Buscar Por:
        <input type="text" ng-model="search" ng-change="filter()" placeholder="Carnet de Identidad" class="form-control" />
    </div>
    <div class="col-md-4">
        <h5>Listados {{ filtered.length }} de {{ totalItems}} Clientes registrados</h5>
    </div>
</div>
<br/>
<div class="row">
    <div class="col-md-12" ng-show="filteredItems > 0">
        <table class="table table-striped table-bordered">
        <thead>
        <th>ID Cliente&nbsp;</th>
        <th>Nombre&nbsp;</th>
        <th>eMail&nbsp;</th>
        <th>Detalles&nbsp;</th>
        <th>Marcar Ingreso&nbsp;</th>
        </thead>
        <tbody>
            <tr ng-repeat="data in filtered = (list | filter:search | orderBy : predicate :reverse) | startFrom:(currentPage-1)*entryLimit | limitTo:entryLimit">
                <td>{{data.id}}</td>
                <td>{{data.name}}</td>
                <td>{{data.email}}</td>
                <td>{{data.extras}}</td>
                <td><a ng-click="updateCliente(data.id)" class="pull-right"><i class="glyphicon glyphicon-ok-sign"></i></a></td>
            </tr>
        </tbody>
        </table>
    </div>
    <div class="col-md-12" ng-show="filteredItems == 0">
        <div class="col-md-12">
            <h4>No customers found</h4>
        </div>
    </div>
    <div class="col-md-12" ng-show="filteredItems > 0">    
        <div pagination="" page="currentPage" on-select-page="setPage(page)" boundary-links="true" total-items="filteredItems" items-per-page="entryLimit" class="pagination-small" previous-text="&laquo;" next-text="&raquo;"></div>


    </div>
</div>

and my app.js

var app = angular.module('myApp', ['ui.bootstrap']);

app.filter('startFrom', function() {
return function(input, start) {
    if(input) {
        start = +start; //parse to int
        return input.slice(start);
    }
    return [];
}
});
app.controller('customersCrtl', function ($scope, $http, $timeout) {
$http.get('ajax/getCustomers.php').success(function(data){
    $scope.list = data;
    $scope.currentPage = 1; //current page
    $scope.entryLimit = 5; //max no of items to display in a page
    $scope.filteredItems = $scope.list.length; //Initially for no filter  
    $scope.totalItems = $scope.list.length;
});
$scope.setPage = function(pageNo) {
    $scope.currentPage = pageNo;
};
$scope.filter = function() {
    $timeout(function() { 
        $scope.filteredItems = $scope.filtered.length;
    }, 10);
};
$scope.sort_by = function(predicate) {
    $scope.predicate = predicate;
    $scope.reverse = !$scope.reverse;
};
$scope.reload = function() {
    location.reload(); 
}; 
$scope.updateCliente = function (cliente) {
if(confirm("Confirma el ingreso?")){
$http.post("ajax/updateCliente.php?idCliente="+cliente).success(function(data){
    reload();
  });
 }
};

});

On the success event of the post, I need to reload. The DB is updated, so the result of the http.post must be a Success.

I have also tried with $window.location.reload(); instead of location.reload(); and it did not work.

5
  • Why do you want to reload? Also, your reload() should be $scope.reload() and you should not use the $http success method as it has long been deprecated. Use then instead of success. Commented Jun 2, 2017 at 5:00
  • Dear Aluan, basically the idea of the webpage is to display a list of customers and a button related to each, when pressed the button display a Confirm and executes a query in updateCliente.php that makes an update on the data of that client, after that, the client is still in the list but, based on the query that builds the table, it must be removed, if I press F5 the client is no more show and the database is all ok... Commented Jun 2, 2017 at 5:08
  • You should prefer to update the list on the client if the request succeeds instead of reloading the page, that is the point of SPAs Commented Jun 2, 2017 at 5:10
  • 1
    Other way to see is.. I need to call again $http.get('ajax/getCustomers.php') but, as this is a template that im using to learn.. i dont know exatcly how... Commented Jun 2, 2017 at 5:12
  • I added an answer showing how to get the data again. It also demonstrates some practices you really should follow Commented Jun 2, 2017 at 5:38

2 Answers 2

1

Don't forget your reload function is in scope so call it via $scope.

Replace

reload();

By

$scope.reload();
Sign up to request clarification or add additional context in comments.

1 Comment

It works!!! :D :D :D thank you so much, if you have any idea about how can i write the app.js to call $http.get('ajax/getCustomers.php') again, it will be really great :D
0

Instead of reloading the page, fetch the data again

Note: I am using then not success as the latter is deprecated, so observe the difference in how data is accessed)

(function () {
  "use strict";

  app.controller({CustomersController});

  CustomersController.$inject = ['$scope', '$http', '$timeout'];
  function CustomersController($scope, $http, $timeout) {

    getData(); // defined below

    function getData() {
      $http.get('ajax/getCustomers.php').then(function (response) {
        $scope.list = response.data;
        $scope.currentPage = 1; //current page
        $scope.entryLimit = 5; //max no of items to display in a page
        $scope.filteredItems = $scope.list.length; //Initially for no filter  
        $scope.totalItems = $scope.list.length;
      });
    }

    $scope.updateCliente = function (cliente) {
      if(confirm("Confirma el ingreso?")) {
        // Below, we pass `getData` as the function that will be called when the 
        // http request completes successfully. This will refresh the data on `$scope`
        $http.post("ajax/updateCliente.php?idCliente=" + cliente).then(getData);
      }
    };
  }

}());

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.