Check documentation for the latest version of DHTMLX Pivot Working with Pivot Pivot Docs

Working with Pivot

Getting/Setting Fields

It is possible to get the current Pivot fields object and set a new one. Use the two related methods - getFields and setFields for this purpose:

// get the current pivot fields 
var fields = myPivot.getFields();
 
// set a new fields object
myPivot.setFields(newFields);

The setFields() method takes a fields' object as a parameter.

The newFields object in the above example has the same structure as the fields object in the Pivot constructor:

var newFields = {
    rows: ["form", "name"],
    columns: ["year"],
    values: [
        { id: "oil", method: "max" }, 
        { id: "oil", method: "sum" }
    ]
}

Related sample:  Set fields

Defining Data Operations

All data operations are specified as objects of the values array within the Pivot fields object.

fields: {
    rows: ["form", "name"],
    columns: ["year"],
    values: [
        { id: "oil", method: "max" }, // max value of the "oil" property will be shown
        { id: "oil", method: "sum" }  // values of the "oil" property will be summed up       
    ]
}
  • The id of the field object corresponds to a data property
  • The method attribute specifies the type of operation that will be applied to data

You can choose between four preset types of operations that you can apply for a dataset:

  • sum - displays the sum of values of the property
  • max - displays the max value of the property from the dataset
  • min - displays the min value of the property from the dataset
  • count - displays the number of occurrences of the property in the dataset

Read also about possibilities of configuring Pivot operations.

Adding a custom type of operation

You can add your own operation in addition to the predefined set. For this, you have to use the addMathMethod method.

You need to pass three parameters to this method:

  1. name - (string) the name of the new operation
  2. label - (string) the label of the new operation
  3. function - (function) the function that will be called for cells assigned to this operation
myPivot.addMathMethod("avr", "Avr", (cellData) => {
    const sum = cellData.reduce((el, all) => all += el);
    return (sum / cellData.length).toFixed(3);
});

Related sample:  Custom methods

Getting Config Settings

You can get the current Pivot configuration settings via the getConfig method to check whether everything is adjusted correctly:

var config = myPivot.getConfig();

The returned config object will contain all the attributes included in the Pivot configuration object.

Read about the ways of configuring Pivot in the article Configuring Pivot.

Back to top