Export hide column to excel kendo grid












0















I have a problem. I have a kendo grid who work perfectly. And excel export too. But I have some column who is hidden and I want to display them in excel export. So i found this code in internet who work pefretcly to display my hidden column:



if (!exportFlag) {
for(var i=0; i < columns.length; i++) {
if(columns[i].hidden)
e.sender.showColumn(i);
}
e.preventDefault();
exportFlag = true;
setTimeout(function () {
e.sender.saveAsExcel();
});
} else {
for(var i=0; i < columns.length; i++) {
if(columns[i].hidden)
e.sender.hideColumn(i);
}
exportFlag = false;
}


But this solution doesn't work I have this error: TypeError: n[0] is undefined



in this line: e.sender.hideColumn(k);



This is my grid:



 // Configuration des KendoGrids
var sort = {
field: 'nom',
dir: 'asc'
};

var titleButtonAdd = "Ajouter un nouveau pef";

var columns = [
{field: "nom", title: "Nom PEF ", hidden: false,width: 200, headerAttributes: {style: "text-align: center; "}},
{field: "codeug", title: "UG PEF ", hidden: false,width: 200, headerAttributes: {style: "text-align: center; "}},
{field: "type", title: "Type ", hidden: false,width: 0,headerAttributes: { style: "text-align: center;display:none; " }, attributes: { style:"text-align: center; display:none;"} },
{
field: "responsable.libelle",
title: "Responsable ",hidden: false,
width: 200,
headerAttributes: {style: "text-align: center; "},
template: " #= (responsable === null) ? '' : responsable.libelle #"
},
{field: "ordresurartere", title: "Ordre/art ", hidden: false,width: 0,headerAttributes: { style: "text-align: center;display:none; " }, attributes: { style:"text-align: center; display:none;"} },
{field: "codemire", title: "Code mire ", hidden: false,width: 0,headerAttributes: { style: "text-align: center;display:none; " }, attributes: { style:"text-align: center; display:none;"} },
{field: "telephone", title: "tel ", hidden: false,width: 0,headerAttributes: { style: "text-align: center;display:none; " }, attributes: { style:"text-align: center; display:none;"} },
{
field: "paupef",
title: "Permanent ",hidden: false,
width: 40,
headerAttributes: {style: "text-align: center; "},
template: "<input type='checkbox' #= (true === paupef) ? checked='checked' : '' # disabled />"
},
{
field: "mainteneurprincipal",
title: "Mainteneur Principal ",
width: 200,hidden: false,
headerAttributes: {style: "text-align: center; "}
},
{field: "badge", title: "Badge ", hidden: false,width: 0, headerAttributes: { style: "text-align: center;display:none; " }, attributes: { style:"text-align: center; display:none;"} },
{field: "cheminement", title: "Cheminement ", hidden: true },
{
title: "", width: 30,
template: "<a href='#=url_edit#' title='Modifier' class='icon-1 info-tooltip'></a><a href='#=url_delete#' title='Supprimer' onClick='return window.confirm("Voulez-vous vraiment supprimer ce pef ?") ' class='icon-2 info-tooltip'></a>",
},
];
var xlsFileName = "Alimentations.xlsx";
var addRoute = "{{ path('referentiel_pef_add') }}";
var listData={{ listData|raw }};
var dataSource = new kendo.data.DataSource({
data: listData,
sort: sort
});
var $grid = $("#grid");
var grid =$grid.data("kendoGrid");
if (typeof grid !== "undefined") {
grid.destroy();
}
var toolbar ;
if (typeof titleButtonAdd !== "undefined") {
toolbar = [{
text:titleButtonAdd ,
className: "k-grid-custom",
imageClass: "k-icon k-add",
iconClass: "k-icon k-i-add"
}, "excel"];
}

if (typeof templateButtonAdd !== "undefined") {
toolbar = [{
className: "k-grid-custom",
imageClass: "k-icon k-add",
template: templateButtonAdd,
}, "excel"];
}

$grid.kendoGrid({
toolbar: toolbar,
excel: {
fileName: xlsFileName
},
columns:columns,
dataSource: dataSource,
height: 550,
scrollable: true,
sortable: true,
excelExport: excelExport,
dataBound: function () {
DisplayNoResultsFound($('#grid'));
},
});
dataSource.read();
$(".k-grid-custom").click(function () {
location.href = addRoute;
});


