Author Topic: How to use pivot mode with xlsx import.  (Read 5983 times)

Hidehiro Ishii

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 44
    • View Profile
How to use pivot mode with xlsx import.
« on: March 19, 2018, 02:30:52 pm »
Hi Team,

I want to use pivot mode with xlsx import.
Please let me show a sample code
and how to use a parameter headerRow of importWb().

Regards,
Koichi

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6299
    • View Profile
Re: How to use pivot mode with xlsx import.
« Reply #1 on: March 19, 2018, 09:29:01 pm »
xlsx import and pivot mode are independent of each other and are not related. Please follow the respective examples of both.

When Excel spreadsheet has data in table format with first row as header, then headerRow parameter can be passed as 1.

Code: [Select]
importWb({
  headerRow: 1,
  ...
})

Hidehiro Ishii

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: How to use pivot mode with xlsx import.
« Reply #2 on: March 20, 2018, 06:10:36 am »
Thank you for your reply.
I want to set a parameter which are 'title' and 'dataIndx' of colModel from headerRow parameter of Excel spreadsheet directory.
Can I do it ?

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6299
    • View Profile
Re: How to use pivot mode with xlsx import.
« Reply #3 on: March 20, 2018, 10:10:38 pm »
title of columns is copied from first row in Excel spreadsheet when headerRow parameter value is 1.

'dataIndx' of columns can't be set with headerRow parameter.

dataIndx of columns is automatically set by importWb module as 0, 1, 2, etc.

colModel of grid can be accessed as usual after import is complete.

Code: [Select]
var colModel = grid.option('colModel');

