• We just launched and are currently in beta. Join us as we build and grow the community.

Server-Side Table Pagination using PHP, AngularJS, and Bootstrap 5 Tutorial

fizzy88

Protocol Vulnerability Finder
F Rep
0
0
0
Rep
0
F Vouches
0
0
0
Vouches
0
Posts
142
Likes
85
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, you will learn how to Create a Server-Side Table Pagination using PHP, MySQL Database, AngularJS, and Bootstrap 5 Framework. The main purpose of this tutorial is to provide the IT/CS students and new programmers with a reference for implementing a Server-Side Pagination. Here, snippets are provided and a sample web application source code is also provided and is free to download.

What is Server-Side Table Pagination?

Server-Side Table Pagination is one of the common features of web applications. This feature helps to optimize the speed of the page load. It is a process of dividing the data into discrete pages. It also prevents exhausting the database of retrieving data.

How to Create a Server-Side Table Pagination in PHP, JavaScript, and AngularJS?

To create a Server-Side Table pagination using AngularJS, we will use $scope, $http, and $compile API. Using JavaScript and Bootstrap 5 Framework, we can create a script that generates the pagination buttons dynamically. And on the server side, using PHP and MySQL Database, we can query the limited data using LIMIT and OFFSET clauses of MySQL. Check out the snippets below that I prepared for this tutorial to have some idea of how to achieve this tutorial's objective.

Sample Web Application Snippets

The following snippets of the simple web application demonstrate the Server-Side Table Pagination.

Technologies

Here are the following technologies used to develop the snippets below.

  • HTML
  • CSS
  • JavaScript
  • PHP
  • MySQL Database
  • AngularJS
  • Bootstrap 5 Framework

Database

