Is it possible to implement pagination for loading JSON data in chunks in jsGrid?

Currently, I am utilizing jsgrid and facing an issue with loading a JSON file containing 5000 registries into a grid page by page. My goal is to display only 50 registries per page without loading all 5000 at once. Even though I have implemented paging in my grid, it still reads the entire JSON file each time. Below is the code snippet from my controller:

controller: {
loadData: function(filter) {
  var def = $.Deferred();
  $.ajax({
    url: "http://www.json-generator.com/api/json/get/clGvbnRZmG?indent=2", //5000
    //url: "http://www.json-generator.com/api/json/get/cpERCWvHzC?indent=2", //5
    dataType: "json",
    data: filter
  }).done(function(response) {
    var startIndex = (filter.pageIndex - 1) * filter.pageSize;
    var filteredArray = response;

    // Filter criteria
    if (filter.name !== "") {
      filteredArray= $.grep(filteredArray, function(item) {
        return item.name.includes(filter.name);
      });
    } if (filter.age !== undefined) {
      filteredArray= $.grep(filteredArray, function(item) {
        return item.age === filter.age;
      });
    } if (filter.email !== "") {
      filteredArray= $.grep(filteredArray, function(item) {
        return item.email.includes(filter.email);
      });
    } if (filter.gender !== "") {
      filteredArray= $.grep(filteredArray, function(item) {
        return item.gender === filter.gender;
      });
    }

    // Sorting logic
    if (filter.hasOwnProperty("sortField")) {
      if (filter.sortOrder === "asc") filteredArray.sort(ascPredicateBy(filter.sortField));
      else filteredArray.sort(descPredicateBy(filter.sortField));
    }
    
    var dataToDisplay = {
      data: filteredArray.slice(startIndex, startIndex + filter.pageSize),
      itemsCount: filteredArray.length
    };
    def.resolve(dataToDisplay);
  });

  return def.promise();
}

I have utilized slice method to extract a portion of the object array for display on the page.

I am unsure if this limitation is specific to jsgrid or AJAX itself. It seems retrieving only a part of the JSON using AJAX may not be feasible.

Answer №1

jsGrid is optimized for handling paging functionality, allowing you to remove a chunk of unnecessary code from the promise!

In order to enable paging in jsGrid, you need to specify the following configuration:

paging: true,
pageLoading: true,
pageSize: 50,

When implementing your loadData controller, it will receive the following properties within the filter parameter:

  • pageSize - indicates the number of records to display per page.
  • pageIndex - represents the particular page out of a total of 5,000 records. This value is determined by jsGrid when the user interacts with the pagination controls.

You must create a web service that utilizes these parameters to fetch and return the correct page of data. For instance, the URL structure could resemble the following example:

url: "/api/json/get/clGvbnRZmG/" + filter.pageSize + "/" + filter.pageIndex

The data returned should adhere to this format:

{
  data: [ { ..first item ...}, { ..second item..}, ...],
  itemsCount: n 
}

Here, itemsCount corresponds to the total number of records, which in this case is 5000.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

retrieve object from s3 using angular and open it as a pdf

I'm attempting to access a file from an s3 bucket using Angular and display it as a PDF. I have a node service set up to retrieve the object, which I then call from Angular. However, when I try to open the PDF in Angular, it appears as a blank (white) ...

Can Next.js 13 components maximize performance by utilizing server and client components simultaneously? What is the best approach to achieve this?

Introduction Currently, I am exploring server and client components in Next.js 13 and looking to incorporate them into my project. One of the key features is a container component that utilizes react-intersection-observer to track which section is visible ...

When utilizing the POST method to call a PHP file, it consistently returns a success response regardless of the nested conditions within the outer if-else statement

I've also included some code... When a nested else statement inside an if condition is executed... the ajax success function functions properly... but when the else condition is false, it should trigger the failure function //AJAX Script $(document) ...

MUI Error: Incorrect prop provided to menu item

I am encountering an issue with a React component that generates a list of elements, each containing a button to open a menu. The problem is that the props being passed to each menu are incorrect; they always reflect the props of the last element in the ...

Tips for invoking a function using ng-model together with the ng-model value

Within a div element, I have a text field where I am using ng-model to capture the value. I need to trigger a function for validation when a date is entered in the text field. How can I achieve this while still utilizing ng-model? Any suggestions on how to ...

Convert a number to binary in JavaScript, but display the result as infinity

data = parseInt(num); bin =0; pow=1; var rem=0 ; while(data != 0){ rem = data % 2; data = data / 2; bin = rem * pow + bin; pow = pow *10; } document.write(bin); I encountered an issue with my JavaScript code. Even though the example should output 11011 ...

What are some ways to conceal methods within a class so that they are not accessible outside of the constructor

I am a newcomer to classes and I have written the following code: class BoardTypeResponse { created_on: string; name: string; threads: string[]; updated_on: string; _id: string; delete_password: string; loading: BoardLoadingType; error: Bo ...

JavaScript and Angular are used to define class level variables

Hello, I'm currently diving into Angular and have encountered an issue with a class level variable called moratoriumID in my component. I have a method that makes a POST request and assigns the returned number to moratoriumID. Everything seems to work ...

Emails sent through an HTML form submission should always include a valid "from"

I am currently in the process of creating a feedback form that allows individuals to provide anonymous feedback. I have explored nodemailer and node-sendmail, but I have encountered an issue with both requiring a from address. While I am aware that this ca ...

Retrieve data with a web API

I am currently developing a web API to fetch data from a mock database using express My goal is to retrieve a JSON list containing all portfolios and their corresponding positions from the database module. Is there a way to structure the returned data so ...

When using Wordpress AJAX, the Jquery component successfully sends the data, however the corresponding PHP function fails to execute

Trying to implement a jQuery function that triggers an AJAX call when a user clicks on a specific element. The goal is to extract the data value from this element, pass it to a PHP function, and execute the function using the received variable. While the j ...

No data is being returned by the Jquery Ajax function

I am experiencing an issue with a Jquery Ajax call in my code: $('body').on('click', '#btnPopulate', function() { alert(getTree()); }); function getTree() { var url = getUrlPath() + "/StoryboardAdmin/BuildStoryboardViewMode ...

Tips for showing HTML content in an Angular UI grid

I've been attempting to showcase HTML within the grid by checking out this resource: Add html link in anyone of ng-grid However, my attempts led me to this code snippet: var app = angular.module('myApp', ['ngGrid']); ...

What steps can I take to condense and tidy up this output into a more compact string?

Recently, I've been experimenting with Google's APIs and attempting to create a terminal command that can inform me of the distance and travel time to a particular location. However, I'm running into an issue with the current output format: ...

What is the best way to refresh a PHP function based on a JavaScript event?

I have a website where a div tag shows all the values from a SQL database using PHP. I want to reload this div tag with a JavaScript event, such as clicking on an HTML button. Is it possible to bring back all the values from the SQL database and display th ...

What could be the reason why this function in jQuery is not functioning properly?

I am trying to duplicate the span element inside the `test` element using the .clone method. It seems like it works as expected. However, when I try to use .html in a `.click` function, it doesn't work. Can someone point out where I went wrong and sug ...

What other ways can websockets be utilized besides comet?

Websockets offer a more efficient solution for comet (reverse Ajax, often achieved through long-polling). However, are there other ways we can utilize websockets? For instance: - Can websockets be used to facilitate communication between different bro ...

unexpected alteration of text sizing in mathjax within reveal.js presentations

Something strange is happening with the font size in my slides. The code for each slide is the same, but there is an unexpected change between the 3rd and 4th slide. I cannot figure out what is causing this discrepancy. Oddly enough, when I remove the tit ...

What is the best method to retrieve checked checkbox values and the selected dropdown value using AngularJS?

My code is set up to capture checked checkbox values and selected dropdown value when the submit button is pressed. It successfully retrieves the checked checkbox values, but I'm having trouble getting the selected offer from the dropdown. For example ...

Having difficulties understanding JSON and JSONP

I'm completely new to working with JSON and I am really struggling with making a cross-domain request. It's getting frustrating for me :( This is the code I currently have: $.getJSON('http://api.steampowered.com/IEconDOTA2_570/GetHeroes/v0 ...