Giter Site home page Giter Site logo

pyrox18 / pyrox.blazorcomponents.datagrid Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 255 KB

An opinionated Blazor data grid component built on top of BlazorStrap

License: MIT License

HTML 40.55% C# 51.47% CSS 7.98%
blazor blazor-server dotnet-core asp-net-core data-grid

pyrox.blazorcomponents.datagrid's Introduction

Pyrox.BlazorComponents.DataGrid

An opinionated Blazor data grid component built on top of BlazorStrap.

Installation

Download and install this package from NuGet using the Package Manager Console, .NET CLI or Visual Studio's NuGet Package Manager.

PM> Install-Package Pyrox.BlazorComponents.DataGrid
# OR
$ dotnet add package Pyrox.BlazorComponents.DataGrid

Usage

NOTE: This component is built and tested with Blazor Server only. This component is not guaranteed to work with other versions of Blazor, such as Blazor WebAssembly.

General usage (with example)

The instructions here are based on the weather forecast service provided in the default Blazor Server template. The code can be found in tests/Pyrox.BlazorComponents.DataGrid.E2ETests.

Assuming we have the following WeatherForecast entity:

public class WeatherForecast
{
    public DateTime Date { get; set; }
    public int TemperatureC { get; set; }
    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
    public string Summary { get; set; }
}

Implement the IDataGridService<TItem> interface, where TItem is the type of the data item (in this case, WeatherForecast). You may need to import the Pyrox.BlazorComponents.DataGrid.Interfaces namespace for this.

public class WeatherForecastService : IDataGridService<WeatherForecast>
{
    // This should be replaced with your actual data source
    private readonly List<WeatherForecast> Data = new List<WeatherForecast>();

    public async Task<DataGridResult<WeatherForecast>> GetItemsAsync(
        int pageNumber,
        int pageSize,
        SortInformation<WeatherForecast> sortInfo = null,
        string searchQuery = null,
        object parameters = null)
    {
        var query = Data.AsQueryable();

        if (!(sortInfo is null))
        {
            // Add logic for sorting here
        }

        if (!(searchQuery is null))
        {
            // Add logic for search queries here
        }

        if (!(parameters is null))
        {
            // Add logic for parameter handling here
        }

        // Get total item count before performing pagination
        var totalItems = (uint)query.Count();

        // Logic for pagination
        var items = query.Skip((pageNumber - 1) * pageSize)
            .Take(pageSize)
            .ToList();

        return new DataGridResult<WeatherForecast>(items, totalItems);
    }
}

SortInformation<TItem> is a class that contains:

  • a Key property of type string that indicates the name of the property to sort the items by, and
  • a Type property, which is a SortType enumeration that has two possible values: Ascending and Descending.

Use these values to determine the sort order for your fetched items and apply those accordingly in your service's logic.

Finally, use the DataGrid component in your Razor pages.

@page "/"

<h1>Weather Forecast</h1>

<DataGrid TItem="WeatherForecast"
          DefaultSort="SortInformation<WeatherForecast>.SortAscending(f => f.Date)"
          Parameters="parameters">
    <GridHeader>
        <th>Date</th>
        <th>Temperature (C)</th>
        <th>Temperature (F)</th>
        <th>Summary</th>
    </GridHeader>
    <GridRow>
        <td>@context.Date</td>
        <td>@context.TemperatureC</td>
        <td>@context.TemperatureF</td>
        <td>@context.Summary</td>
    </GridRow>
</DataGrid>

@code {
    private object parameters = new
    {
        Summary = "Balmy"
    }
}

Supply the TItem type parameter when declaring the component.

The following parameters are optional:

  • DefaultSort: Determines the default sort order for the fetched items. Use the SortInformation<TItem>.SortAscending or SortInformation<TItem>.SortDescending static methods to quickly get the SortInformation<TItem> instance that you want.
  • Parameters: Parameters that you want to filter the results by. For example, supply an OrderId as a parameter and only fetch order items related to that OrderId. You are responsible for handling the presence/absence of these parameters in your IDataGridService implementation.

Customise sort key display name

By default, the sort dropdown gets its key names from the property names of TItem and converts them into title case. If you would like to provide your own sort key display name, you can use the SortKeyDisplayName attribute on your TItem class properties.

public class WeatherForecast
{
    public DateTime Date { get; set; }

    [SortKeyDisplayName("Temperature (C)")]
    public int TemperatureC { get; set; }

    [SortKeyDisplayName("Temperature (F)")]
    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

    public string Summary { get; set; }
}

Contributing

Refer to the CONTRIBUTING.md file for more information on how to contribute to this project.

License

This library is licensed under the MIT license.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.