How to provide pagination in js knockout

JSON Data (test.json)

[{
    "name": "A Goofy Movie (1995) 720p HDTVRip x264 Eng Subs [Dual Audio] [Hindi DD 2.0 - English DD 2.0] Exclusive By -=!Dr.STAR!=-",
    "progress": 0,
    "size": "1.06 GB",
    "downloaded": "87.98 KB",
    "hash": "8fe65e43464debe1bc0961e4656ea780368d4fba",
    "peer": 0,
    "delete": "delete.php?del=8fe65e43464debe1bc0961e4656ea780368d4fba",
    "speed": "0 Byte",
    "eta": "23:59:59",
    "path": "C:\\xampp\\htdocs\\final\\download\/8fe65e43464debe1bc0961e4656ea780368d4fba"
}, {
    "name": "Logan+2017+HDCAM+XviD+UnKnOwN",
    "progress": 0,
    "size": "0 Byte",
    "downloaded": "0 Byte",
    "hash": "624911f8e4fc172e5ed7970d3bc097198bfd4e76",
    "peer": 0,
    "delete": "delete.php?del=624911f8e4fc172e5ed7970d3bc097198bfd4e76",
    "speed": "0 Byte",
    "eta": "23:59:59",
    "path": "C:\\xampp\\htdocs\\final\\download\/624911f8e4fc172e5ed7970d3bc097198bfd4e76"
}, {
    "name": "Internet Download Manager 6.27 Build 5 Multilingual + Patch",
    "progress": 100,
    "size": "6.97 MB",
    "downloaded": "7.49 MB",
    "hash": "bffe600ae08ba8e55db30dae6acd86979e30ce15",
    "peer": 0,
    "delete": "delete.php?del=bffe600ae08ba8e55db30dae6acd86979e30ce15",
    "speed": "0 Byte",
    "eta": "23:59:59",
    "path": "C:\\xampp\\htdocs\\final\\download\/bffe600ae08ba8e55db30dae6acd86979e30ce15"
}]

KnockoutJS Data

function ExampleViewModel() {
    var self = this;
    self.ExampleData = ko.observableArray([]);
    self.update = function() {
        $.ajax("test.json", {
            success: function(allData) {
                var mappeddata = $.map(allData, function(item) {
                    return new DataItem(item)
                });
                self.ExampleData(mappeddata);
            }
        });
    }
}

function DataItem(data) {
    ko.observable(data.name);
    ko.observable(data.progress);
}
var exampleViewModel = new ExampleViewModel();
ko.applyBindings(exampleViewModel);

How to ensure pagination and display only the first two values?

Working example

+4
source share
2 answers

You can go through self.Databelow

function ExampleViewModel() {
  var self = this;
  self.ExampleData = ko.observableArray([]);
  self.CurrentPage = ko.observable(1);
  self.DataPerPage = ko.observable(2); // You can change from here if you want to show the data other than 2 per page
  self.Data = ko.pureComputed(function(){
   var startIndex = self.CurrentPage() === 1? 0 : (self.CurrentPage() - 1) * self.DataPerPage();
   return self.ExampleData().slice(startIndex, startIndex + self.DataPerPage())
  });
  self.update = function() {
    $.ajax("test.json", {
      success: function(allData) {
        var mappeddata = $.map(allData, function(item) {
          return new DataItem(item)
        });
        self.ExampleData(mappeddata);
      }
    });
  }
}

A simple example:

