Lazy loading in koGrid

Below is the code for implementing Lazy loading in koGrid. Here is a working plunker. You can download the code from here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
     
    <title>Lazy loading in koGrid</title>
    <link href="KoGrid.css" rel="stylesheet" type="text/css">
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-debug.js" type="text/javascript"></script>
    <script src="koGrid-2.1.1.debug.js" type="text/javascript"></script>
    <style>
        .gridStyle {
            border: 1px solid rgb(212,212,212);
            width: 600px;
            height: 300px;
        }
    </style>
    <script>
        function pagingVm() {
            var self = this;
            this.myData = ko.observableArray([]);
 
            this.filterOptions = {
                filterText: ko.observable(""),
                useExternalFilter: true
            };
 
            this.pagingOptions = {
                pageSizes: ko.observableArray([10, 20]),
                pageSize: ko.observable(10),
                totalServerItems: ko.observable(0),
                currentPage: ko.observable(1)
            };
 
            this.setPagingData = function (data, page, pageSize) {
                var pagedData = self.myData();
 
                var newData = data.slice((page - 1) * pageSize, page * pageSize);
                for (var i = 0; i < newData.length; i++) {
                    pagedData.push(newData[i]);
                }
                self.myData(pagedData);
                self.pagingOptions.totalServerItems(data.length);
            };
 
            this.getPagedDataAsync = function (pageSize, page, searchText) {
                setTimeout(function () {
                    var data;
                    if (searchText) {
                        var ft = searchText.toLowerCase();
                        data = largeLoad.filter(function (item) {
                            return JSON.stringify(item).toLowerCase().indexOf(ft) != -1;
                        });
                        self.setPagingData(data, page, pageSize);
                    } else {
                        self.setPagingData(largeLoad, page, pageSize);
                    }
                }, 100);
            };
 
            self.filterOptions.filterText.subscribe(function (data) {
                self.getPagedDataAsync(self.pagingOptions.pageSize(), self.pagingOptions.currentPage(), self.filterOptions.filterText());
            });
 
            self.pagingOptions.pageSizes.subscribe(function (data) {
                self.getPagedDataAsync(self.pagingOptions.pageSize(), self.pagingOptions.currentPage(), self.filterOptions.filterText());
            });
            self.pagingOptions.pageSize.subscribe(function (data) {
                self.getPagedDataAsync(self.pagingOptions.pageSize(), self.pagingOptions.currentPage(), self.filterOptions.filterText());
            });
            
            self.pagingOptions.currentPage.subscribe(function (data) {
                self.getPagedDataAsync(self.pagingOptions.pageSize(), self.pagingOptions.currentPage(), self.filterOptions.filterText());
            });
 
            self.getPagedDataAsync(self.pagingOptions.pageSize(), self.pagingOptions.currentPage());
 
            this.gridOptions = {
                data: self.myData,
                columnDefs: [{ field: "name", displayName: "name" },
                            { field: "allowance", displayName: "allowance" },
                            { field: "paid", displayName: "paid" }],
                enablePaging: false,
                pagingOptions: self.pagingOptions,
                filterOptions: self.filterOptions,
                displaySelectionCheckbox: false,
                //footerVisible: false
                headerRowHeight: 40,
                rowHeight: 35,
                canSelectRows: false
            };
        };
        $(document).ready(function () {
            vm = new pagingVm();
            ko.applyBindings(vm);
 
            $('.kgViewport').scroll(function () {
                var div = $(this);
                if (div[0].scrollHeight - div.scrollTop() <= div.height()) {
                    vm.pagingOptions.currentPage(vm.pagingOptions.currentPage() + 1);
                }
            });
 
        });
    </script>
 
 
 
 
 
    <div class="gridStyle" data-bind="koGrid: gridOptions">
</div>
<script>
        var largeLoad = [{ "name": "Moroni", "allowance": 505050505050505050, "paid": true },
        { "name": "Tiancum", "allowance": 53, "paid": false },
        { "name": "Jacob", "allowance": 27, "paid": false },
        { "name": "Nephi", "allowance": 29, "paid": false },
        { "name": "Enos", "allowance": 34, "paid": false },
        { "name": "Ether", "allowance": 42, "paid": false },
        { "name": "Alma", "allowance": 43, "paid": true },
        { "name": "Jared", "allowance": 21, "paid": true },
        { "name": "Moroni2", "allowance": 50, "paid": true },
        { "name": "Tiancum2", "allowance": 53, "paid": false },
        { "name": "Jacob2", "allowance": 27, "paid": false },
        { "name": "Nephi2", "allowance": 29, "paid": false },
        { "name": "Enos2", "allowance": 34, "paid": false },
        { "name": "Ether2", "allowance": 42, "paid": false },
        { "name": "Alma2", "allowance": 43, "paid": true },
        { "name": "Jared2", "allowance": 21, "paid": true },
        { "name": "Moroni3", "allowance": 50, "paid": true },
        { "name": "Tiancum3", "allowance": 53, "paid": false },
        { "name": "Jacob3", "allowance": 27, "paid": false },
        { "name": "Nephi3", "allowance": 29, "paid": false },
        { "name": "Enos3", "allowance": 34, "paid": false },
        { "name": "Ether3", "allowance": 42, "paid": false },
        { "name": "Alma3", "allowance": 43, "paid": true },
        { "name": "Jared3", "allowance": 21, "paid": true },
        { "name": "Moroni4", "allowance": 50, "paid": true },
        { "name": "Tiancum4", "allowance": 53, "paid": false },
        { "name": "Jacob4", "allowance": 27, "paid": false },
        { "name": "Nephi4", "allowance": 29, "paid": false },
        { "name": "Enos4", "allowance": 34, "paid": false },
        { "name": "Ether4", "allowance": 42, "paid": false },
        { "name": "Alma4", "allowance": 43, "paid": true },
        { "name": "Jared4", "allowance": 21, "paid": true },
        { "name": "Moroni5", "allowance": 50, "paid": true },
        { "name": "Tiancum5", "allowance": 53, "paid": false },
        { "name": "Jacob5", "allowance": 27, "paid": false },
        { "name": "Nephi5", "allowance": 29, "paid": false },
        { "name": "Enos5", "allowance": 34, "paid": false },
        { "name": "Ether5", "allowance": 42, "paid": false },
        { "name": "Alma5", "allowance": 43, "paid": true },
        { "name": "Jared5", "allowance": 21, "paid": true }];
    </script>

Here I have modified the example given on koGrid examples page for server side paging, but to get the real advantage of on-demand loading you need to fetch paged and filtered data from the server itself. For that you can take help from my another post on server side paging in koGrid and you might need to update the "getPagedDataAsync" function in above code accordingly.




No comments:

Post a Comment