Hidehiro Ishii

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: How to use pivot mode with xlsx import.
« Reply #4 on: March 22, 2018, 06:46:23 am »
Thank you for your reply.
I try to make source code.
But after importing xlsx file,a header of grid does NOT match a first header of xlsx file.
I will show my source code as follows and attach  a load test file(pivot_test.xlsx) with screenshot image(screenshot.xlsx).
Please suggest how to solve this problem. 

    jQuery(function($) {
        function groupChg(val){           
            var lower = Math.floor( val/ 10 ) * 10,
                upper = Math.ceil( (val + 1)/ 10 ) * 10;
            return lower + " < " + upper;
        };
        function fillOptions(grid) {
            var column = grid.getColumn({ dataIndx: 'country' });
            column.filter.options = grid.getData({ dataIndx: ['country'] });
            column.filter.cache = null;       
        }
      
      var colM = [
            //{title: 'Group', width: 150, tpCls: 'pq-deny-drag'},
            {title: "Athlete", dataIndx: "athlete", width: 200 },
            {title: "Age", dataIndx: "age", width: 90, align: 'center', dataType:'integer',
                groupChange: groupChg
            },
            {title: "Gold", dataIndx: "gold", width: 100, dataType:'integer',
                tpCls: 'gold',denyGroup: true, denyPivot: true
            },
            {title: "Silver", dataIndx: "silver", width: 100, dataType:'integer', denyGroup: true},
            {title: "Bronze", dataIndx: "bronze", width: 100, dataType:'integer', denyGroup: true},
            {title: "Total", dataIndx: 'total', width: 100, dataType:'integer', denyGroup: true },
            {title: "Country", dataIndx: "country", width: 120,               
                filter: {
                    type: 'select',
                    prepend: { '': 'All Countries' },
                    valueIndx: 'country',
                    labelIndx: 'country',
                    condition: 'equal',
                    listeners: ['change']
                }
            },
            {title: "Year", dataIndx: "year", width: 90, dataType: 'integer'},
            //{title: "Date", dataIndx: "date", dataType:'date', width: 110},
            {title: "Sport", dataIndx: "sport", width: 110}
      ];
        var groupM = {
            on: true, //grouping mode.
            pivot: true, //pivotMode
/*
       groupCols: ['year'], //grouping along column axis.
            agg:{ //aggregate fields.
                gold: 'sum',
                silver: 'sum',
                bronze: 'sum'               
            },
            header: false, //hide grouping toolbar.
            grandSummary: true, //show grand summary row.           
            dataIndx: ['country'], //grouping along row axis.
*/
            collapsed: [ false, true ]
      };

        var obj = {         
            height: 500,
            numberCell: {width: 50},
            freezeCols: 1,
            rowBorders: false,
            colModel: colM,           
            groupModel: groupM,
            summaryTitle: {
                avg: "",
                count: '',
                max: "",
                min: "",
             stdev: "",
             stdevp: "",
                sum: ""
            },
            formulas: [['total', function(rd){
                var total = rd.gold + rd.silver + rd.bronze;
                return isNaN(total)? "": total;
            }]],                                   
            filterModel: {on: true, header: true},
            load: function(){
                fillOptions(this);
            },
            showTitle: false,           
            wrap: false,
            hwrap:false,
            toolPanel:{
                show: true  //show toolPanel initially.
            },
            toolbar: {
                items: [
                    {
                        type: 'file',                   
                        label: 'Import xlsx',
                        listener: function( evt ){
                            //import xlsx file via HTML5 file input control.
                            var file = evt.target.files[0];//doesn't work in IE9.
                            if(file && file.name.toUpperCase().substr(-5,5)  == (".XLSX")){
                                grid.showLoading();
                                //import first sheet of xlsx into workbook.
                                pq.excel.importXl( {file: file, sheets:
  • }, function( wb ){

                               
                                    //import workbook into grid.                           
                                    grid.importWb({workbook: wb, headerRow: 1, extraRows: 1000, extraCols: 1000});
                                    grid.hideLoading();
                                });
                        var colM = grid.option('colModel');
                            }                        
                  }
                    },
                    {
                        type: 'button',
                        label: 'Export',
                        listener: function() {
                            this.exportExcel({url: "/pro/demos/exportData", render: true, noheader: true});
                        }
                    },
                {
                        type: 'button',
                        label: "Toolbar Panel",
                        icon: 'ui-icon-wrench',
                        listener: function (evt) {
                            this.ToolPanel().toggle();
                        }
                    }
                ]
            },
            autoRow: true,
            editor:{type:'textarea'},
            editModel: {
                keyUpDown: false,
                saveKey:''         
            }
        };

        var grid = pq.grid( "#grid_pivot", obj);
   
   });

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6299
    • View Profile
Re: How to use pivot mode with xlsx import.
« Reply #5 on: March 22, 2018, 09:09:52 pm »
Sorry, the parameter name is headerRowIndx instead of headerRow.

Code: [Select]
importWb({
  headerRowIndx: 0,
  ...
})


Hidehiro Ishii

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: How to use pivot mode with xlsx import.
« Reply #6 on: March 24, 2018, 04:51:31 pm »
Hi Team,

Thank you for reply.
According to your suggestion, I can set a header of grid from a first header of xlsx file to use the parameter name is headerRowIndx instead of headerRow.
But it does NOT display any parameter on toolPanel.
I want to display a parameter like as a demo of 'Pivot Grid'.
Please more suggestion about this issue.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6299
    • View Profile
Re: How to use pivot mode with xlsx import.
« Reply #7 on: March 26, 2018, 03:56:37 pm »
toolPanel at right side of grid is used for grouping and pivoting.

headerRowIndx or any of the importWb parameters are not supposed to be displayed in the toolPanel.

Can you please provide more details of your requirements, may be I'm not clear about your question.

Hidehiro Ishii

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: How to use pivot mode with xlsx import.
« Reply #8 on: March 29, 2018, 08:21:28 am »
Hi Team,

I make a document what I want to do.
Please check attached file.
I hope it will help for you to understand what I want to do.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6299
    • View Profile
Re: How to use pivot mode with xlsx import.
« Reply #9 on: March 29, 2018, 10:05:47 pm »
There are 2 points to be taken care of.

1) Pivot mode should be turned off before import and turned on only after import is complete.

