Getting Started with the Vue Pivotview Component in Vue 2
16 Jul 202414 minutes to read
This article provides a step-by-step guide for setting up a Vue 2 project using Vue-CLI and integrating the Syncfusion Vue Pivotview component
To quickly get started with Vue Pivot Table, you can refer to this video:
Prerequisites
System requirements for Syncfusion Vue UI components
Dependencies
The following list of dependencies are required to use the pivot table component in your application.
|-- @syncfusion/ej2-vue-pivotview
|-- @syncfusion/ej2-base
|-- @syncfusion/ej2-data
|-- @syncfusion/ej2-excel-export
|-- @syncfusion/ej2-file-utils
|-- @syncfusion/ej2-compression
|-- @syncfusion/ej2-pdf-export
|-- @syncfusion/ej2-file-utils
|-- @syncfusion/ej2-compression
|-- @syncfusion/ej2-grids
|-- @syncfusion/ej2-inputs
|-- @syncfusion/ej2-buttons
|-- @syncfusion/ej2-dropdowns
|-- @syncfusion/ej2-lists
|-- @syncfusion/ej2-popups
|-- @syncfusion/ej2-navigations
Setting up the Vue 2 project
To generate a Vue 2 project using Vue-CLI, use the vue create command. Follow these steps to install Vue CLI and create a new project:
npm install -g @vue/cli
vue create quickstart
cd quickstart
npm run serve
or
yarn global add @vue/cli
vue create quickstart
cd quickstart
yarn run serve
When creating a new project, choose the option Default ([Vue 2] babel, eslint)
from the menu.
Once the quickstart
project is set up with default settings, proceed to add Syncfusion components to the project.
Add Syncfusion Vue packages
Syncfusion packages are available at npmjs.com. To use Vue components, install the required npm package.
This article uses the Vue Pivotview component as an example. Install the @syncfusion/ej2-vue-pivotview
package by running the following command:
npm install @syncfusion/ej2-vue-pivotview --save
or
yarn add @syncfusion/ej2-vue-pivotview
The –save will instruct NPM to include the pivot table package inside the
dependencies
section of thepackage.json
.
Import Syncfusion CSS styles
The pivot table has different themes. They are:
- Material
- Fabric
- Bootstrap
- High Contrast
import pivot table component CSS as given below in <style>
section of the App.vue
file.
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/material.css";
@import "../node_modules/@syncfusion/ej2-lists/styles/material.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/material.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/material.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/material.css";
@import "../node_modules/@syncfusion/ej2-grids/styles/material.css";
@import "../node_modules/@syncfusion/ej2-pivotview/styles/material.css";
</style>
Add Syncfusion Vue component
Follow the below steps to add the Vue Pivotview component:
1. First, import and register the Pivotview component in the script
section of the src/App.vue file.
<script setup>
import { PivotViewComponent, FieldList, GroupingBar, CalculatedField } from "@syncfusion/ej2-vue-pivotview";
</script>
<script>
import { PivotViewComponent } from "@syncfusion/ej2-vue-pivotview";
export default {
components: {
'ejs-pivotview': PivotViewComponent
}
}
</script>
2. In the template
section, define the Pivotview component.
<template>
<div id="app">
<ejs-pivotview> </ejs-pivotview>
</div>
</template>
Browser compatibility
Polyfills are required to use the Pivot Table in Internet Explorer 11 browser. Refer the documentation for more details.
Assigning sample data to the pivot table
The Pivot Table component further needs to be populated with an appropriate data source. For illustration purpose, a collection of objects mentioning the sales details of certain products over a period and region has been prepared. This sample data is assigned to the pivot table component through dataSource
property under dataSourceSettings
.
<template>
<div id="app">
<ejs-pivotview :dataSourceSettings="dataSourceSettings"> </ejs-pivotview>
</div>
</template>
<script>
import { PivotViewComponent } from "@syncfusion/ej2-vue-pivotview";
export default {
components: {
'ejs-pivotview': PivotViewComponent
},
data () {
return {
dataSourceSettings: {
dataSource: [
{
"balance": 2430.87,
"quantity": 11,
"name": "Skinner Ward",
"gender": "male",
"company": "GROK",
"state": "New Jercy"
},
{
"balance": 3192.7,
"quantity": 15,
"name": "Gwen Dixon",
"gender": "female",
"company": "ICOLOGY",
"state": "Vetaikan"
},
{
"balance": 1663.84,
"quantity": 14,
"name": "Deena Gillespie",
"gender": "female",
"company": "OVERPLEX",
"state": "New Jercy"
}
]
}
}
}
}
</script>
<style>
@import "@syncfusion/ej2-vue-pivotview/styles/material.css";
</style>
Adding fields to row, column, value and filter axes
Now that pivot table is initialized and assigned with sample data, will further move to showcase the component by organizing appropriate fields in row, column, value and filter axes.
In dataSourceSettings
, four major axes - rows
, columns
, values
and filters
plays a vital role in defining and organizing fields from the bound data source, to render the entire pivot table component in a desired format.
rows
– Collection of fields that needs to be displayed in row axis of the pivot table.
columns
– Collection of fields that needs to be displayed in column axis of the pivot table.
values
– Collection of fields that needs to be displayed as aggregated numeric values in the pivot table.
filters
- Collection of fields that would act as master filter over the data bound in row, column and value axes of the pivot table.
In-order to define each field in the respective axis, the following basic properties should be set.
-
name
: It allows to set the field name from the bound data source. It’s casing should match exactly like in the data source and if not set properly, the pivot table will not be rendered. -
caption
: It allows to set the field caption, which is the alias name of the field that needs to be displayed in the pivot table. -
type
: It allows to set the summary type of the field. By default, Sum is applied.
In this illustration, “Year” and “Quarter” are added in column, “Country” and “Products” in row, and “Sold” and “Amount” in value section respectively.
{% include code-snippet/pivot-grid/default-cs88/app-composition.vue %}
{% include code-snippet/pivot-grid/default-cs88/app.vue %}
Applying formatting to a value field
Formatting defines a way in which values should be displayed. For example, format “C” denotes the values should be displayed in currency pattern. To do so, define the formatSetting
with its name
and format
properties and add it to formatSettings
. In this illustration, the name
property is set as Amount, a field from value section and its format is set as currency. Likewise, we can set format for other value fields as well and add it to formatSettings
.
Only fields from value section, which is in the form of numeric data values are applicable for formatting.
{% include code-snippet/pivot-grid/default-cs89/app-composition.vue %}
{% include code-snippet/pivot-grid/default-cs89/app.vue %}
Module injection
To create pivot table with additional features, inject the required modules. The modules that are available with basic functionality are as follows.
-
GroupingBar
- Inject this module to access grouping bar feature. -
FieldList
- Inject this module to access pivot field list feature. -
CalculatedField
- Inject this module to access calculated field feature.
Register the required array of modules under the key pivotview
in the provide
section within the App.vue
file as shown below. On doing so, only the injected views will be loaded and displayed along with pivot table.
<script>
provide: {
pivotview: [GroupingBar, FieldList, CalculatedField]
}
</script>
Enable Pivot Field List
The field list allows to add or remove fields and also rearrange the fields between different axes, including column, row, value, and filter along with filter and sort options dynamically at runtime. It can be enabled by setting the showFieldList
property to true. To know more about field list, refer
here.
If the
FieldList
module is not injected, the Field List will not be rendered with the pivot table component.
{% include code-snippet/pivot-grid/default-cs90/app-composition.vue %}
{% include code-snippet/pivot-grid/default-cs90/app.vue %}
Enable Grouping Bar
The grouping bar feature automatically populates fields from the bound data source and allows end users to drag fields between different axes such as columns, rows, values, and filters, and alter pivot table at runtime. It also provides option to sort, filter and remove fields. It can be enabled by setting the showGroupingBar
property to true. To know more about grouping bar, refer
here.
If the
GroupingBar
module is not injected, the grouping bar will not be rendered with the pivot table component.
{% include code-snippet/pivot-grid/default-cs91/app-composition.vue %}
{% include code-snippet/pivot-grid/default-cs91/app.vue %}
Exploring Filter Axis
The filter axis contains collection of fields that would act as master filter over the data bound in row, column and value axes of the pivot table. The fields along with filter members could be set to filter axis either through report via code behind or by dragging and dropping fields from other axes to filter axis via grouping bar or field list at runtime.
{% include code-snippet/pivot-grid/default-cs92/app-composition.vue %}
{% include code-snippet/pivot-grid/default-cs92/app.vue %}
Calculated field
The calculated field feature allows user to insert or add a new calculated field based on the available fields from the bound data source using basic arithmetic operators. The calculated field can be included in pivot table using the calculatedFieldSettings
from code behind. Or else, calculated fields can be added at run time through the built-in dialog by just setting the allowCalculatedField
property to true in pivot table. You will see a button enabled in the Field List UI automatically to invoke the calculated field dialog and perform necessary operation. To know more about calculated field, refer
here.
If the
CalculatedField
module is not injected, the calculated field popup will not be rendered with the pivot table component. By default, the calculated fields created through code-behind are only added to the field list and calculated field dialog UI. To display the calculated field in the pivot table UI, it must be added to thevalues
property, as shown in the code below. Additionally, calculated fields can only be added to the value axis.
{% include code-snippet/pivot-grid/default-cs93/app-composition.vue %}
{% include code-snippet/pivot-grid/default-cs93/app.vue %}
Run the application
The quickstart project is configured to compile and run the application in the browser. Use the following command to run the application.
npm run serve
or
yarn run serve
Output will be displayed as follows.
{% include code-snippet/pivot-grid/default-cs94/app-composition.vue %}
{% include code-snippet/pivot-grid/default-cs94/app.vue %}
For more information and to access the quick start project, visit: GitHub Repository
You can also explore our Vue Pivot Table example that shows how to rendering of the pivot table with drill-up and drill-down functionality bound to a relational report.