can't add data to jqGrid from php within json format

后端 未结 2 1247
情话喂你
情话喂你 2021-01-29 03:47

Hello StackOverFlow nation . I\'m trying to add information to jqGrid , which is retrieved from MySQL database. I\'ve two files => index.html and data.php (both in the same dire

相关标签:
2条回答
  • 2021-01-29 04:21

    Your response format is wrong. You can go to jqGrid Demos page where you will find a sample for PHP/MySQL after expanding Loading Data and then choosing JSON Data.

    The proper format of data should look like this:

    {
        "total": "1",
        "page": "1",
        "records": "2",
        "rows": [
            { "name": "Robert", "surname": "De Niro", "b_year": "1943", "film": "Once Upon A Time In America" },
            { "name": "Al", "surname": "Pacino", "b_year":"1971", "film": "Scent Of A Woman"}
        ]
    }
    

    Where:

    • total --> total count of pages
    • page --> current page number
    • records --> total count of records
    • rows --> the rows of data

    Also if you want rows to be objects, you need to disable repeatitems in jqGrid jsonReader options:

    $("#jqGrid_tb").jqGrid({
        ...
        jsonReader: { repeatitems: false }
    });
    

    It is also adviced for rows to have unique id for later reference.

    0 讨论(0)
  • 2021-01-29 04:35

    You should include

    jsonReader: {
        repeatitems: false,
        root: function (obj) { return obj; },
        page: function (obj) { return 1; },
        total: function (obj) { return 1; },
        records: function (obj) { return obj.length; }
    }
    

    as additional option of jqGrid is you don't want to change format of input data. Moreover you should specify which values should assign jqGrid as id attribute of the row. You can include additional id property as additional property of every returned item or you can add key: true property to the column (in colModel) which contains unique values. For example if you can guarantied that the values from "name" are already unique then you can include key: true property in definition of "name" column.

    Additionally you can consider to use loadonce: true option of jqGrid. In the case the full data of the grid will be loaded at once and the sorting, paging and searching (filtering) of data will be implemented by jqGrid on the client side without needs to implement some additional code on the server side. You should don't use the option in case of large number of rows (many hundred or many thousand rows) in the grid.

    0 讨论(0)
提交回复
热议问题