Friday, July 28, 2017

How to find current url & last 2 character of current url using java script.

Current URL & Pathname :

var pathname = window.location.pathname;       // Returns path only
var url      = window.location.href;                     // Returns full URL
alert(pathname);
alert(url);


Current URL Last 2 Character:

var url      = window.location.href.slice(-2);     // Returns last 2 character from current URL.
alert(url);

var member = "my name is amar";
var last2 = member.slice(-2);
alert(last2);        // returns "ar"

$rest = substr("abcdef", -1);    // returns "f"

Wednesday, February 15, 2017

How to Show listing of users, no of record, pagination and search fetch data using angular js?

JS file:

var adminmodule=angular.module('tasteapp',['ngRoute','ngResource','ui.bootstrap']);

 adminmodule.filter('startFrom', function () {
    return function(input, start) {
         if (!angular.isArray(input)) {
            return [];
         }
         start = +start; //parse to int
        return input.slice(start);
   };
});

OR

adminmodule.filter('startFrom', function() {
    return function(input, start) {
        if (!input || !input.length) { return; }
        start = +start; //parse to int
        return input.slice(start);
    }
});


/*Users management*/

adminmodule.controller("userslist", ["$scope", "$http","$filter","$timeout" , function($scope, $http,$filter,$timeout) {

    $scope.message="user history";
    $http.get(base_ur+"/users/userlistdata").success(function(response) {//$scope.userdata = response;
    $scope.users = response;

    if($scope.users.length > 0)
        {
            for(var i=0; i< $scope.users.length;i++)
            {
                $scope.users[i].sr_no = i+1;
            }
        }

        $scope.currentPage = 1; //current page
        $scope.entryLimit = 10; //max no of items to display in a page
        $scope.filteredItems = $scope.users.length; //Initially for no filter
        console.log($scope.filteredItems);
        $scope.totalItems = $scope.users.length;

        $scope.itemsPerPage = $scope.entryLimit;
    });

    $scope.setPage = function(pageNo) {
        $scope.currentPage = pageNo;
    };
    $scope.filter = function() {
        $timeout(function() {
            $scope.filteredItems = $scope.filtered.length;
        }, 10);
    };
    $scope.setItemsPerPage = function(num) {
      $scope.itemsPerPage = num;
      $scope.currentPage = 1; //reset to first paghe
    };
 
    $scope.sort = function(keyname){
    $scope.sortKey = keyname;   //set the sortKey to the param passed
    $scope.reverse = !$scope.reverse; //if true make it false and vice versa
  }
}])

Controller file:

  public function userlistdata()
    {      
        $usersresult = $this->usermodel->getuserList();
        echo json_encode($usersresult);    

    }


View file:

 <tr ng-repeat="user in filtered = (users | filter:search | orderBy : sortKey :reverse) | startFrom:(currentPage-1)*entryLimit  | limitTo:entryLimit"></tr>

 <div class="col-md-12" style = "margin-left: 10px;" ng-show="filteredItems > 0">
 <pagination total-items="totalItems" ng-model="currentPage" ng-change="pageChanged()" class="pagination-sm" items-per-page="itemsPerPage"></pagination>
 </div>