For this tutorial purposes only, let's create a new database named dummy_db and use the following MySQL Schema and Table Data.


  1. CREATE

    TABLE

    `employee_

    list`

    (
  2. `id`

    mediumint

    (

    8

    )

    UNSIGNED

    NOT

    NULL

    PRIMARY KEY

    AUTO_INCREMENT

    ,
  3. `name`

    varchar

    (

    255

    )

    DEFAULT

    NULL

    ,
  4. `phone`

    varchar

    (

    100

    )

    DEFAULT

    NULL

    ,
  5. `address`

    varchar

    (

    255

    )

    DEFAULT

    NULL


  6. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    utf8mb4;


  7. INSERT

    INTO

    `employee_

    list`

    (

    `id`

    ,

    `name`

    ,

    `phone`

    ,

    `address`

    )

    VALUES


  8. (

    1

    ,

    'Bradley Charles'

    ,

    '1-387-631-7773'

    ,

    '201-7452 Interdum Rd.'

    )

    ,
  9. (

    2

    ,

    'Lysandra Schwartz'

    ,

    '1-329-483-7978'

    ,

    '9649 Dolor Road'

    )

    ,
  10. (

    3

    ,

    'Whitney Moran'

    ,

    '(392) 138-7649'

    ,

    'Ap #502-4286 Elementum Av.'

    )

    ,
  11. (

    4

    ,

    'Flavia Adkins'

    ,

    '1-517-765-3895'

    ,

    '563-8222 At Street'

    )

    ,
  12. (

    5

    ,

    'Vladimir Bradley'

    ,

    '1-436-562-8352'

    ,

    'P.O. Box 170, 9079 Amet, Ave'

    )

    ,
  13. (

    6

    ,

    'Asher Osborn'

    ,

    '(255) 217-5562'

    ,

    '332-4023 Ridiculus Street'

    )

    ,
  14. (

    7

    ,

    'Shaine Mathews'

    ,

    '1-528-220-9818'

    ,

    'P.O. Box 865, 3851 Dui. Road'

    )

    ,
  15. (

    8

    ,

    'Donovan Todd'

    ,

    '1-615-939-1360'

    ,

    'P.O. Box 637, 895 Ac, Avenue'

    )

    ,
  16. (

    9

    ,

    'Jackson Ward'

    ,

    '1-305-537-3752'

    ,

    '199-8650 Sapien. Rd.'

    )

    ,
  17. (

    10

    ,

    'Naida Michael'

    ,

    '(157) 545-9897'

    ,

    '502-3887 Ultrices. St.'

    )

    ,
  18. (

    11

    ,

    'Scarlet Willis'

    ,

    '1-761-717-4296'

    ,

    '9935 A, St.'

    )

    ,
  19. (

    12

    ,

    'Colette Carr'

    ,

    '1-586-832-2814'

    ,

    'Ap #654-7677 Mattis Ave'

    )

    ,
  20. (

    13

    ,

    'Macy Pena'

    ,

    '1-322-550-6531'

    ,

    '6567 Malesuada Rd.'

    )

    ,
  21. (

    14

    ,

    'Rooney Mckinney'

    ,

    '(295) 257-5029'

    ,

    'P.O. Box 444, 1418 Vivamus Street'

    )

    ,
  22. (

    15

    ,

    'Denton Wilder'

    ,

    '1-464-768-6866'

    ,

    '798 Dictum St.'

    )

    ,
  23. (

    16

    ,

    'Jin Emerson'

    ,

    '(295) 421-5258'

    ,

    '394-5031 Risus. Rd.'

    )

    ,
  24. (

    17

    ,

    'Jack Rose'

    ,

    '(716) 946-1411'

    ,

    'Ap #511-7704 Odio Rd.'

    )

    ,
  25. (

    18

    ,

    'Eric Serrano'

    ,

    '(911) 717-7798'

    ,

    'P.O. Box 760, 9708 Penatibus Road'

    )

    ,
  26. (

    19

    ,

    'Forrest Andrews'

    ,

    '1-657-248-8527'

    ,

    '600-6646 Tellus Rd.'

    )

    ,
  27. (

    20

    ,

    'Elijah Gregory'

    ,

    '(252) 814-5344'

    ,

    'Ap #372-9501 Aliquet Avenue'

    )

    ,
  28. (

    21

    ,

    'Phoebe Moses'

    ,

    '1-332-384-8360'

    ,

    '746-9767 Suspendisse St.'

    )

    ,
  29. (

    22

    ,

    'William Peters'

    ,

    '(850) 847-5722'

    ,

    'P.O. Box 382, 4880 Neque St.'

    )

    ,
  30. (

    23

    ,

    'Basil Parsons'

    ,

    '(751) 775-5828'

    ,

    'Ap #497-4995 Per Rd.'

    )

    ,
  31. (

    24

    ,

    'Mufutau Bradley'

    ,

    '(935) 451-8759'

    ,

    '520-315 Congue, Avenue'

    )

    ,
  32. (

    25

    ,

    'Ciaran Bradshaw'

    ,

    '(788) 774-8911'

    ,

    'Ap #388-1102 Magna Ave'

    )

    ;

Database Connection

Next, in our source code folder, let's create a new PHP File and save it as db-connect.php. This file contains the PHP Script that connects our created database to our web application.

db-connect.php

  1. <?php
  2. $host

    =

    "localhost"

    ;
  3. $username

    =

    "root"

    ;
  4. $pw

    =

    ""

    ;
  5. $db_name

    =

    "dummy_db"

    ;

  6. $conn

    =

    new

    mysqli(

    $host

    ,

    $username

    ,

    $pw

    ,

    $db_name

    )

    ;

  7. if

    (

    !

    $conn

    )

    {
  8. die

    (

    "Database Connection Failed"

    )

    ;
  9. }

Page Interface

Next, we'll create the page interface of the application which contains a table for listing the data from the database. The script contains the HTML elements and AngularJS that are needed for the application. Save the snippet below as index.php.

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  6. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  7. <title

    >

    Angular JS Table Pagination</

    title

    >
  8. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css"

    integrity=

    "sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A=="

    crossorigin=

    "anonymous"

    referrerpolicy=

    "no-referrer"

    /

    >
  9. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"

    integrity=

    "sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi"

    crossorigin=

    "anonymous"

    >
  10. <script

    src

    =

    "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/js/all.min.js"

    integrity=

    "sha512-naukR7I+Nk6gp7p5TMA4ycgfxaZBJ7MO5iC3Fp6ySQyKFHOGfpkSZkYVWV5R7u7cfAicxanwYQ5D1e17EfJcMA=="

    crossorigin=

    "anonymous"

    referrerpolicy=

    "no-referrer"

    ></

    script

    >
  11. <script

    src

    =

    "https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.8.3/angular.js"

    integrity=

    "sha512-klc+qN5PPscoGxSzFpetVsCr9sryi2e2vHwZKq43FdFyhSAa7vAqog/Ifl8tzg/8mBZiG2MAKhyjH5oPJp65EA=="

    crossorigin=

    "anonymous"

    referrerpolicy=

    "no-referrer"

    ></

    script

    >
  12. <script

    src

    =

    "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"

    integrity=

    "sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3"

    crossorigin=

    "anonymous"

    ></

    script

    >
  13. <script

    src

    =

    "bootstrap5-custom-pagination.js"

    ></

    script

    >
  14. <style

    >
  15. html, body{
  16. min-height:100%;
  17. width:100%;
  18. }
  19. tbody:empty:after{
  20. content:'No records found'
  21. }
  22. </

    style

    >
  23. </

    head

    >
  24. <body

    >
  25. <nav class

    =

    "navbar navbar-expand-lg navbar-dark bg-primary bg-gradient"

    >
  26. <div

    class

    =

    "container"

    >
  27. <a

    class

    =

    "navbar-brand"

    href

    =

    "./"

    >

    Angular JS Table Pagination</

    a

    >
  28. <div

    >
  29. <a

    href

    =

    "https://sourcecodester.com"

    class

    =

    "text-light fw-bolder h6 text-decoration-none"

    target

    =

    "_blank"

    >

    SourceCodester</

    a

    >
  30. </

    div

    >
  31. </

    div

    >
  32. </

    nav>
  33. <div

    class

    =

    "container-fluid px-5 my-3"

    ng-app=

    "SampleApp"

    ng-controller=

    "tblController"

    >
  34. <div

    class

    =

    "col-lg-8 col-md-10 col-sm-12 mx-auto"

    >
  35. <div

    class

    =

    "card rounded-0 shadow mb-3"

    >
  36. <div

    class

    =

    "card-header"

    >
  37. <div

    class

    =

    "card-title"

    ><b

    >

    Employee List</

    b

    ></

    div

    >
  38. </

    div

    >
  39. <div

    class

    =

    "card-body"

    >
  40. <div

    class

    =

    "container-fluid"

    >
  41. <div

    class

    =

    "alert alert-danger mb-3 rounded-0"

    ng-show=

    "error"

    >

    {{error_msg}}</

    div

    >
  42. <div

    class

    =

    "table-responsive"

    >
  43. <table

    class

    =

    "table table-bordered table-striped"

    >
  44. <thead

    >
  45. <tr

    class

    =

    "bg-primary bg-gradient text-light"

    >
  46. <th

    class

    =

    "text-center"

    >

    Employee</

    th

    >
  47. <th

    class

    =

    "text-center"

    >

    Contact #</

    th

    >
  48. <th

    class

    =

    "text-center"

    >

    Address</

    th

    >
  49. </

    tr

    >
  50. </

    thead

    >
  51. <tbody

    >
  52. <tr

    ng-repeat=

    "data in Employees"

    >
  53. <td

    >

    {{data.name}}</

    td

    >
  54. <td

    >

    {{data.phone}}</

    td

    >
  55. <td

    >

    {{data.address}}</

    td

    >
  56. </

    tr

    >
  57. </

    tbody

    >
  58. </

    table

    >
  59. </

    div

    >

  60. <div

    class

    =

    "bootstrap5-custom-pagination"
  61. data-max-length=

    "5"
  62. data-number-of-pages=

    "{{NumberOfPages}}"
  63. data-current-page=

    "{{currentPage}}"
  64. data-btn-callback=

    "paginate"
  65. >
  66. </

    div

    >
  67. </

    div

    >
  68. </

    div

    >
  69. </

    div

    >
  70. </

    div

    >
  71. </

    div

    >
  72. <script

    src

    =

    "app.js"

    ></

    script

    >
  73. </

    body

    >
  74. </

    html

    >

Retrieving the Data

Next, we'll create a PHP File that contains the script for fetching the sample data from the database. The script requires POST data of "limit" and currentPage which will be needed for fetching only the data of the table's current page. Save the snippet below as getEmployees.php.

  1. <?php
  2. require_once

    (

    'db-connect.php'

    )

    ;
  3. $limit

    =

    isset

    (

    $_POST

    [

    'limit'

    ]

    )

    ? $_POST

    [

    'limit'

    ]

    :

    5

    ;
  4. $currentPage

    =

    $offset

    =

    isset

    (

    $_POST

    [

    'currentPage'

    ]

    )

    ? $_POST

    [

    'currentPage'

    ]

    :

    1

    ;
  5. $offset

    =

    $currentPage

    >

    1

    ? ceil

    (

    (

    $currentPage

    *

    $limit

    )

    -

    $limit

    )

    :

    0

    ;
  6. $sqlTotal

    =

    "SELECT id FROM `employee_list`"

    ;
  7. $sql

    =

    "SELECT * FROM `employee_list` order by `name` asc LIMIT {$limit}

    OFFSET {$offset}

    "

    ;
  8. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  9. $queryTotal

    =

    $conn

    ->

    query

    (

    $sqlTotal

    )

    ;
  10. $data

    [

    'employees'

    ]

    =

    $query

    ->

    fetch_all

    (

    MYSQLI_ASSOC)

    ;
  11. $data

    [

    'total'

    ]

    =

    $queryTotal

    ->

    num_rows

    ;

  12. echo

    json_encode

    (

    $data

    )

    ;
  13. $conn

    ->

    close

    (

    )

    ;

Pagination Buttons

Next, we'll create a custom JavaScript class that contains the script for generating the pagination buttons including the previous and next buttons. The script generates a button that uses Bootstrap 5 Framework's design. In my case, I named this file bootstrap5-custom-pagination.js, and is loaded at the index.php file.

  1. class

    bs5cp_paginations_class {
  2. paginate(

    )

    {
  3. // Get Pagination Node Elements
  4. this

    .bs5cp_paginations_els

    =

    document.querySelectorAll

    (

    '.bootstrap5-custom-pagination'

    )

  5. // Generate Pagination Buttons
  6. this

    .bs5cp_paginations_els

    .forEach

    (

    function

    (

    element,

    index)

    {
  7. element.innerHTML

    =

    ""

    ;
  8. var

    options =

    {
  9. maxLength:

    parseInt(

    element.getAttribute

    (

    'data-max-length'

    )

    )

    ||

    5

    ,
  10. currentPage:

    parseInt(

    element.getAttribute

    (

    'data-current-page'

    )

    )

    ||

    1

    ,
  11. totalBtns:

    parseInt(

    element.getAttribute

    (

    'data-number-of-pages'

    )

    )

    ||

    1

    ,
  12. maxButtons:

    parseInt(

    element.getAttribute

    (

    'data-buttons-max'

    )

    )

    ||

    5

    ,
  13. btn_callback:

    element.getAttribute

    (

    'data-btn-callback'

    )

    ||

    null

    ,
  14. previousText:

    element.getAttribute

    (

    'data-previous-text'

    )

    ||

    "prev"

    ,
  15. nextText:

    element.getAttribute

    (

    'data-next-text'

    )

    ||

    "next"
  16. }

  17. // Button Group Parent
  18. var

    btn_group =

    document.createElement

    (

    'div'

    )
  19. btn_group.setAttribute

    (

    'role'

    ,

    'group'

    )

    ;
  20. btn_group.classList

    .add

    (

    'btn-group'

    )

    ;


  21. //Previous Button
  22. var

    prev_btn =

    document.createElement

    (

    'button'

    )
  23. prev_btn.setAttribute

    (

    'type'

    ,

    'button'

    )

    ;
  24. prev_btn.classList

    .add

    (

    'btn'

    ,

    'btn-sm'

    ,

    'rounded-0'

    ,

    'border'

    )

    ;
  25. var

    prev_page =

    parseInt(

    options.currentPage

    )

    -

    1

    ;
  26. prev_btn.setAttribute

    (

    'ng-click'

    ,

    `${

    options.btn_callback

    }

    (

    ${

    prev_page}

    )

    `)

    ;
  27. if

    (

    options.currentPage

    ==

    1

    )
  28. prev_btn.setAttribute

    (

    'disabled'

    ,

    true

    )
  29. prev_btn.innerHTML

    =

    options.previousText
  30. element.appendChild

    (

    prev_btn)


  31. //First Page Button
  32. var

    first_btn =

    document.createElement

    (

    'button'

    )
  33. first_btn.classList

    .add

    (

    'btn'

    ,

    'btn-sm'

    ,

    'rounded-0'

    ,

    'border'

    )

    ;
  34. first_btn.setAttribute

    (

    'type'

    ,

    'button'

    )

    ;
  35. first_btn.setAttribute

    (

    'ng-click'

    ,

    `${

    options.btn_callback

    }

    (

    1

    )

    `)

    ;
  36. first_btn.innerHTML

    =

    1
  37. if

    (

    options.currentPage

    ==

    1

    )
  38. first_btn.classList

    .add

    (

    'active'

    ,

    'btn-primary'

    )

    ;
  39. element.appendChild

    (

    first_btn)

  40. //Ellipsis Button
  41. var

    ellipsis_btn =

    document.createElement

    (

    'button'

    )
  42. ellipsis_btn.classList

    .add

    (

    'btn'

    ,

    'btn-sm'

    ,

    'rounded-0'

    ,

    'border'

    )

    ;
  43. ellipsis_btn.setAttribute

    (

    'type'

    ,

    'button'

    )

    ;
  44. ellipsis_btn.setAttribute

    (

    'disabled'

    ,

    true

    )

    ;
  45. ellipsis_btn.innerHTML

    =

    '...'


  46. //Dynamic Page Buttons
  47. var

    dynamic_btn =

    document.createElement

    (

    'button'

    )
  48. dynamic_btn.classList

    .add

    (

    'btn'

    ,

    'btn-sm'

    ,

    'rounded-0'

    ,

    'border'

    )

    ;
  49. dynamic_btn.setAttribute

    (

    'type'

    ,

    'button'

    )

    ;
  50. if

    (

    options.currentPage

    >

    3

    &&

    options.totalBtns

    >

    5

    )
  51. element.appendChild

    (

    ellipsis_btn.cloneNode

    (

    true

    )

    )
  52. if

    (

    options.totalBtns

    >

    5

    )

    {
  53. var

    iteratePage =

    parseInt(

    options.currentPage

    >

    2

    ?

    options.currentPage

    :

    3

    )

    ;
  54. for

    (

    var

    $i =

    iteratePage;

    $i <

    (

    iteratePage +

    3

    )

    ;

    $i++

    )

    {
  55. var

    new_page_btn =

    dynamic_btn.cloneNode

    (

    true

    )
  56. var

    num =

    $i -

    1

    ;
  57. if

    (

    (

    iteratePage +

    1

    )

    ==

    options.totalBtns

    )
  58. num =

    $i -

    2

    ;
  59. if

    (

    iteratePage ==

    options.totalBtns

    )
  60. num =

    $i -

    3

    ;
  61. new_page_btn.setAttribute

    (

    'ng-click'

    ,

    `${

    options.btn_callback

    }

    (

    ${

    num}

    )

    `)

    ;
  62. new_page_btn.innerHTML

    =

    num
  63. if

    (

    options.currentPage

    ==

    num)
  64. new_page_btn.classList

    .add

    (

    'active'

    ,

    'btn-primary'

    )

    ;
  65. element.appendChild

    (

    new_page_btn)
  66. }
  67. }

    else

    {
  68. for

    (

    var

    $i =

    2

    ;

    $i <

    5

    ;

    $i++

    )

    {
  69. var

    new_page_btn =

    dynamic_btn.cloneNode

    (

    true

    )
  70. new_page_btn.setAttribute

    (

    'ng-click'

    ,

    `${

    options.btn_callback

    }

    (

    ${

    $i}

    )

    `)

    ;
  71. new_page_btn.innerHTML

    =

    $i
  72. if

    (

    options.currentPage

    ==

    $i)
  73. new_page_btn.classList

    .add

    (

    'active'

    ,

    'btn-primary'

    )

    ;
  74. element.appendChild

    (

    new_page_btn)
  75. }
  76. }
  77. if

    (

    options.totalBtns

    >

    5

    &&

    (

    (

    options.totalBtns

    -

    2

    )

    >

    options.currentPage

    )

    )

    {
  78. element.appendChild

    (

    ellipsis_btn.cloneNode

    (

    true

    )

    )
  79. }


  80. //Last Page Button
  81. if

    (

    options.totalBtns

    >

    1

    )

    {
  82. var

    last_btn =

    document.createElement

    (

    'button'

    )
  83. last_btn.classList

    .add

    (

    'btn'

    ,

    'btn-sm'

    ,

    'rounded-0'

    ,

    'border'

    )

    ;
  84. last_btn.setAttribute

    (

    'type'

    ,

    'button'

    )

    ;
  85. last_btn.setAttribute

    (

    'ng-click'

    ,

    `${

    options.btn_callback

    }

    (

    ${

    options.totalBtns

    }

    )

    `)

    ;
  86. last_btn.innerHTML

    =

    options.totalBtns
  87. if

    (

    options.currentPage

    ==

    options.totalBtns

    )
  88. last_btn.classList

    .add

    (

    'active'

    ,

    'btn-primary'

    )

    ;
  89. element.appendChild

    (

    last_btn)
  90. }

  91. //Next Page Button

  92. var

    next_btn =

    document.createElement

    (

    'button'

    )
  93. next_btn.setAttribute

    (

    'type'

    ,

    'button'

    )

    ;
  94. next_btn.classList

    .add

    (

    'btn'

    ,

    'btn-sm'

    ,

    'rounded-0'

    ,

    'border'

    )

    ;
  95. var

    next_page =

    parseInt(

    options.currentPage

    )

    +

    1

    ;
  96. next_btn.setAttribute

    (

    'ng-click'

    ,

    `${

    options.btn_callback

    }

    (

    ${

    next_page}

    )

    `)

    ;
  97. if

    (

    options.currentPage

    ==

    options.totalBtns

    )
  98. next_btn.setAttribute

    (

    'disabled'

    ,

    true

    )
  99. next_btn.innerHTML

    =

    options.nextText
  100. element.appendChild

    (

    next_btn)

  101. }

    )
  102. }
  103. }
  104. let bs5cp_paginations =

    new

    bs5cp_paginations_class(

    )

    ;


AngularJS

Lastly, we'll create a JavaScript file that initiates the angular module of the application. Save the snippet below as app.js.

  1. var

    mymodule =

    angular.module

    (

    "SampleApp"

    ,

    [

    ]

    )

    ;

  2. /**
  3. * Angular Controller
  4. */

  5. var

    Employee_list =

    mymodule.controller

    (

    "tblController"

    ,

    function

    (

    $compile,

    $scope,

    $http)

    {
  6. $scope.NumberOfPages

    =

    0

    ;
  7. $scope.currentPage

    =

    1

    ;
  8. $scope.paginationMaxBtn

    =

    5

    ;
  9. $scope.maxLength

    =

    5

    ;
  10. $scope.Employees

    =

    {

    }
  11. $scope.error

    =

    false
  12. $scope.error_msg

    =

    ''
  13. /*
  14. * Get Employees
  15. */

  16. $scope.getEmployees

    =

    function

    (

    )

    {
  17. var

    fdata =

    new

    FormData(

    )

    ;
  18. fdata.append

    (

    'currentPage'

    ,

    $scope.currentPage

    )
  19. fdata.append

    (

    'limit'

    ,

    $scope.maxLength

    )
  20. $http.post

    (

    'getEmployees.php'

    ,

    new

    URLSearchParams(

    fdata)

    .toString

    (

    )

    ,

    {
  21. headers:

    {
  22. 'Content-Type'

    :

    'application/x-www-form-urlencoded; charset=utf-8'
  23. }
  24. }

    )
  25. .then

    (
  26. function

    (

    response)

    {
  27. if

    (

    response.status

    ==

    200

    )

    {
  28. var

    data =

    response.data
  29. if

    (

    !!

    data.employees

    &&

    data.total

    )

    {
  30. $scope.Employees

    =

    data.employees

    ;
  31. $scope.NumberOfPages

    =

    Math

    .ceil

    (

    parseInt(

    data.total

    )

    /

    $scope.maxLength

    )

    ;

  32. // Initiate Pagination
  33. bs5cp_paginations.paginate

    (

    )

    ;
  34. document.querySelectorAll

    (

    '.bootstrap5-custom-pagination'

    )

    .forEach

    (

    function

    (

    element)

    {
  35. element.childNodes

    .forEach

    (

    function

    (

    child_element)

    {
  36. $compile(

    child_element)

    (

    $scope)
  37. }

    )
  38. }

    )

  39. }

    else

    if

    (

    !!

    data.error

    )

    {
  40. $scope.error_msg

    =

    data.error
  41. }

    else

    {
  42. console.error

    (

    response)
  43. $scope.error_msg

    =

    "Fetching Employee List failed due to some reasons."
  44. }
  45. }

    else

    {
  46. $scope.error_msg

    =

    "Fetching Employee List failed due to some reasons."
  47. console.error

    (

    response)
  48. }
  49. }

    ,
  50. function

    (

    error)

    {
  51. $scope.error_msg

    =

    "Fetching Employee List failed due to some reasons."
  52. console.error

    (

    error)
  53. }
  54. )
  55. }

  56. /**
  57. * Trigger Pagination
  58. * @param {int} page
  59. */
  60. $scope.paginate

    =

    function

    (

    page)

    {
  61. $scope.currentPage

    =

    page;
  62. }

  63. /**
  64. * Listen for Current Page Changes
  65. */
  66. $scope.$watch(

    'currentPage'

    ,

    function

    (

    )

    {
  67. $scope.getEmployees

    (

    )
  68. }

    )
  69. $scope.getEmployees

    (

    )

  70. }

    )


Application Snapshot

Here is the following snapshot of the result of the snippets of the sample web application that demonstrates the Server-Side Table Pagination.

That's it! You can now test the sample application that we created on your end. I have provided also the complete source code zip file that I created for this tutorial. You can download it by clicking the Download Button below this article.

That's the end of this tutorial. I hope this Server-Side Table Pagination Tutorial helps you with what you are looking for and that you'll find it useful for your current and future web application projects.

Happy Coding :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top