function ViewModel() {
  var self = this;
  self.ExampleData = ko.observableArray([1, 2, 3, 4, 5, 6, 7, 8, 9]);
  self.CurrentPage = ko.observable(1); // Store the current page of the user
  self.DataPerPage = ko.observable(2); // To identify how many data we want to see per page

  self.Data = ko.pureComputed(function(){
   var startIndex = self.CurrentPage() === 1? 0 : (self.CurrentPage() - 1) * self.DataPerPage();
   return self.ExampleData().slice(startIndex, startIndex + self.DataPerPage ())
  });

  self.Next = function() {
    var totalData = self.ExampleData().length;
    var currentPage = self.CurrentPage();

    // if the length is 0, don't allow next
    // if we're on the last page, don't allow next
    if(totalData >= 1 && currentPage < (Math.ceil(totalData/2))) self.CurrentPage(currentPage + 1);
  };

  self.Prev = function() {
    var currentPage = self.CurrentPage();
    
    // if we're on the first page, don't allow prev
    if(currentPage > 1) self.CurrentPage(currentPage - 1);
  };
}   

$(document).ready(function () {
  var myViewModel = new ViewModel();
  ko.applyBindings(myViewModel);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>

<div>
  <ul data-bind="foreach: Data">
    <li data-bind="text: $data"></li>
  </ul>
  <button data-bind="click: Next">Next</button>
  <button data-bind="click: Prev ">Prev</button>
</div>
Run codeHide result
+1
source

- ( ), reusable-component , , .
, .

, , .

Github

JsFiddle

,

JavaScript

function PagingVM(options) {
    var self = this;

    self.PageSize = ko.observable(options.pageSize);
    self.CurrentPage = ko.observable(1);
    self.TotalCount = ko.observable(options.totalCount);

    self.PageCount = ko.pureComputed(function () {
        return Math.ceil(self.TotalCount() / self.PageSize());
    });

    self.SetCurrentPage = function (page) {
        if (page < self.FirstPage)
            page = self.FirstPage;

        if (page > self.LastPage())
            page = self.LastPage();

        self.CurrentPage(page);
    };

    self.FirstPage = 1;
    self.LastPage = ko.pureComputed(function () {
        return self.PageCount();
    });

    self.NextPage = ko.pureComputed(function () {
        var next = self.CurrentPage() + 1;
        if (next > self.LastPage())
            return null;
        return next;
    });

    self.PreviousPage = ko.pureComputed(function () {
        var previous = self.CurrentPage() - 1;
        if (previous < self.FirstPage)
            return null;
        return previous;
    });

    self.NeedPaging = ko.pureComputed(function () {
        return self.PageCount() > 1;
    });

    self.NextPageActive = ko.pureComputed(function () {
        return self.NextPage() != null;
    });

    self.PreviousPageActive = ko.pureComputed(function () {
        return self.PreviousPage() != null;
    });

    self.LastPageActive = ko.pureComputed(function () {
        return (self.LastPage() != self.CurrentPage());
    });

    self.FirstPageActive = ko.pureComputed(function () {
        return (self.FirstPage != self.CurrentPage());
    });

    // this should be odd number always
    var maxPageCount = 7;

    self.generateAllPages = function () {
        var pages = [];
        for (var i = self.FirstPage; i <= self.LastPage() ; i++)
            pages.push(i);

        return pages;
    };

    self.generateMaxPage = function () {
        var current = self.CurrentPage();
        var pageCount = self.PageCount();
        var first = self.FirstPage;

        var upperLimit = current + parseInt((maxPageCount - 1) / 2);
        var downLimit = current - parseInt((maxPageCount - 1) / 2);

        while (upperLimit > pageCount) {
            upperLimit--;
            if (downLimit > first)
                downLimit--;
        }

        while (downLimit < first) {
            downLimit++;
            if (upperLimit < pageCount)
                upperLimit++;
        }

        var pages = [];
        for (var i = downLimit; i <= upperLimit; i++) {
            pages.push(i);
        }
        return pages;
    };

    self.GetPages = ko.pureComputed(function () {
        self.CurrentPage();
        self.TotalCount();

        if (self.PageCount() <= maxPageCount) {
            return ko.observableArray(self.generateAllPages());
        } else {
            return ko.observableArray(self.generateMaxPage());
        }
    });

    self.Update = function (e) {
        self.TotalCount(e.TotalCount);
        self.PageSize(e.PageSize);
        self.SetCurrentPage(e.CurrentPage);
    };

    self.GoToPage = function (page) {
        if (page >= self.FirstPage && page <= self.LastPage())
            self.SetCurrentPage(page);
    }

    self.GoToFirst = function () {
        self.SetCurrentPage(self.FirstPage);
    };

    self.GoToPrevious = function () {
        var previous = self.PreviousPage();
        if (previous != null)
            self.SetCurrentPage(previous);
    };

    self.GoToNext = function () {
        var next = self.NextPage();
        if (next != null)
            self.SetCurrentPage(next);
    };

    self.GoToLast = function () {
        self.SetCurrentPage(self.LastPage());
    };
}

HTML

<ul data-bind="visible: NeedPaging" class="pagination pagination-sm">
    <li data-bind="css: { disabled: !FirstPageActive() }">
        <a data-bind="click: GoToFirst">First</a>
    </li>
    <li data-bind="css: { disabled: !PreviousPageActive() }">
        <a data-bind="click: GoToPrevious">Previous</a>
    </li>

    <!-- ko foreach: GetPages() -->
    <li data-bind="css: { active: $parent.CurrentPage() === $data }">
        <a data-bind="click: $parent.GoToPage, text: $data"></a>
    </li>
    <!-- /ko -->

    <li data-bind="css: { disabled: !NextPageActive() }">
        <a data-bind="click: GoToNext">Next</a>
    </li>
    <li data-bind="css: { disabled: !LastPageActive() }">
        <a data-bind="click: GoToLast">Last</a>
    </li>
</ul>


  • (, , ), HTML .
    data-bind="visible: NeedPaging".


  • , , last page Next ?
    , , data-bind="css: { disabled: !PreviousPageActive() }"


  • ( active), , .
    data-bind="css: { active: $parent.CurrentPage() === $data }"


  • , .


  • , , 1000 , ? ? . , 3 3 .
    <!-- ko foreach: GetPages() -->
    GetPages, , , ( , ) .
    , maxPageCount
    var maxPageCount = 7;, , 7 (3 SelectedPage 3 ) .

    , , , ? ,
    , 11 pages, maxPageCount = 7 selected page is 10,

    5,6,7,8,9,10(selected page),11

    maxPageCount , 5 1 .


  • CurrentPage, , SetCurrentPage. , , , , , .


  • pureComputed not computed, , . , , ,

1
, bootstrap , , , , .
pagination, pagination-sm, active disabled
.

2
, , .
ViewModel, .

function MainVM() {
    var self = this;

    self.PagingComponent = ko.observable(new Paging({
        pageSize: 10,      // how many items you would show in one page
        totalCount: 100,   // how many ALL the items do you have.
    }));

    self.currentPageSubscription = self.PagingComponent().CurrentPage.subscribe(function (newPage) {
        // here is the code which will be executed when the user change the page.
        // you can handle this in the way you need.
        // for example, in my case, I am requesting the data from the server again by making an ajax request
        // and then updating the component

        var data = /*bring data from server , for example*/
        self.PagingComponent().Update({

            // we need to set this again, why? because we could apply some other search criteria in the bringing data from the server, 
            // so the total count of all the items could change, and this will affect the paging
            TotalCount: data.TotalCount,

            // in most cases we will not change the PageSize after we bring data from the server
            // but the component allow us to do that.
            PageSize: self.PagingComponent().PageSize(),

            // use this statement for now as it is, or you have to made some modifications on the 'Update' function.
            CurrentPage: self.PagingComponent().CurrentPage(),
        });
    });

    self.dispose = function () {
        // you need to dispose the manual created subscription, you have created before.
        self.currentPageSubscription.dispose();
    }
}

, : , html- Model with binding,

<div data-bind="with: PagingComponent()">
    <!-- put the component here -->
</div>

0

Source: https://habr.com/ru/post/1673230/


All Articles