jqGrid Data - export

jqGrid v4.6

by 1004lucifer

HTML

<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqgrid/4.6.0/css/ui.jqgrid.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqgrid/4.6.0/js/i18n/grid.locale-kr.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqgrid/4.6.0/js/jquery.jqGrid.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.css">
<div style="height:450px">
  <table id="list"><tr><td></td></tr></table> 
  <div id="pager"></div> 
  <button id="export">Export</button><br/>
  <h4>export result</h4>
  <textarea id="result" style="width:100%;height:150px;border:1px black solid"></textarea>
</div>

JavaScript

var serverJsonData = 
{ 
  'totalpages' : '10', 
  'currpage' : '1',
  'totalrecords' : '15',
  'invdata' : [
    {'invid' : '1','invdate':'cell11', 'amount' :'cell12', 'tax' :'cell13', 'total' :'1234', 'note' :'somenote'},
    {'invid' : '2','invdate':'cell21', 'amount' :'cell22', 'tax' :'cell23', 'total' :'2345', 'note' :'some note'},
    {'invid' : '3','invdate':'cell21', 'amount' :'cell22', 'tax' :'cell23', 'total' :'2345', 'note' :'some note'},
    {'invid' : '4','invdate':'cell21', 'amount' :'cell22', 'tax' :'cell23', 'total' :'2345', 'note' :'some note'},
  ]
};

$("#list").jqGrid({
  url: '/echo/json/', // use JSFiddle echo service
  datatype: 'json',
  mtype: 'POST',
  postData: { json: JSON.stringify(serverJsonData) },
  colModel :[ 
    {name:'invid', index:'invid', width:55}, 
    {name:'invdate', index:'invdate', width:90}, 
    {name:'amount', index:'amount', width:80, align:'right'}, 
    {name:'tax', index:'tax', width:80, align:'right'}, 
    {name:'total', index:'total', width:80, align:'right'}, 
    {name:'note', index:'note', width:150, sortable:false} 
  ],
  jsonReader : {
    root:"invdata",
    page: "currpage",
    total: "totalpages",
    records: "totalrecords",
    repeatitems: false,
    id: "0"
  },
  caption : 'JSON Data',
  height: 'auto',
  autowidth: true,
  rowNum: 5,
  pager: '#pager'
});
$("#export").on("click", function(){
  var exportResult = $("#list").jqGrid('jqGridExport', {
    exptype: 'jsonstring'
  });
  $('#result').html(exportResult)
})