2

I want to create a download button in my EXT JS panel which when click download/exports EXTJS grid into downloadable CSV file. PS: I think we can use store data or the JSON from which i am filling data in grid store to populate data into CSV file I have tried Ext.ux.CSVExporter but i wasn't able to use it successfully. My current code is :

       <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title id='title'>HTML Page setup Tutorial</title>       
        <link rel="stylesheet" type="text/css" href="ext-all.css" />       
        <script type="text/javascript" src="ext-all.js"></script>
        <script type="text/javascript" src="FileSaver.js"></script>
        <script type="text/javascript" src="Formatter.js"></script>
        <script type="text/javascript" src="CSVFormatter/CSVFormatter.js"></script>
         <script type="text/javascript" src="ExcelFormatter/Cell.js"></script>
        <script type="text/javascript" src="ExcelFormatter/Style.js"></script>
         <script type="text/javascript" src="ExcelFormatter/Workbook.js"></script>
          <script type="text/javascript" src="ExcelFormatter/Worksheet.js"></script>
<script type="text/javascript" src="ExcelFormatter/ExcelFormatter.js"></script>  
        <script type="text/javascript" src="Exporter.js"></script>
        <script type="text/javascript" src="ExporterButton.js"></script>



<script type="text/javascript">
Ext.define('Ext.ux.ProgressBarPager', {

    requires: ['Ext.ProgressBar'],
    /**
     * @cfg {Number} width
     * <p>The default progress bar width.  Default is 225.</p>
    */
    width   : 225,
    /**
     * @cfg {String} defaultText
    * <p>The text to display while the store is loading.  Default is 'Loading...'</p>
     */
    defaultText    : 'Loading...',
    /**
     * @cfg {Object} defaultAnimCfg
     * <p>A {@link Ext.fx.Anim Ext.fx.Anim} configuration object.</p>
     */
    defaultAnimCfg : {
        duration: 1000,
        easing: 'bounceOut' 
    },  

    /**
     * Creates new ProgressBarPager.
     * @param {Object} config Configuration options
     */
    constructor : function(config) {
        if (config) {
            Ext.apply(this, config);
        }
    },
    //public
    init : function (parent) {
        var displayItem;
        if (parent.displayInfo) {
            this.parent = parent;

            displayItem = parent.child("#displayItem");
            if (displayItem) {
                parent.remove(displayItem, true);
            }

            this.progressBar = Ext.create('Ext.ProgressBar', {
                text    : this.defaultText,
                width   : this.width,
                animate : this.defaultAnimCfg,
                style: {
                    cursor: 'pointer'
                },
                listeners: {
                    el: {
                        scope: this,
                        click: this.handleProgressBarClick
                    }
                }
            });

            parent.displayItem = this.progressBar;

            parent.add(parent.displayItem);
            Ext.apply(parent, this.parentOverrides);
        }
    },
    // private
    // This method handles the click for the progress bar
    handleProgressBarClick : function(e){
        var parent = this.parent,
            displayItem = parent.displayItem,
            box = this.progressBar.getBox(),
            xy = e.getXY(),
            position = xy[0]- box.x,
            pages = Math.ceil(parent.store.getTotalCount() / parent.pageSize),
            newPage = Math.max(Math.ceil(position / (displayItem.width / pages)), 1);

        parent.store.loadPage(newPage);
    },

    // private, overriddes
    parentOverrides  : {
        // private
        // This method updates the information via the progress bar.
        updateInfo : function(){
            if(this.displayItem){
                var count = this.store.getCount(),
                    pageData = this.getPageData(),
                    message = count === 0 ?
                    this.emptyMsg :
                    Ext.String.format(
                        this.displayMsg,
                        pageData.fromRecord, pageData.toRecord, this.store.getTotalCount()
                    ),
                    percentage = pageData.pageCount > 0 ? (pageData.currentPage / pageData.pageCount) : 0;

                this.displayItem.updateProgress(percentage, message, this.animate || this.defaultAnimConfig);
            }
        }
    }
});


