jQWidgets Forums

Forum Replies Created

Viewing 1 post (of 1 total)
  • Author
    Posts
  • in reply to: JqxPivotGrid and json data JqxPivotGrid and json data #97477

    curibe
    Participant

    Hi,
    The same thing happened to me and I managed to find the solution.

    I got the data in a separate function to then pass it localdata. This my solution

    <script>
    $(document).ready(function() {
    ObtenerDatos();
    });

    function ObtenerDatos() {
    var url = “Home/DataHandler”;
    $.ajax({
    type: “POST”,
    url: url,
    success: function(data) {
    BindPivot(data);
    }
    });
    }

    function BindPivot(data)
    {
    var source =
    {
    localdata : data,
    datatype: “json”,
    datafields: [
    { name: “FirstName” }, { name: “LastName” }, { name: “Product” }, { name: “Price”, type: “float” },
    { name: “Quantity”, type: “int” }, { name: “Total”, type: “float” }
    ]
    };

    var dataAdapter = new $.jqx.dataAdapter(source);
    dataAdapter.dataBind();
    // create a pivot data source from the dataAdapter
    var 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,
    fields: [
    { dataField: ‘FirstName’, text: ‘Name’ },
    { dataField: ‘LastName’, text: ‘Last Name’ },
    { dataField: ‘Product’, text: ‘Product Name’ },
    { dataField: ‘Quantity’, text: ‘Quantity’ },
    { dataField: ‘Price’, text: ‘Price’ },
    { dataField: ‘Total’, text: ‘Total’ }
    ],
    rows: [
    { dataField: ‘FirstName’, text: ‘FirstName’ },
    { dataField: ‘LastName’, text: ‘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’ }
    ]
    });
    var localization = { ‘var’: ‘Variance’ };
    // create a pivot grid
    $(‘#divPivotGrid’).jqxPivotGrid(
    {
    localization: localization,
    source: pivotDataSource,
    treeStyleRows: false,
    autoResize: false,
    multipleSelectionEnabled: true
    });
    var pivotGridInstance = $(‘#divPivotGrid’).jqxPivotGrid(‘getInstance’);
    // create a pivot grid
    $(‘#divPivotGridDesigner’).jqxPivotDesigner(
    {
    type: ‘pivotGrid’,
    target: pivotGridInstance
    });
    }

    </script>
    }

Viewing 1 post (of 1 total)