Search code examples
jqgridfree-jqgrid

Why "allData" is an empty array?


I am trying to get the data from a jqGrid (free version and latest version) and it's suppose that I get:

  • all the data if none is selected
  • the selected data if any

This is how I am doing it:

$(function () {
    var $order_logs = $('#order_logs');
    $order_logs.jqGrid({
        url: Routing.generate('api_order_logs'),
        datatype: "json",
        colModel: $colmodel.data('values'),
        width: 980,
        height: 300,
        pager: true,
        toppager: true,
        hoverrows: true,
        shrinkToFit: true,
        autowidth: true,
        rownumbers: true,
        viewrecords: true,
        rowList: [25, 50, 100],
        rownumWidth: 60,
        gridview: true,
        sortable: {
            options: {
                items: ">th:not(:has(#jqgh_order_logs_cb,#jqgh_order_logs_rn,#jqgh_order_logs_actions),:hidden)"
            }
        },
        jsonReader: {
            root: 'rows',
            page: 'page',
            total: 'total',
            records: 'records',
            cell: '',
            repeatitems: false
        },
        cmTemplate: {autoResizable: true, editable: true},
        autoResizing: {compact: true, resetWidthOrg: true},
        autoresizeOnLoad: true
    }).jqGrid('navGrid', {
        edit: false,
        add: false,
        del: false,
        search: false,
        refresh: true,
        refreshstate: "current",
        cloneToTop: true
    }).jqGrid('navButtonAdd', {
        caption: 'Export',
        title: 'Export',
        onClickButton: function () {
            var filteredData = $order_logs.jqGrid("getGridParam").lastSelectedData,
                allData = $order_logs.jqGrid('getGridParam', 'data');

            exportData(filteredData, allData);
        }
    });
});

function exportData(filteredData, allData) {
    if (filteredData.length === 0 || allData.length === 0) {
        alert('There is no data to export');
        return;
    }

    // Export only the filtered data
    if (filteredData.length > 0) {
        return;
    }

    // Export all the grid data
}

For some reason the value of allData is always an empty array and I am not sure since I am using the same code as everyone is using out there and found in a lot of answer here in SO.

UPDATE:

Currently the grid is holding six columns and a set of 60 records as total paginated by 20 each time however you can change the pagination to be 50 or 100.

Can any tell me why is this?


Solution

  • I'd recommend to use loadonce: true, forceClientSorting: true options in case of small dataset: less at 1000 or 10000 rows. It simplifies the code server side and you can use full functionality of free jqGrid. The problem with access to lastSelectedData and data will be solved.

    More then that, you can easy use many advanced features like createColumnIndex: true, generateValue: true of generateDatalist: true options and so on. See demos included in README of version 4.14.1. Good and comfortable filtering of the data is, in my option, the part of displaying the data. Having the data locally allows to find unique values and build <select> in the filter bar or to use <datalist> to have autocomplete functionality.