jQWidgets Forums

jQuery UI Widgets Forums Grid Export data to excel from a hidden datagrid

This topic contains 2 replies, has 2 voices, and was last updated by  adison1989 11 years, 2 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
  • Export data to excel from a hidden datagrid #49989

    adison1989
    Participant

    Hi Peter,

    I have used jqxgrid in my code and transferred my data in it binding it to JSON and also used the export to excel button to download that grid data. Now, the requirement is that I want this grid to be hidden(it should not be visible on webpage). I am doing this by altering the display property. When I try to do so, the excel does not downloads, but if I keep the grid visible on the webpage then it gets download. Is there a way through which we can download the data from a hidden jqxGrid??Please help. Thanks in advance

    Export data to excel from a hidden datagrid #49991

    Peter Stoev
    Keymaster

    Hi adison1989,

    If the Grid is hidden when you call the jqxGrid constructor, the Grid will not be initialized until the DIV tag is hidden so you would not be able to export its data in that case, because the data would not be loaded. You can create the Grid, then hide the Grid if necessary. If you do so, you will be able to export its data.

    Example:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title id='Description'>With jqxGrid, you can export your data to Excel, XML, CSV, TSV, JSON and HTML.</title>
        <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" />
        <script type="text/javascript" src="../../scripts/jquery-1.10.2.min.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxcore.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxbuttons.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxscrollbar.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxmenu.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxcheckbox.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxgrid.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxgrid.selection.js"></script> 
        <script type="text/javascript" src="../../jqwidgets/jqxgrid.columnsresize.js"></script> 
        <script type="text/javascript" src="../../jqwidgets/jqxdata.js"></script> 
        <script type="text/javascript" src="../../jqwidgets/jqxdata.export.js"></script> 
        <script type="text/javascript" src="../../jqwidgets/jqxgrid.export.js"></script> 
        <script type="text/javascript" src="../../jqwidgets/jqxgrid.sort.js"></script> 
        <script type="text/javascript" src="../../scripts/demos.js"></script>
        <script type="text/javascript" src="generatedata.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                // prepare the data
                var data = generatedata(100);
    
                var source =
                {
                    localdata: data,
                    datatype: "array",
                    datafields:
                    [
                        { name: 'firstname', type: 'string' },
                        { name: 'lastname', type: 'string' },
                        { name: 'productname', type: 'string' },
                        { name: 'available', type: 'bool' },
                        { name: 'date', type: 'date' },
                        { name: 'quantity', type: 'number' },
                        { name: 'price', type: 'number' }
                    ]                     
                };
    
                var dataAdapter = new $.jqx.dataAdapter(source);
    
                // initialize jqxGrid
                $("#jqxgrid").jqxGrid(
                {
                    width: 670,
                    source: dataAdapter,                
                    altrows: true,
                    sortable: true,
                    ready: function()
                    {
                        $("#jqxgrid").hide();
                    },
                    selectionmode: 'multiplecellsextended',
                    columns: [
                      { text: 'First Name', datafield: 'firstname', width: 90 },
                      { text: 'Last Name', datafield: 'lastname', width: 90 },
                      { text: 'Product', datafield: 'productname', width: 177 },
                      { text: 'Available', datafield: 'available', columntype: 'checkbox', width: 67, cellsalign: 'center', align: 'center' },
                      { text: 'Ship Date', datafield: 'date', width: 90, align: 'right', cellsalign: 'right', cellsformat: 'd' },
                      { text: 'Quantity', datafield: 'quantity', width: 70, align: 'right', cellsalign: 'right' },
                      { text: 'Price', datafield: 'price', width: 65, cellsalign: 'right', align: 'right', cellsformat: 'c2' }
                    ]
                });
    
                $("#excelExport").jqxButton({ theme: theme });
                $("#xmlExport").jqxButton({ theme: theme });
                $("#csvExport").jqxButton({ theme: theme });
                $("#tsvExport").jqxButton({ theme: theme });
                $("#htmlExport").jqxButton({ theme: theme });
                $("#jsonExport").jqxButton({ theme: theme });
    
                $("#excelExport").click(function () {
                    $("#jqxgrid").jqxGrid('exportdata', 'xls', 'jqxGrid');           
                });
                $("#xmlExport").click(function () {
                    $("#jqxgrid").jqxGrid('exportdata', 'xml', 'jqxGrid');
                });
                $("#csvExport").click(function () {
                    $("#jqxgrid").jqxGrid('exportdata', 'csv', 'jqxGrid');
                });
                $("#tsvExport").click(function () {
                    $("#jqxgrid").jqxGrid('exportdata', 'tsv', 'jqxGrid');
                });
                $("#htmlExport").click(function () {
                    $("#jqxgrid").jqxGrid('exportdata', 'html', 'jqxGrid');
                });
                $("#jsonExport").click(function () {
                    $("#jqxgrid").jqxGrid('exportdata', 'json', 'jqxGrid');
                });
            });
        </script>
    </head>
    <body class='default'>
        <div id='jqxWidget' style="font-size: 13px; font-family: Verdana; float: left;">
            <div id="jqxgrid"></div>
            <div style='margin-top: 20px;'>
                <div style='float: left;'>
                    <input type="button" value="Export to Excel" id='excelExport' />
                    <br /><br />
                    <input type="button" value="Export to XML" id='xmlExport' />
                </div>
                <div style='margin-left: 10px; float: left;'>
                    <input type="button" value="Export to CSV" id='csvExport' />
                    <br /><br />
                    <input type="button" value="Export to TSV" id='tsvExport' />
                </div>
                <div style='margin-left: 10px; float: left;'>
                    <input type="button" value="Export to HTML" id='htmlExport' />
                    <br /><br />
                    <input type="button" value="Export to JSON" id='jsonExport' />
                </div>
            </div>
        </div>
    </body>
    </html>
    

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com


    adison1989
    Participant

    Thank You very much for the reply Peter. It was very helpful.

    Thanks and Regards,
    adison1989

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.