2017-04-27 50 views
0

我正在使用免費的jqgrid 4.14,我需要將網格的數據導出爲CSV/Excel。我已經做出了將所有數據導出到CSV的功能。 我正在捕獲來自服務器的數據並使用該數據形成CSV。但這裏的問題是,我將如何從網格獲得過濾數據。是否有任何默認屬性或功能?從jqgrid導出過濾的數據爲CSV

這是我的功能 -

function convertor (gridData, scopes, ShowLabel, fileTitle,grid_header_column_value,id) { 


     scopes.grid_header_columns = scopes.grid_header_column_value[id]; 
     var bg = "<button class='btnfilter' />"; 
     for (var i = 0; i < scopes.grid_hidden_columns[id].length; i++) { 
      if (includes(scopes.grid_header_columns,scopes.grid_hidden_columns[id][i]) == true) { 
       var indexhed = scopes.grid_header_columns.indexOf(scopes.grid_hidden_columns[id][i]); 
       scopes.grid_header_columns.splice(indexhed, 1); 
      } 
     } 
     var arrData = typeof gridData != 'object' ? JSON.parse(gridData) : gridData; 
     var CSV = ''; 
     if (ShowLabel) { 
      var row = ""; 
      var count = 0; 
      if (fileTitle != "") 
      { 
       var htmltext = fileTitle.split(','); 
       for (var jj = 0; jj < htmltext.length; jj++) 
       { 
        var datacont = document.getElementById(htmltext[jj]); 
        if (datacont == undefined) 
         datacont = ''; 
        else 
         datacont = datacont.textContent + '\r\n'; 
        CSV += datacont; 
       } 
      } 
      for (var index in arrData[0]) { 
       var a = scopes.gridextra.indexOf(index); 
       if (a == -1 && scopes.grid_header_columns[count] != undefined) { 
        row += scopes.grid_header_columns[count].replace(bg, "") + ','; 
        count++; 
       } 
      } 
      row = row.slice(0, -1); 
      CSV += row + '\r\n'; 
     } 

     for (var i = 0; i < arrData.length; i++) { 

      var row = ""; 
      for (var index in arrData[i]) { 

       var a = scopes.gridextra.indexOf(index); 

       if (a == -1) { 
        if (isNaN(arrData[i][index]) == false) { 
          if(arrData[i][index]<0) { 
           if(arrData[i][index] == -2) 
            arrValue = '="'+"*"+'"'; 
           else 
            arrValue = '="'+"N/A"+'"'; 
          } 
          else 
           var arrValue = arrData[i][index] == null ? "" : '="' + arrData[i][index] + '"'; 
        } 
        else 
          var arrValue = arrData[i][index] == null ? "" : '="' + arrData[i][index] + '"'; 
         row += arrValue + ','; 
       } 
      } 
      row.slice(0, row.length - 1); 
      CSV += row + '\r\n'; 
     } 
     if (CSV == '') { 
      growl.error("Invalid data"); 
      return; 
     } 
     csvData = CSV;  
    } 

回答

0

如果我正確理解你的問題,那麼你應該使用lastSelectedData參數,而不是data參數。 data參數包含原始數據。 lastSelectedData參數包含過濾和排序的數據(所有頁面)。請參閱我爲the answer創建的演示。

+0

一如既往的感謝:) – shv22

+0

@ shv22:不客氣! – Oleg

+0

如果網格上沒有過濾,則lastSelectedData參數保持爲空? – shv22