jQWidgets JqxPivotGrid in Quasar - errors



  • Hi,
    does anybody experience with jQWidgets used in Quasar? I am just trying to use their JqxPivotGrid in my Quasar app with no luck. I use the Vue flavour of the JqxPivotGrid component. There is a demo app here: https://www.jqwidgets.com/vue/vue-pivotgrid/#https://www.jqwidgets.com/vue/vue-pivotgrid/vue-pivotgrid-designer.htm (see the App.vue tab).

    I have tried to copy their code to my app, but an error is raised in the following code

    let dataAdapter = new jqx.dataAdapter(source);
                    dataAdapter.dataBind();
    

    “TypeError: Cannot read property ‘dataAdapter’ of undefined”

    I have tried to build and run their demo that uses pure Vue and it works flawlessly, even the new jqx.dataAdapter(source) code.

    Am I missing something?
    Thanks Ales



  • Just a note: the same sample code using jQuery is:

    var dataAdapter = new $.jqx.dataAdapter(source);
    


  • @ales

    I played with it and it works somewhat. An issue I found was when pressing a filter button: the the popup dialog always opens in the top left corner.

    My recipe is:

    1# install dependencies (quasar package.json)

    // package.js
      "dependencies": {
          ....
         "jqwidgets-scripts": "^8.1.4"
      },
    
    

    then: yarn or npn install

    2# create a page with a vue-pivotgrid-designer component

    https://www.jqwidgets.com/vue/vue-pivotgrid/#https://www.jqwidgets.com/vue/vue-pivotgrid/vue-pivotgrid-designer.htm

    • Wrapped this component in template and q-page
    • fixed the style href urls (last 2 lines)
    <template>
        <q-page class="flex flex-center">
            <table>
                <tr>
                    <td>
                        <JqxPivotDesigner style="width: 250px; height: 400px;"
                                          ref="pivotDesigner"
                                          :type="'pivotGrid'">
                        </JqxPivotDesigner>
                    </td>
                    <td>
                        <JqxPivotGrid style="width: 550px; height: 400px;"
                                      ref="pivotGrid"
                                      :source="pivotDataSource"
                                      :treeStyleRows="false"
                                      :autoResize="false"
                                      :multipleSelectionEnabled="true">
                        </JqxPivotGrid>
                    </td>
                </tr>
            </table>
        </q-page>
    </template>
    <script>
        import JqxPivotGrid from 'jqwidgets-scripts/jqwidgets-vue/vue_jqxpivotgrid.vue';
        import JqxPivotDesigner from 'jqwidgets-scripts/jqwidgets-vue/vue_jqxpivotdesigner.vue';
        export default {
            name: "OfPivotGridDesigner",
            components: {
                JqxPivotGrid,
                JqxPivotDesigner
            },
            data: function () {
                return {
                    pivotDataSource: this.pivotDataSource
                }
            },
            beforeCreate: function () {
                const createPivotDataSource = function () {
                    // prepare sample data
                    let data = new Array();
                    let firstNames =
                        [
                            "Andrew", "Nancy", "Shelley", "Regina", "Yoshi", "Antoni", "Mayumi", "Ian", "Peter", "Lars", "Petra", "Martin", "Sven", "Elio", "Beate", "Cheryl", "Michael", "Guylene"
                        ];
                    let lastNames =
                        [
                            "Fuller", "Davolio", "Burke", "Murphy", "Nagase", "Saavedra", "Ohno", "Devling", "Wilson", "Peterson", "Winkler", "Bein", "Petersen", "Rossi", "Vileid", "Saylor", "Bjorn", "Nodier"
                        ];
                    let productNames =
                        [
                            "Black Tea", "Green Tea", "Caffe Espresso", "Doubleshot Espresso", "Caffe Latte", "White Chocolate Mocha", "Cramel Latte", "Caffe Americano", "Cappuccino", "Espresso Truffle", "Espresso con Panna", "Peppermint Mocha Twist"
                        ];
                    let priceValues =
                        [
                            "2.25", "1.5", "3.0", "3.3", "4.5", "3.6", "3.8", "2.5", "5.0", "1.75", "3.25", "4.0"
                        ];
                    for (let i = 0; i < 500; i++) {
                        let row = {};
                        let productindex = Math.floor(Math.random() * productNames.length);
                        let price = parseFloat(priceValues[productindex]);
                        let quantity = 1 + Math.round(Math.random() * 10);
                        row["firstname"] = firstNames[Math.floor(Math.random() * firstNames.length)];
                        row["lastname"] = lastNames[Math.floor(Math.random() * lastNames.length)];
                        row["productname"] = productNames[productindex];
                        row["price"] = price;
                        row["quantity"] = quantity;
                        row["total"] = price * quantity;
                        data[i] = row;
                    }
                    // create a data source and data adapter
                    let source =
                        {
                            localdata: data,
                            datatype: "array",
                            datafields:
                                [
                                    { name: 'firstname', type: 'string' },
                                    { name: 'lastname', type: 'string' },
                                    { name: 'productname', type: 'string' },
                                    { name: 'quantity', type: 'number' },
                                    { name: 'price', type: 'number' },
                                    { name: 'total', type: 'number' }
                                ]
                        };
                    let dataAdapter = new jqx.dataAdapter(source);
                    dataAdapter.dataBind();
                    // create a pivot data source from the dataAdapter
                    let pivotDataSource = new jqx.pivot(
                        dataAdapter,
                        {
                            customAggregationFunctions: {
                                'var': function (values) {
                                    if (values.length <= 1)
                                        return 0;
                                    // sample's mean
                                    var mean = 0;
                                    for (var i = 0; i < values.length; i++)
                                        mean += values[i];
                                    mean /= values.length;
                                    // calc squared sum
                                    var ssum = 0;
                                    for (var i = 0; i < values.length; i++)
                                        ssum += Math.pow(values[i] - mean, 2)
                                    // calc the variance
                                    var variance = ssum / values.length;
                                    return variance;
                                }
                            },
                            pivotValuesOnRows: false,
                            rows: [{ dataField: 'firstname', text: 'First Name' }, { dataField: 'lastname' }],
                            columns: [{ dataField: 'productname', align: 'left' }],
                            filters: [
                                {
                                    dataField: 'productname',
                                    text: 'Product name',
                                    filterFunction: function (value) {
                                        if (value == "Black Tea" || value == "Green Tea")
                                            return true;
                                        return false;
                                    }
                                }
                            ],
                            values: [
                                { dataField: 'price', 'function': 'sum', text: 'Sum', align: 'left', formatSettings: { prefix: '$', decimalPlaces: 2, align: 'center' }, cellsClassName: 'myItemStyle', cellsClassNameSelected: 'myItemStyleSelected' },
                                { dataField: 'price', 'function': 'count', text: 'Count', className: 'myItemStyle', classNameSelected: 'myItemStyleSelected' }
                            ]
                        }
                    );
                    return pivotDataSource;
                };
                this.pivotDataSource = createPivotDataSource();
            },
            mounted: function() {
                let pivotGridComponent = this.$refs.pivotGrid;
                let pivotGridInstance = pivotGridComponent.getInstance();
                this.$refs.pivotDesigner.target = pivotGridInstance;
                this.$refs.pivotDesigner.refresh();
            }
        }
    </script>
    
    <style src='jqwidgets-scripts/jqwidgets/styles/jqx.base.css'></style>
    <style src='jqwidgets-scripts/jqwidgets/styles/jqx.material-green.css'></style>
    

    Result:

    Image of jqx-desinger



  • @dobbel
    Many thanks, you have helped me a lot!
    My code was almost exactly the same as yours, but there were 2 problems in my code:
    1# I did not copy all the code from then demo exactly, I wrote the imports manually:

    import { JqxPivotGrid, JqxPivotDesigner } from 'jqwidgets-scripts'
    

    but the correct way is (as you wrote):

    import JqxPivotGrid from 'jqwidgets-scripts/jqwidgets-vue/vue_jqxpivotgrid.vue'
    import JqxPivotDesigner from 'jqwidgets-scripts/jqwidgets-vue/vue_jqxpivotdesigner.vue'
    

    My bad!

    2# Then the linter still complained about the code let dataAdapter = new jqx.dataAdapter(source):

    89:33  error  A constructor name should not start with a lowercase letter  new-cap
    89:33  error  'jqx' is not defined                                         no-undef
    

    I have added the following to the linter rules:

        'new-cap': 'off',
        'no-undef': 'off',
    

    And now it works! Thank you again!
    Ales


Log in to reply