Sort columns in jqgrid

1

How can I sort my JqGrid according to the NumUnity column

Code:

$("#jqGrid").jqGrid({
                url: URLTemario + id,
                mtype: "GET",
                datatype: "json",
                colModel: [
                    { label: 'Id', name: 'IdReg', key: true, width: 70, hidden: true },
                    { label: 'IdMateria', name: 'IdMateria', width: '60', hidden: true },
                    { label: 'Relacion', name: 'Relacion', width: '60', hidden: true },
                    { label: 'Unidad', name: 'NumUnidad', width: '60' },
                    { label: 'Tema', name: 'Descripcion', width: '300' }
                ],
                sortname: 'IdReg',
                loadonce: true,
                height: 'auto',
                rowNum: 10,
                pager: "#jqGridPager",
                width: '100%',
                caption: "REGISTROS",
                viewrecords: true,
});

In NumUnidad I receive a set of values such as:

NumUnity = 1, NumUnity = 2, NumUnity = 1.1 NumUnity = 1.2 NumUnity = 2.1

Then I want to sort them up in my table by NumUnity (Unit number of a subject or subject)

    
asked by José MN 12.11.2016 в 00:45
source

2 answers

1

Hello good first of all it's true you have to support:

sortname: "NumUnidad",
sortorder: "asc",

These variables are sent automatically by GET , so you can receive it in your query file, assuming you use PHP you receive it in the following way:

$sortname = $_GET['sortname']; 
$sortorder = $_GET['sortorder']; 

With this you can already use them in the part where you make your query:

$sql = "SELECT * FROM Registros ORDER BY $sortname '$sortorder'";

The value that you define in the creation of your Grid is the one that you will take when loading, as soon as the user clicks on another column you can change the value for that column and send the name value of the column clicked as well as ascending or descending ordering.

EYE the name: of your grid must have the same name as the fields in your table.

    
answered by 16.05.2017 в 20:48
0

I do not have much idea of what parameters the table receives but I have investigated a bit and I have come up with this:

sortname: "NumUnidad",
sortorder: "asc",

Where sortname is the field to sort by default.

And also sortorder that will be the way in which asc or desc will be shown in your case I think it will be asc , although in the documentation says it's the default asc .

Then your code would be:

$("#jqGrid").jqGrid({
    url: URLTemario + id,
    mtype: "GET",
    datatype: "json",
    colModel: [
      { label: 'Id', name: 'IdReg', key: true, width: 70, hidden: true },
      { label: 'IdMateria', name: 'IdMateria', width: '60', hidden: true },
      { label: 'Relacion', name: 'Relacion', width: '60', hidden: true },
      { label: 'Unidad', name: 'NumUnidad', width: '60' },
      { label: 'Tema', name: 'Descripcion', width: '300' }
    ],
    sortname: 'NumUnidad',
    sortorder: "asc",
    loadonce: true,
    height: 'auto',
    rowNum: 10,
    pager: "#jqGridPager",
    width: '100%',
    caption: "REGISTROS",
    viewrecords: true,
});
    
answered by 12.11.2016 в 01:14