3

I'm a newbie to JQuery.

I'm trying to use the function listed here.

The mya=$("#list").getDataIDs(); // Get All IDs is listing the IDs that are only in the current view. However my grid is paginated one. How can I fetch all the IDs?

Here is my code:

$(document).ready(function () {

    jQuery("#customer_list").jqGrid({
      url:'jq_customer_list',
      datatype: "json",
      colNames:['Col1','Col2','Col3'],
      colModel:[
{name:'Col1',index:'Col1', width:100},
{name:'Col2',index:'Col2', width:80},
{name:'Col3',index:'Col3', width:75},
     ],
     rowNum:20,
     rowList:[5,10,20],
     mtype:"GET",
     rownumber:true,
     rownumWidth:40,
     pager: $("#customer_list_pager"),
     viewrecords: true,
     gridview: true,
     caption:"My Data",
     height: '50%',
     pagination:true

    }).navGrid('#customer_list_pager', { search:true, del: false, add: false, edit: false,searchtext:"Search" },
               {}, // default settings for edit
               {}, // default settings for add
               {}, // delete
               {closeOnEscape: true, multipleSearch: true, 
                     closeAfterSearch: true }, // search options
               {}
             ).navButtonAdd('#customer_list_pager',{
                 caption:"Export to Excel", 
                 buttonicon:"ui-icon-save", 
                 onClickButton: function(){ 
                   exportExcel($(this));
                 }, 
                 position:"last"
             });

    function exportExcel($id){
        alert('excelExport');
          var keys=[], ii=0, rows="";
          var ids=$id.getRowData();  // Get All IDs
          var row=$id.getRowData(ids[0]);     // Get First row to get the labels
          for (var k in row) {
            keys[ii++]=k;    // capture col names
            rows=rows+k+"\t";     // output each Column as tab delimited
          }
          rows=rows+"\n";   // Output header with end of line
          for(i=0;i<ids.length;i++) {
            row=$id.getRowData(ids[i]); // get each row
            for(j=0;j<keys.length;j++) rows=rows+row[keys[j]]+"\t"; // output each Row as tab delimited
            rows=rows+"\n";  // output each row with end of line
          }
          rows=rows+"\n";  // end of line at the end
          var form = "<form name='csvexportform' action='excelExport' method='post'>";
          form = form + "<input type='hidden' name='csvBuffer' value='"+rows+"'>";
          form = form + "</form><script>document.csvexportform.submit();</sc"+"ript>";
          OpenWindow=window.open('', '');
          OpenWindow.document.write(form);
          OpenWindow.document.close();
        }

    $("#customer_list").filterToolbar({autosearch:true });

    });
Community
  • 1
  • 1
jai
  • 21,519
  • 31
  • 89
  • 120

2 Answers2

6

You use datatype: "json" without loadonce:true. So the server is responsible to sorting and pagination. So I would implement the export in CSV or XLSX in the server code only. The jqGrid has no information about the full list of data ids or any other information about the full dataset. What you can do is just set window.location to the new url. The server part of the url will generate the CSV or XLSX return it in the HTTP body and set additional HTTP headers like Content-Type to ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" for XLSX, "application/vnd.ms-excel" for XLS or "text/csv" for CSV) and "content-disposition" to "attachment; filename=youfilname.xslx" (of another file extension). In the case the web browser will save the data in the file with the corresponding name and open the file with respect of the corresponding application (Excel.exe for example).

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • I got lost in this answer. I have a jqgrid with some filters and sort orders set, and I'd like have a button somewhere on my page to send the current active filters to the server along with a excel flag so that my asp.net mvc code (which gets a jqgridrequest and said flag), can know to send back a file. The above looks close to what I want, but I got lost at window.location. – Randy Magruder Feb 20 '17 at 21:54
  • @RandyMagruder: There are many approaches to solve your problem, but the answer depend on what you do, which version of jqGrid you use (can use) and from which forf of jqGrid ([free jqGrid](https://github.com/free-jqgrid/jqGrid), commercial [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334) or an old jqGrid in version <=4.7). If you use **local** filtering then you can get it by `.jqGrid("getGridParam", "postData").filters`. Alternatively you can get the ids of all rows after applying the filter. – Oleg Feb 20 '17 at 22:06
  • I'm using the free one that comes with the Lib.Aspnet.Core.Mvc.Jqgrid.Core Nuget package for Asp.net MVC Core. I'm not using local filtering if by this you mean it's loading all the data and filtering clientside. Every sort, filter and page is sent as a request back to the server which then calls a pagination supporting Web API service to get the data. I'm trying to figure out how the postdata gets processed into the form that it's sent with an http post. Is it just a JSON Body sent back with a POST? – Randy Magruder Feb 21 '17 at 15:01
1

As Oleg said, there is no built-in method to return all of the grid ID's for pages that are not displayed in the grid.

If you really need a list of all ID's in the grid, I suggest creating a custom web method to generate them. Then instead of using the grid, just call this method directly using one of the jQuery AJAX functions such as jQuery.get(). Since on the back-end you are already using a query to generate data for the grid, just tweak that query to return a list of ID's - or the ID's and associated data, if necessary.

But Oleg is also right in that if you are really trying to implement a CSV / Excel output, you are better off creating a server-side method to do this, since that way you can create a better user experience by setting HTTP headers correctly, allowing the browser to open an external application for the file, etc.

Does that help?

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284