将隐藏列导出到Excel Kendo网格

时间:2018-11-23 15:02:24

标签: javascript kendo-grid export-to-excel

我有问题。我有一个剑道网格,可以完美工作。并且也擅长导出。但是我有一些隐藏的列,我想在excel导出中显示它们。因此,我在互联网上找到了这段代码,这些代码专门用于显示我的隐藏列:

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;
        }

但是此解决方案不起作用,但我遇到此错误:TypeError:n [0]未定义

在此行中:e.sender.hideColumn(k);

这是我的网格:

 // 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;
    }


}

有人可以帮助我吗?还是有个主意?

0 个答案:

没有答案