1

This is just a section of my JSON Data string as it is quite extensive...

{
    "total": 2,
    "page": 1,
    "records": 15,
    "rows": [
        {
            "id": 2148,
            "cell": {
                "MRN": "840134833",
                "Hospital_Fin": "987141516",
                "First_Name": "YELLOW",
                "Last_Name": "CRAYON",
                "Date_of_birth": "/Date(1253160000000)/"}
         }
      ]}

And this would be how I set up my jqGrid...

    $(document).ready(function () {
        jQuery("#frTable").jqGrid ({
            cmTemplate: { sortable: false },
            caption: '@TempData["POPNAME"]' + ' Population',
            datatype: 'json',
            mtype: 'GET',
            url: '@Url.Action("GetAjaxPagedGridData", "Encounters", new { popId = TempData["POPULATIONID"] })',//'/Encounters/GetAjaxPagedGridData/'+ '',                
            pager: '#pager',
            loadonce: true,
            height: 450,
            gridview: true,
            viewrecords: true,
            rowNum: 15,
            shrinkToFit: false,
            autowidth: true,
            colNames: [
                   'MRN',
                   'Hospital Fin',
                   'First Name',
                   'Last Name',
                   'Date of birth'
            colModel: [
                   { name: 'MRN', width: 125, align: 'left' },
                   { name: 'Hospital_Fin', width: 145, align: 'left' },
                   { name: 'First_Name', width: 115, align: 'left' },
                   { name: 'Last_Name', width: 115, align: 'left' },
                   { name: 'Date_of_birth', width: 145, align: 'left', formatter:'date', formatoptions: {newformat: 'm/d/Y'}}]

Now... I look through JSfiddle and I do get the Json string back in the right format. In fact, That is how I was able to copy and paste to this fine website. So what am I missing? It looks like everything should be about right?

UPDATE Do my col models and the names of my columns in my cell have to match up 100 percent? Maybe that would be someplace to take a look?

SoftwareSavant
  • 9,467
  • 27
  • 121
  • 195

1 Answers1

2

You use wrong format of JSON data. Compare your data with the data expected by jqGrid described in the documentation. To fix JSON data you can change it to for example the following

{
    "total": 2,
    "page": 1,
    "records": 15,
    "rows": [
        {
            "id": 2148,
            "MRN": "840134833",
            "Hospital_Fin": "987141516",
            "First_Name": "YELLOW",
            "Last_Name": "CRAYON",
            "Date_of_birth": "/Date(1253160000000)/"
        }
    ]
}

You need include additionally jsonReader: {repeatitems: false} in the list of jqGrid options.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • does jsonReader is also needed when we create jqgrid with struts2-jqgrid-plugin – arvin_codeHunk Feb 27 '13 at 06:49
  • @arvin_codeHunk: All depends on the format of data which returned from the server. If The data are like in my answer above one have to use `jsonReader: {repeatitems: false}` additionally. – Oleg Feb 27 '13 at 07:13
  • i did this, but nothing really changed, please have a look a this post http://stackoverflow.com/questions/15068668/how-to-load-json-in-jqgrid – arvin_codeHunk Feb 27 '13 at 07:16
  • @arvin_codeHunk: I don't use struts2 myself and I I don't understand what you do in the question. Moreover I find wrong the usage of `addJSONData` in most cases (see one from my first [posts](http://stackoverflow.com/q/2660226/315935) on the stackoverflow). One should add some parameters in `postData` and use reloadGrid instead of `addJSONData`. Probably you will be able to fix your problem by replacing `complete` callback to `success` inside of `filterFunction`, but it's just guessing. – Oleg Feb 27 '13 at 07:27
  • okay skip struts2, guess if there is an already constructed grid and I want to load that grid with some json-data based on an event then how to achieve this, Note : that grid is previously loaded with some data, I want to reload this grid with some new json-data, then what will be your strategies using jqgrid? – arvin_codeHunk Feb 27 '13 at 07:34
  • @arvin_codeHunk: You write comments to the answer which has no relation with your comments. To reload the data in the grid you need just call `trigger("reloadGrid")`. Before it you can set `postData` to send additional parameters to the `url` of just change `url`. So I recommend you to fill the grid always by requests to `url` of the grid. – Oleg Feb 27 '13 at 07:39
  • sorry hijacking for this post,But I indeed in trouble thats why I found you here, will you please check updated section in this post http://stackoverflow.com/questions/15068668/how-to-load-json-in-jqgrid – arvin_codeHunk Feb 27 '13 at 07:45
  • Well, I'll be damned. It worked! I thought that a cell type was needed. – SoftwareSavant Feb 27 '13 at 12:08
  • @DmainEvent: You are not the first person who makes the same error. You are welcome! – Oleg Feb 27 '13 at 12:11