2) options in filter drop down can be filled after import is complete.

Code: [Select]
        grid.getColumn({dataIndx:0}).filter = {
            type:'select',
            condition:'range',
            options: grid.getData({dataIndx:[0]})
        }
        grid.refreshHeaderFilter({dataIndx:0});
« Last Edit: March 30, 2018, 10:29:26 pm by paramquery »

Hidehiro Ishii

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: How to use pivot mode with xlsx import.
« Reply #10 on: March 31, 2018, 09:36:30 am »
Hi Team,

Thank you for your suggestion.
According to your suggestion, I had found out a reason why it is NOT visible any parameters in toolPanel.
When a theme of Dark Hive is selected,PQGrid does NOT show any parameters in toolPanel.
So many questions about pivot mode are cleared.
But options in filter drop down does NOT work correctly.
I will show my source code and screenshot in attached file.
Please more suggestion.

    jQuery(function($) {

      function groupChg(val){           
            var lower = Math.floor( val/ 10 ) * 10,
                upper = Math.ceil( (val + 1)/ 10 ) * 10;
            return lower + " < " + upper;
        };

      function fillOptions(grid) {
            var column = grid.getColumn({ dataIndx: 0 });
            column.filter.options = grid.getData({ dataIndx:
  • });

            column.filter.cache = null;       
        }
       
        var colM = [
            //{title: 'Group', width: 150, tpCls: 'pq-deny-drag'},
            {title: "Country", dataIndx: 0, width: 120,               
                filter: {
                    type: 'select',
                    prepend: { '': 'All Countries' },
                    valueIndx: 0,
                    labelIndx: 0,
                    condition: 'equal',
                    listeners: ['change']
                }
            },
         {title: "Athlete", dataIndx: 1, width: 200 },
            {title: "Age", dataIndx: 2, width: 90, align: 'center', dataType:'integer',
                groupChange: groupChg
            },
            {title: "Gold", dataIndx: 3, width: 100, dataType:'integer',
                tpCls: 'gold',denyGroup: true, denyPivot: true
            },
            {title: "Silver", dataIndx: 4, width: 100, dataType:'integer', denyGroup: true},
            {title: "Bronze", dataIndx: 5, width: 100, dataType:'integer', denyGroup: true},
            {title: "Total", dataIndx: 6, width: 100, dataType:'integer', denyGroup: true },

            {title: "Year", dataIndx: 7, width: 90, dataType: 'integer'},
            //{title: "Date", dataIndx: "date", dataType:'date', width: 110},
            {title: "Sport", dataIndx: 8, width: 110}
      ];

//      var colM = grid.option('colModel');
      var groupM = {
            on: true, //grouping mode.
            pivot: false, //pivotMode

            groupCols: [7], //grouping along column axis.

         agg:{ //aggregate fields.
                [3]: 'sum',
                [4]: 'sum',
                [5]: 'sum'               
            },
/*
         header: false, //hide grouping toolbar.
*/
            grandSummary: true, //show grand summary row.           
            dataIndx:
  • , //grouping along row axis.


            collapsed: [ false, true ]           
        };
      
      var dataM = {
            location: "remote",
            url: "https://paramquery.com/pro/demos/Content/olympicWinners.json",
            getData: function (data) {
                return { data: data };
            }
        };

        var obj = {
            height: 500,
//            dataModel: dataM,
            numberCell: {width: 50},
            freezeCols: 1,
            rowBorders: false,
            colModel: colM,           
            groupModel: groupM,
            summaryTitle: {
                avg: "",
                count: '',
                max: "",
                min: "",
             stdev: "",
             stdevp: "",
                sum: ""
            },

            formulas: [['total', function(rd){
                var total = rd.gold + rd.silver + rd.bronze;
                return isNaN(total)? "": total;
            }]],                                   

            filterModel: {on: true, header: true},

            load: function(){
                fillOptions(this);
            },

         showTitle: false,           
            wrap: false,
            hwrap:false,
            toolPanel:{
                show: true  //show toolPanel initially.
            },
            toolbar: {
                cls: 'pq-toolbar-export',
                items: [
                    {
                        type: 'file',                   
                        label: 'Import xlsx',
                        listener: function( evt ){
                            //import xlsx file via HTML5 file input control.
                            var file = evt.target.files[0];//doesn't work in IE9.
                            if(file && file.name.toUpperCase().substr(-5,5)  == (".XLSX")){
                                grid.showLoading();
                                //import first sheet of xlsx into workbook.
                                pq.excel.importXl( {file: file, sheets:
  • }, function( wb ){

                               
                                    //import workbook into grid.                           
                                    grid.importWb({workbook: wb, headerRowIndx: 0, extraRows: 0, extraCols: 0});
                                    grid.hideLoading();
                                });

                        grid.getColumn({dataIndx:0}).filter = {
                                    type:'select',
                                    condition:'range',
                                    options: grid.getData({dataIndx:[0]})
                                };
                                grid.refreshHeaderFilter({dataIndx:0});

                            }                       
                        }
                    },
               {
                        type: 'button',
                        label: "Export to Excel(xlsx)",
                        icon: 'ui-icon-document',
                        listener: function (evt) {
                            var str = this.exportExcel({render: true });                           
                            saveAs(str, "pivot.xlsx");
                        }
                    },
                    {
                        type: 'button',
                        label: "Toolbar Panel",
                        icon: 'ui-icon-wrench',
                        listener: function (evt) {
                            this.ToolPanel().toggle();
                        }
                    }
                ]
            }
        };
        var grid = pq.grid( "#PQ_Grid", obj);
    });

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6299
    • View Profile