var exportFlag = false;
function excelExport(e) {

if (!exportFlag) {
for(var i=0; i < columns.length; i++) {
if(columns[i].hidden)
e.sender.showColumn(i);
}
e.preventDefault();
exportFlag = true;
setTimeout(function () {
e.sender.saveAsExcel();
});
} else {
for (var k = 0; k < columns.length; k++) { console.log(k);
if (columns[k].hidden)
e.sender.hideColumn(k);
}
exportFlag = false;
}


}


Someone can help me please ?? Or have an idea ?










share|improve this question





























    0















    I have a problem. I have a kendo grid who work perfectly. And excel export too. But I have some column who is hidden and I want to display them in excel export. So i found this code in internet who work pefretcly to display my hidden column:



    if (!exportFlag) {
    for(var i=0; i < columns.length; i++) {
    if(columns[i].hidden)
    e.sender.showColumn(i);
    }
    e.preventDefault();
    exportFlag = true;
    setTimeout(function () {
    e.sender.saveAsExcel();
    });
    } else {
    for(var i=0; i < columns.length; i++) {
    if(columns[i].hidden)
    e.sender.hideColumn(i);
    }
    exportFlag = false;
    }


    But this solution doesn't work I have this error: TypeError: n[0] is undefined



    in this line: e.sender.hideColumn(k);



    This is my grid:



     // Configuration des KendoGrids
    var sort = {
    field: 'nom',
    dir: 'asc'
    };

    var titleButtonAdd = "Ajouter un nouveau pef";

    var columns = [
    {field: "nom", title: "Nom PEF ", hidden: false,width: 200, headerAttributes: {style: "text-align: center; "}},
    {field: "codeug", title: "UG PEF ", hidden: false,width: 200, headerAttributes: {style: "text-align: center; "}},
    {field: "type", title: "Type ", hidden: false,width: 0,headerAttributes: { style: "text-align: center;display:none; " }, attributes: { style:"text-align: center; display:none;"} },
    {
    field: "responsable.libelle",
    title: "Responsable ",hidden: false,
    width: 200,
    headerAttributes: {style: "text-align: center; "},
    template: " #= (responsable === null) ? '' : responsable.libelle #"
    },
    {field: "ordresurartere", title: "Ordre/art ", hidden: false,width: 0,headerAttributes: { style: "text-align: center;display:none; " }, attributes: { style:"text-align: center; display:none;"} },
    {field: "codemire", title: "Code mire ", hidden: false,width: 0,headerAttributes: { style: "text-align: center;display:none; " }, attributes: { style:"text-align: center; display:none;"} },
    {field: "telephone", title: "tel ", hidden: false,width: 0,headerAttributes: { style: "text-align: center;display:none; " }, attributes: { style:"text-align: center; display:none;"} },
    {
    field: "paupef",
    title: "Permanent ",hidden: false,
    width: 40,
    headerAttributes: {style: "text-align: center; "},
    template: "<input type='checkbox' #= (true === paupef) ? checked='checked' : '' # disabled />"
    },
    {
    field: "mainteneurprincipal",
    title: "Mainteneur Principal ",
    width: 200,hidden: false,
    headerAttributes: {style: "text-align: center; "}
    },
    {field: "badge", title: "Badge ", hidden: false,width: 0, headerAttributes: { style: "text-align: center;display:none; " }, attributes: { style:"text-align: center; display:none;"} },
    {field: "cheminement", title: "Cheminement ", hidden: true },
    {
    title: "", width: 30,
    template: "<a href='#=url_edit#' title='Modifier' class='icon-1 info-tooltip'></a><a href='#=url_delete#' title='Supprimer' onClick='return window.confirm("Voulez-vous vraiment supprimer ce pef ?") ' class='icon-2 info-tooltip'></a>",
    },
    ];
    var xlsFileName = "Alimentations.xlsx";
    var addRoute = "{{ path('referentiel_pef_add') }}";
    var listData={{ listData|raw }};
    var dataSource = new kendo.data.DataSource({
    data: listData,
    sort: sort
    });
    var $grid = $("#grid");
    var grid =$grid.data("kendoGrid");
    if (typeof grid !== "undefined") {
    grid.destroy();
    }
    var toolbar ;
    if (typeof titleButtonAdd !== "undefined") {
    toolbar = [{
    text:titleButtonAdd ,
    className: "k-grid-custom",
    imageClass: "k-icon k-add",
    iconClass: "k-icon k-i-add"
    }, "excel"];
    }

    if (typeof templateButtonAdd !== "undefined") {
    toolbar = [{
    className: "k-grid-custom",
    imageClass: "k-icon k-add",
    template: templateButtonAdd,
    }, "excel"];
    }

    $grid.kendoGrid({
    toolbar: toolbar,
    excel: {
    fileName: xlsFileName
    },
    columns:columns,
    dataSource: dataSource,
    height: 550,
    scrollable: true,
    sortable: true,
    excelExport: excelExport,
    dataBound: function () {
    DisplayNoResultsFound($('#grid'));
    },
    });
    dataSource.read();
    $(".k-grid-custom").click(function () {
    location.href = addRoute;
    });


    var exportFlag = false;
    function excelExport(e) {

    if (!exportFlag) {
    for(var i=0; i < columns.length; i++) {
    if(columns[i].hidden)
    e.sender.showColumn(i);
    }
    e.preventDefault();
    exportFlag = true;
    setTimeout(function () {
    e.sender.saveAsExcel();
    });
    } else {
    for (var k = 0; k < columns.length; k++) { console.log(k);
    if (columns[k].hidden)
    e.sender.hideColumn(k);
    }
    exportFlag = false;
    }


    }


    Someone can help me please ?? Or have an idea ?










    share|improve this question



























      0












      0








      0








      I have a problem. I have a kendo grid who work perfectly. And excel export too. But I have some column who is hidden and I want to display them in excel export. So i found this code in internet who work pefretcly to display my hidden column:



      if (!exportFlag) {
      for(var i=0; i < columns.length; i++) {
      if(columns[i].hidden)
      e.sender.showColumn(i);
      }
      e.preventDefault();
      exportFlag = true;
      setTimeout(function () {
      e.sender.saveAsExcel();
      });
      } else {
      for(var i=0; i < columns.length; i++) {
      if(columns[i].hidden)
      e.sender.hideColumn(i);
      }
      exportFlag = false;
      }


      But this solution doesn't work I have this error: TypeError: n[0] is undefined



      in this line: e.sender.hideColumn(k);



      This is my grid:



       // Configuration des KendoGrids
      var sort = {
      field: 'nom',
      dir: 'asc'
      };

      var titleButtonAdd = "Ajouter un nouveau pef";

      var columns = [
      {field: "nom", title: "Nom PEF ", hidden: false,width: 200, headerAttributes: {style: "text-align: center; "}},
      {field: "codeug", title: "UG PEF ", hidden: false,width: 200, headerAttributes: {style: "text-align: center; "}},
      {field: "type", title: "Type ", hidden: false,width: 0,headerAttributes: { style: "text-align: center;display:none; " }, attributes: { style:"text-align: center; display:none;"} },
      {
      field: "responsable.libelle",
      title: "Responsable ",hidden: false,
      width: 200,
      headerAttributes: {style: "text-align: center; "},
      template: " #= (responsable === null) ? '' : responsable.libelle #"
      },
      {field: "ordresurartere", title: "Ordre/art ", hidden: false,width: 0,headerAttributes: { style: "text-align: center;display:none; " }, attributes: { style:"text-align: center; display:none;"} },
      {field: "codemire", title: "Code mire ", hidden: false,width: 0,headerAttributes: { style: "text-align: center;display:none; " }, attributes: { style:"text-align: center; display:none;"} },
      {field: "telephone", title: "tel ", hidden: false,width: 0,headerAttributes: { style: "text-align: center;display:none; " }, attributes: { style:"text-align: center; display:none;"} },
      {
      field: "paupef",
      title: "Permanent ",hidden: false,
      width: 40,
      headerAttributes: {style: "text-align: center; "},
      template: "<input type='checkbox' #= (true === paupef) ? checked='checked' : '' # disabled />"
      },
      {
      field: "mainteneurprincipal",
      title: "Mainteneur Principal ",
      width: 200,hidden: false,
      headerAttributes: {style: "text-align: center; "}
      },
      {field: "badge", title: "Badge ", hidden: false,width: 0, headerAttributes: { style: "text-align: center;display:none; " }, attributes: { style:"text-align: center; display:none;"} },
      {field: "cheminement", title: "Cheminement ", hidden: true },
      {
      title: "", width: 30,
      template: "<a href='#=url_edit#' title='Modifier' class='icon-1 info-tooltip'></a><a href='#=url_delete#' title='Supprimer' onClick='return window.confirm("Voulez-vous vraiment supprimer ce pef ?") ' class='icon-2 info-tooltip'></a>",
      },
      ];
      var xlsFileName = "Alimentations.xlsx";
      var addRoute = "{{ path('referentiel_pef_add') }}";
      var listData={{ listData|raw }};
      var dataSource = new kendo.data.DataSource({
      data: listData,
      sort: sort
      });
      var $grid = $("#grid");
      var grid =$grid.data("kendoGrid");
      if (typeof grid !== "undefined") {
      grid.destroy();
      }
      var toolbar ;
      if (typeof titleButtonAdd !== "undefined") {
      toolbar = [{
      text:titleButtonAdd ,
      className: "k-grid-custom",
      imageClass: "k-icon k-add",
      iconClass: "k-icon k-i-add"
      }, "excel"];
      }

      if (typeof templateButtonAdd !== "undefined") {
      toolbar = [{
      className: "k-grid-custom",
      imageClass: "k-icon k-add",
      template: templateButtonAdd,
      }, "excel"];
      }

      $grid.kendoGrid({
      toolbar: toolbar,
      excel: {
      fileName: xlsFileName
      },
      columns:columns,
      dataSource: dataSource,
      height: 550,
      scrollable: true,
      sortable: true,
      excelExport: excelExport,
      dataBound: function () {
      DisplayNoResultsFound($('#grid'));
      },
      });
      dataSource.read();
      $(".k-grid-custom").click(function () {
      location.href = addRoute;
      });


      var exportFlag = false;
      function excelExport(e) {

      if (!exportFlag) {
      for(var i=0; i < columns.length; i++) {
      if(columns[i].hidden)
      e.sender.showColumn(i);
      }
      e.preventDefault();
      exportFlag = true;
      setTimeout(function () {
      e.sender.saveAsExcel();
      });
      } else {
      for (var k = 0; k < columns.length; k++) { console.log(k);
      if (columns[k].hidden)
      e.sender.hideColumn(k);
      }
      exportFlag = false;
      }


      }


      Someone can help me please ?? Or have an idea ?










      share|improve this question
















      I have a problem. I have a kendo grid who work perfectly. And excel export too. But I have some column who is hidden and I want to display them in excel export. So i found this code in internet who work pefretcly to display my hidden column:



      if (!exportFlag) {
      for(var i=0; i < columns.length; i++) {
      if(columns[i].hidden)
      e.sender.showColumn(i);
      }
      e.preventDefault();
      exportFlag = true;
      setTimeout(function () {
      e.sender.saveAsExcel();
      });
      } else {
      for(var i=0; i < columns.length; i++) {
      if(columns[i].hidden)
      e.sender.hideColumn(i);
      }
      exportFlag = false;
      }


      But this solution doesn't work I have this error: TypeError: n[0] is undefined



      in this line: e.sender.hideColumn(k);



      This is my grid:



       // Configuration des KendoGrids
      var sort = {
      field: 'nom',
      dir: 'asc'
      };

      var titleButtonAdd = "Ajouter un nouveau pef";

      var columns = [
      {field: "nom", title: "Nom PEF ", hidden: false,width: 200, headerAttributes: {style: "text-align: center; "}},
      {field: "codeug", title: "UG PEF ", hidden: false,width: 200, headerAttributes: {style: "text-align: center; "}},
      {field: "type", title: "Type ", hidden: false,width: 0,headerAttributes: { style: "text-align: center;display:none; " }, attributes: { style:"text-align: center; display:none;"} },
      {
      field: "responsable.libelle",
      title: "Responsable ",hidden: false,
      width: 200,
      headerAttributes: {style: "text-align: center; "},
      template: " #= (responsable === null) ? '' : responsable.libelle #"
      },
      {field: "ordresurartere", title: "Ordre/art ", hidden: false,width: 0,headerAttributes: { style: "text-align: center;display:none; " }, attributes: { style:"text-align: center; display:none;"} },
      {field: "codemire", title: "Code mire ", hidden: false,width: 0,headerAttributes: { style: "text-align: center;display:none; " }, attributes: { style:"text-align: center; display:none;"} },
      {field: "telephone", title: "tel ", hidden: false,width: 0,headerAttributes: { style: "text-align: center;display:none; " }, attributes: { style:"text-align: center; display:none;"} },
      {
      field: "paupef",
      title: "Permanent ",hidden: false,
      width: 40,
      headerAttributes: {style: "text-align: center; "},
      template: "<input type='checkbox' #= (true === paupef) ? checked='checked' : '' # disabled />"
      },
      {
      field: "mainteneurprincipal",
      title: "Mainteneur Principal ",
      width: 200,hidden: false,
      headerAttributes: {style: "text-align: center; "}
      },
      {field: "badge", title: "Badge ", hidden: false,width: 0, headerAttributes: { style: "text-align: center;display:none; " }, attributes: { style:"text-align: center; display:none;"} },
      {field: "cheminement", title: "Cheminement ", hidden: true },
      {
      title: "", width: 30,
      template: "<a href='#=url_edit#' title='Modifier' class='icon-1 info-tooltip'></a><a href='#=url_delete#' title='Supprimer' onClick='return window.confirm("Voulez-vous vraiment supprimer ce pef ?") ' class='icon-2 info-tooltip'></a>",
      },
      ];
      var xlsFileName = "Alimentations.xlsx";
      var addRoute = "{{ path('referentiel_pef_add') }}";
      var listData={{ listData|raw }};
      var dataSource = new kendo.data.DataSource({
      data: listData,
      sort: sort
      });
      var $grid = $("#grid");
      var grid =$grid.data("kendoGrid");
      if (typeof grid !== "undefined") {
      grid.destroy();
      }
      var toolbar ;
      if (typeof titleButtonAdd !== "undefined") {
      toolbar = [{
      text:titleButtonAdd ,
      className: "k-grid-custom",
      imageClass: "k-icon k-add",
      iconClass: "k-icon k-i-add"
      }, "excel"];
      }

      if (typeof templateButtonAdd !== "undefined") {
      toolbar = [{
      className: "k-grid-custom",
      imageClass: "k-icon k-add",
      template: templateButtonAdd,
      }, "excel"];
      }

      $grid.kendoGrid({
      toolbar: toolbar,
      excel: {
      fileName: xlsFileName
      },
      columns:columns,
      dataSource: dataSource,
      height: 550,
      scrollable: true,
      sortable: true,
      excelExport: excelExport,
      dataBound: function () {
      DisplayNoResultsFound($('#grid'));
      },
      });
      dataSource.read();
      $(".k-grid-custom").click(function () {
      location.href = addRoute;
      });


      var exportFlag = false;
      function excelExport(e) {

      if (!exportFlag) {
      for(var i=0; i < columns.length; i++) {
      if(columns[i].hidden)
      e.sender.showColumn(i);
      }
      e.preventDefault();
      exportFlag = true;
      setTimeout(function () {
      e.sender.saveAsExcel();
      });
      } else {
      for (var k = 0; k < columns.length; k++) { console.log(k);
      if (columns[k].hidden)
      e.sender.hideColumn(k);
      }
      exportFlag = false;
      }


      }


      Someone can help me please ?? Or have an idea ?







      javascript kendo-grid export-to-excel






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 15:27







      Arendelle

















      asked Nov 23 '18 at 15:02









      ArendelleArendelle

      376




      376
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448974%2fexport-hide-column-to-excel-kendo-grid%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448974%2fexport-hide-column-to-excel-kendo-grid%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen