How to bind with JSON data in jqGrid?

Member

by charles , in category: JavaScript , a year ago

How to bind with JSON data in jqGrid?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by jerod.sauer , a year ago

@charles 

To bind JSON data to a jqGrid, you will need to follow these steps:

  1. Set the datatype property of the grid to "json". This tells jqGrid that the data will be in JSON format.
  2. Set the url property to the URL from which you want to load the data. This can be a local or a remote URL.
  3. Set the mtype property to "GET" or "POST", depending on the type of HTTP request you want to use to load the data.
  4. Set the colModel property to specify the structure of the data that will be displayed in the grid. The colModel property is an array of objects, where each object represents a column in the grid. Each object should have a name property that matches the name of a field in the JSON data, and a label property that specifies the text to be displayed in the column header.
  5. Set the loadComplete event handler to process the data after it has been loaded. In the event handler, you can use the getRowData method to retrieve the data for each row in the grid.


Here is an example of how you might bind JSON data to a jqGrid:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
$("#grid").jqGrid({
  datatype: "json",
  url: "/data/json",
  mtype: "GET",
  colModel: [
    { name: "id", label: "ID" },
    { name: "name", label: "Name" },
    { name: "age", label: "Age" }
  ],
  loadComplete: function() {
    var data = $(this).jqGrid("getRowData");
    // Process the data as needed
  }
});


This code will bind the JSON data from the URL "/data/json" to the jqGrid, using the structure specified in the colModel property. The loadComplete event handler will be called after the data has been loaded, and the getRowData method can be used to retrieve the data for each row in the grid.


I hope this helps! Let me know if you have any questions.

by audrey.hodkiewicz , 4 months ago

@charles 

This solution assumes that you have already included the necessary JavaScript and CSS files for jqGrid, and have set up a basic HTML structure for the grid.