隐藏字段Excel导出在kendo ui

时间:2018-02-27 10:41:04

标签: kendo-ui

要求如何使用kendoui网格优化导出隐藏字段

  

通过使用kendo编辑器我实现了网格,我想在kendo ui网格中使用Excel导出导出隐藏字段在这里我想导出隐藏在这里的Excel中的Id字段

     

我的主要动机是使用excel export

导出隐藏的列
<div id='grid-container'>
    <div id='student-details-grid'
         data-role='grid'
         data-groupable='true'
         data-sortable='true'
         data-toolbar=['excel']
         data-excel="{fileName: 'StudentDetails.xlsx', proxyURL: '/save', filterable: true, allPages: true}"
         data-height='450px'
         data-pageable="[
                             {'refresh':'true' },
                             { 'pageSizes':'true'},
                             {'buttonCount':'5'}
                          ]"
         data-bind='source:gridDataSource'
         data-columns="[{'field':'Id','title':'Id'},{'field':'Name','title':'Name'},{'field':'FatherName','title':'FatherName'},{'field':'Email','title':'Email'},{'field':'Address','title':'Address'},{'field':'ContactNo','title':'ContactNo'}]"
         style="height: 550px">
    </div>
</div>



<script type="text/javascript">
    var viewModel = '';
    $(document).ready(function (e) {
        viewModel = kendo.observable({
            gridDataSource: new kendo.data.DataSource({
                transport: {
                    read: {
                        url: "/Home/GetStudents",
                        dataType: "json"
                    }
                },
                pageSize: 10,
                schema: {
                    model: {
                        id: "Id",
                        fields: {
                            Id: { editable: false, nullable: true },
                            Name: { validation: { required: true } },
                            FatherName: { type: "text", validation: { required: true, min: 1 } },
                            Email: { type: 'email', validation: { min: 0, required: true } },
                            Address: { type: "text", validation: { min: 0, required: true } },
                            ContactNo: { type: 'text', validation: { min: 0, required: true } },
                        }
                    },
                    parse: function (data) {
                        debugger
                        if (!data.success && typeof data.success !== 'undefined') {
                            gridDataSource.read();
                        }
                        if (data.success) {
                            viewModel.gridDataSource.read();
                        }
                        return data;
                    }
                }
            }),

        });
        kendo.bind($("#grid-container"), viewModel);
    });
</script>

1 个答案:

答案 0 :(得分:0)

*全局变量

  

isExport = false;

 data-bind="source: dataSource,events:{excelExport : onExcelExport}"

onExcelExport: function (e) {
            var sheet = e.workbook.sheets[0];
            for (var rowIndex = 1; rowIndex < sheet.rows.length; rowIndex++) {
                var row = sheet.rows[rowIndex];
                for (var cellIndex = 0; cellIndex < row.cells.length; cellIndex++) {
                    if (cellIndex === 6 || cellIndex === 8 || cellIndex === 20)
                        row.cells[cellIndex].format = "HH:mm"
                }
            }
            var gridcolumn = e.sender.columns;
            if (!isExport) {
                $.each(gridcolumn, function (index, value) {
                    if (value.hidden)
                        e.sender.showColumn(index);
                    else if (value.hidden === false)
                        e.sender.hideColumn(index);
                    e.sender.hideColumn(0);

                });
                e.preventDefault();
                isExport = true;
                setTimeout(function () {
                    e.sender.saveAsExcel();
                });
            } else {
                $.each(gridcolumn, function (index, value) {
                    if (value.hidden === false)
                        e.sender.hideColumn(index);
                    else
                        e.sender.showColumn(index);
                });
                isExport = false;
            }
        },