Re: How to use pivot mode with xlsx import.
« Reply #11 on: April 02, 2018, 11:06:18 pm »
Instead of this

Code: [Select]
grid.getColumn({dataIndx:0}).filter = {
                                    type:'select',
                                    condition:'range',
                                    options: grid.getData({dataIndx:[0]})
                                };
                                grid.refreshHeaderFilter({dataIndx:0});

please use this:

Code: [Select]
fillOptions(grid)
grid.refreshHeaderFilter({dataIndx:0});

Kindly share a jsfiddle if it doesn't work.

Hidehiro Ishii

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: How to use pivot mode with xlsx import.
« Reply #12 on: April 03, 2018, 12:16:33 pm »
Hi Team,

I have changed my code which you have showed.
But it does NOT work correctly.
I will be share my code on jsfiddle.

http://jsfiddle.net/k_ida00/eLs8jyq6/68/

Please check it and give me more suggestion.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6299
    • View Profile
Re: How to use pivot mode with xlsx import.
« Reply #13 on: April 04, 2018, 01:11:19 pm »
Please check this:

http://jsfiddle.net/eLs8jyq6/108/

Code: [Select]
      function fillOptions(grid) {
        var column = grid.getColumn({
          dataIndx: 0
        });
        column.filter = {
          type: 'select',
          prepend: {
            '': 'All Countries'
          },
          valueIndx: 0,
          labelIndx: 0,
          condition: 'equal',
          listeners: ['change'],
          options: grid.getData({
            dataIndx: [0]
          })
        }
      }

Code: [Select]
                  //import first sheet of xlsx into workbook.
                  pq.excel.importXl({
                    file: file,
                    sheets: [0]
                  }, function(wb) {

                    //import workbook into grid.                           
                    grid.importWb({
                      workbook: wb,
                      headerRowIndx: 0,
                      extraRows: 0,
                      extraCols: 0
                    });
                    grid.hideLoading();
                    fillOptions(grid)
                    grid.refreshHeaderFilter({
                      dataIndx: 0
                    });                   
                  });

Hidehiro Ishii

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: How to use pivot mode with xlsx import.
« Reply #14 on: April 05, 2018, 10:46:30 am »
Hi Team,

Thank you for your reply.
According to your suggestion, it works correctly what I want.
Please close this case.