Analyze Your Data with DataSpark and PivotTable.js

Using PivotTable.js in an ASP.NET MVC Application

Learn how to integrate PivotTable.js into an ASP.NET MVC application for interactive data analysis and dynamic pivot table generation.

Overview

PivotTable.js is a powerful JavaScript library that enables users to create pivot tables directly in the browser. This example demonstrates how to integrate PivotTable.js into an ASP.NET MVC application, allowing for interactive data analysis with a dynamic pivot table rendered on a Razor view.

Using PivotTable.js in an ASP.NET MVC Application

Learn how to integrate PivotTable.js into an ASP.NET MVC application for interactive data analysis and dynamic pivot table generation.

Overview

PivotTable.js is a powerful JavaScript library that allows you to create interactive pivot tables in your web applications. In this guide, we'll explore how to integrate PivotTable.js into a .NET 8 MVC application, leveraging MVC Controllers, Razor Pages, and Bootstrap 5 for UI enhancements.

Setting Up Your .NET 8 MVC Project

Start by creating a new .NET 8 MVC project in Visual Studio. Ensure you have the latest version of .NET 8 installed. Use the following command in your terminal:

        
dotnet new mvc -n PivotTableDemo
              
            

Once your project is set up, open the Startup.cs file to configure the services and middleware.

Adding PivotTable.js to Your Project

Include PivotTable.js in your project by adding its CSS and JavaScript files. You can use a CDN link for simplicity:

        
<link href="https://cdnjs.cloudflare.com/ajax/libs/pivottable/2.23.0/pivot.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/pivottable/2.23.0/pivot.min.js"></script>
              
            

Add these links to your _Layout.cshtml file under the <head> section.

Creating the Pivot Table View

In your MVC project, create a new View named PivotTable.cshtml. Use Bootstrap's grid system to layout the pivot table interface:

        
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <h2>Pivot Table Example</h2>
            <div id="pivot-output" class="mt-3"></div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $("#pivot-output").pivotUI([
            {name: "John", age: 30, gender: "Male"},
            {name: "Jane", age: 25, gender: "Female"},
            {name: "Doe", age: 35, gender: "Male"}
        ], {
            rows: ["gender"],
            cols: ["age"]
        });
    });
</script>
              
            

Handling Data in MVC Controllers

Create an API endpoint in your MVC Controller to handle data requests. Here is an example controller action that returns data in JSON format:

        
public class PivotController : Controller
{
    [HttpGet]
    public JsonResult GetData()
    {
        var data = new[]
        {
            new { Name = "John", Age = 30, Gender = "Male" },
            new { Name = "Jane", Age = 25, Gender = "Female" },
            new { Name = "Doe", Age = 35, Gender = "Male" }
        };
        return Json(data);
    }
}
              
            

Integrating Bootstrap 5 UI Elements

Enhance the user interface using Bootstrap 5 components like cards, buttons, and modals. Below is an example of wrapping the pivot table in a Bootstrap card:

        
<div class="card">
    <div class="card-header">Pivot Table Visualization</div>
    <div class="card-body">
        <div id="pivot-output"></div>
    </div>
</div>
              
            

Final Thoughts

Implementing PivotTable.js in a .NET 8 MVC application can significantly enhance your data visualization capabilities. By combining PivotTable.js with Bootstrap 5, you can create interactive and visually appealing data representations that are easy to navigate and interpret.

© 2024 - PivotTable.js Implementation Guide