Ext.onReady(function() {

    var field = [];
    var columnList = [];
    var counter = {
        "levels":

            [{
            "name": "class",
            "samples": [{
                "name": "1660SH_3",
                "features": [{
                    "count": 8,
                    "name": "Bacteroidia"
                }, {
                    "count": 9,
                    "name": "Bacteroidiaa"
                },
                {
                    "count": 10,
                    "name": "Bacteroidiab"
                },
                {
                    "count": 11,
                    "name": "Bacteroidiac"
                }]
            }, {
                "name": "1660SH_4",
                "features": [{
                    "count": 5,
                    "name": "Bacteroidia"
                }, {
                    "count": 6,
                    "name": "Bacteroidiaa"
                },
                 {
                    "count": 7,
                    "name": "Bacteroidiab"
                },
                {
                    "count": 8,
                    "name": "Bacteroidiac"
                }]
            }]
        }, ]
    };
    columnList.push({
        header: "Sample v/s Feature",
        dataIndex: "Sample v/s Feature",
        width: 0.1 * Ext.getBody().getViewSize().width,
        columnLines: true,
        locked: true
    });
    field.push("Sample v/s Feature");
    for (var p = 0; p < Object.keys(counter.levels[0].samples).length; p++) {

        columnList.push({
            header: counter.levels[0].samples[p].name,
            dataIndex: counter.levels[0].samples[p].name,
            flex: 1,
            columnLines: true
        });
        field.push(counter.levels[0].samples[p].name);
    }

    if (counter.levels[0].name == 'class') {
      var mydata=[];
        for (var p = 0; p < Object.keys(counter.levels[0].samples[0].features).length; p++)
         {
            var s = [];
            s["Sample v/s Feature"] = '<b>' + counter.levels[0].samples[0].features[p].name + '</b>';
            for (var j = 0; j < Object.keys(counter.levels[0].samples).length; j++) 
            {
                s[counter.levels[0].samples[j].name] = counter.levels[0].samples[j].features[p].count;
            }
            mydata.push(s);

        }

        var store = Ext.create('Ext.data.ArrayStore', {
                 autoLoad: false,
                 pageSize : 2,

                fields: field,
                data: {
                    count:mydata.length,
                    data:mydata
                },
                proxy:{
                    type:"memory",
                     enablePaging: true,
                    data:mydata,
                    reader: {
                        type: 'json',
                        root: 'data',

                    }
                }
            });
         store.load({
    params: {
        // specify params for the first page load if using paging
        start: 0,          
        limit: 2,

    }
});


        var classTable = Ext.create('Ext.grid.Panel', {
            style: 'border: solid Blue 1px',
            id: 'family',
            renderTo: Ext.getBody(),
            store: store,
             requires: [
        'Ext.ux.exporter.Exporter'
    ],

            columns: columnList,
            columnLines: true,
            width: Ext.getBody().getViewSize().width,
            height: Ext.getBody().getViewSize().height,
           bbar: {
                xtype: 'pagingtoolbar',
                pageSize: 2,
                store: store,
                displayInfo: true,
                plugins: new Ext.ux.ProgressBarPager()
            },
             dockedItems:[
                     {
                xtype: 'toolbar',
                dock: 'top',
                items: [
                    {
                        xtype: 'exporterbutton',
                        text: 'export data'
                    }
                         ]
                     }
                         ]


        });

    }
});




  </script>
  </head>
    <body>

    </body>
</html>
Prateek Ratnaker
  • 817
  • 11
  • 35

1 Answers1

0

repository link use this project

here is example how to use it fiddle link

first of all copy repository files to you ext library's ux folder located in ext/src/ux under exporter folder. full path will be ext/src/ux/exporter/[repository_files] and modify your grid

  var classTable = Ext.create('Ext.grid.Panel', {
    style: 'border: solid Blue 1px',
    id: 'family',
    renderTo: Ext.getBody(),
    store: store,
    requires: [
        'Ext.ux.exporter.Exporter'
    ],
    columns: columnList,
    columnLines: true,
    width: Ext.getBody().getViewSize().width,
    height: Ext.getBody().getViewSize().height,
   bbar: {
        xtype: 'pagingtoolbar',
        pageSize: 2,
        store: store,
        displayInfo: true,
        plugins: new Ext.ux.ProgressBarPager()
    },
    dockedItems:[
    {
                xtype: 'toolbar',
                dock: 'top',
                items: [
                    {
                        xtype: 'exporterbutton',
                        text: 'exrpot data'
                    }
                ]
            }
    ]

});
LightNight
  • 851
  • 1
  • 6
  • 14
  • i have edited my code...i tried what u said....i included all the files u mentioned....but on pressing export button...nothing happens....i get an error message "Uncaught TypeError: col.getXType is not a function"........I am making a standalone html file and using EXTJS 4.2 – Prateek Ratnaker Feb 20 '16 at 18:40
  • it worked for me. can you provide fiddle? no need if it will not work. I just need full your code to track problem – LightNight Feb 20 '16 at 20:47
  • this is the link to fiddle https://fiddle.sencha.com/#fiddle/160l .I haven't included the repository to app directory(I don't know how to do that :( ) please include those repository files..... PS: I think the code for exporter works only in ext js 5.1 and above...it doesn't work for 4.2.....Please find a solution to my problem – Prateek Ratnaker Feb 21 '16 at 04:52
  • https://github.com/iwiznia/Ext.ux.Exporter https://www.sencha.com/forum/showthread.php?136598-Export-store-to-Excel ..............Please check if this can help in getting me the required functionality – Prateek Ratnaker Feb 21 '16 at 09:42
  • http://www.fileconvoy.com/dfl.php?id=g840338132e4c674299979060027cb051aff91c590 ......this is how i am maintaining my directory structure – Prateek Ratnaker Feb 21 '16 at 09:45
  • I have fixed your fiddle, it;s working now. [linki to fiddle](https://fiddle.sencha.com/#fiddle/161g). important changes. add xtype tu your grids column. also modify CSVFormatter.js line 23 and 47, col.getXType() change to col.xtype and download will work – LightNight Feb 21 '16 at 09:57
  • Please check your fiddle again https://fiddle.sencha.com/#fiddle/161g ......it is not working for extjs 4.2 ....even the grid is not being displayed on your fiddle – Prateek Ratnaker Feb 21 '16 at 10:22
  • After making changes that you have mentioned,I edited my files like this .... here is the link to download my files http://www.fileconvoy.com/dfl.php?id=ga7b82f0b6a4d3f0e9997906102589fe7da2771375 ....now it gives the error on chrome console that col.xtype is not a function – Prateek Ratnaker Feb 21 '16 at 10:27
  • you changed col.getXType() to col.xtype(), xtype is not a functino its property. chane it to col.xtype != "rownumberer" – LightNight Feb 21 '16 at 11:01
  • it now it works....but the file that gets downloaded doesn't get downloaded with CSV extension .....so certain issues still exist 1. No extension for file getting downloaded 2.no column header names in the file getting downloaded 3. In case of grid with pagination whole data is not getting downloaded,only data on given page is getting downloaded ......could you fix these issues please? – Prateek Ratnaker Feb 21 '16 at 11:19
  • did you find any success in solving these issues? – Prateek Ratnaker Feb 21 '16 at 15:31
  • 1
    sorry I was busy by job's stuff. today is already too late (here :) ). I will try my best tomorrow to help you – LightNight Feb 21 '16 at 17:02
  • Thanks a lot :) ..... I found one more useful link https://fiddle.sencha.com/#fiddle/17j ...This works in extjs 4.2 ...I tried it but was not able to implement it successfully – Prateek Ratnaker Feb 21 '16 at 17:21
  • issue 1 that was when i downloaded file using google chrome, a file with no extension got downloaded,that issue has been resolved ....Please check if you can work something out for issue 2 and 3 – Prateek Ratnaker Feb 21 '16 at 20:04
  • i used the JSON to export data of grid to CSV....but now the issue is multiple CSV files are to be downloaded into a single ZIP file.....please have a look at http://stackoverflow.com/questions/35579820/downloading-multiple-files-into-a-zip-file-javascript-using-data-uri – Prateek Ratnaker Feb 23 '16 at 14:16