Enterprise-level component library based on Bootstrap and Blazor

star nuget master download license repo commit

Table 表格

支持动态添加列

仅展示 DataTable 数据

Demo
Loading...

增加编辑维护功能

Demo
Loading...

通过代码动态调整 DataTable 表格组件自动更新

Demo
Loading...
@page "/tables/dynamic"

<h3>Table 表格</h3>

<h4>支持动态添加列</h4>

<Block Title="普通用法" Introduction="仅展示 <code>DataTable</code> 数据">
    <Table TItem="DynamicObject" DynamicContext="DataTableDynamicContext" />
</Block>

<Block Title="编辑功能" Introduction="增加编辑维护功能">
    <Table TItem="DynamicObject" DynamicContext="DataTableDynamicContext"
           IsMultipleSelect="true" IsBordered="true" IsStriped="true"
           ShowToolbar="true" ShowExtendButtons="true" />
</Block>

<Block Title="动态列" Introduction="通过代码动态调整 <code>DataTable</code> 表格组件自动更新">
    <Table TItem="DynamicObject" DynamicContext="DataTableDynamicContext"
           IsMultipleSelect="true" IsBordered="true" IsStriped="true"
           ShowToolbar="true" ShowExtendButtons="true">
        <TableToolbarTemplate>
            <TableToolbarButton TItem="DynamicObject" Color="Color.Info" Icon="fa fa-fw fa-plus-circle" Text="@ButtonAddColumnText" OnClick="OnAddColumn" />
            <TableToolbarButton TItem="DynamicObject" Color="Color.Secondary" Icon="fa fa-fw fa-minus-circle" Text="@ButtonRemoveColumnText" OnClick="OnRemoveColumn" />
        </TableToolbarTemplate>
    </Table>
</Block>
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

using BootstrapBlazor.Components;
using BootstrapBlazor.Shared.Pages.Components;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;

namespace BootstrapBlazor.Shared.Pages.Table
{
    /// <summary>
    /// 动态表格示例
    /// </summary>
    public partial class TablesDynamic
    {
        [NotNull]
        private DataTableDynamicContext? DataTableDynamicContext { get; set; }

        private DataTable UserData { get; } = new DataTable();

        [Inject]
        [NotNull]
        private IStringLocalizer<Foo>? Localizer { get; set; }

        [Inject]
        [NotNull]
        private IStringLocalizer<Tables>? TablesLocalizer { get; set; }

        private string? ButtonAddColumnText { get; set; }

        private string? ButtonRemoveColumnText { get; set; }

        /// <summary>
        /// OnInitialized 方法
        /// </summary>
        protected override void OnInitialized()
        {
            base.OnInitialized();

            ButtonAddColumnText ??= TablesLocalizer[nameof(ButtonAddColumnText)];
            ButtonRemoveColumnText ??= TablesLocalizer[nameof(ButtonRemoveColumnText)];

            // 初始化 DataTable
            InitDataTable();
        }

        private void CreateContext()
        {
            // 初始化动态类型上下文实例
            DataTableDynamicContext = new DataTableDynamicContext(UserData, (context, col) =>
            {
                var propertyName = col.GetFieldName();
                if (propertyName == nameof(Foo.DateTime))
                {
                    context.AddRequiredAttribute(nameof(Foo.DateTime));
                    // 使用 AutoGenerateColumnAttribute 设置显示名称示例
                    context.AddAutoGenerateColumnAttribute(nameof(Foo.DateTime), new KeyValuePair<string, object?>[] {
                        new(nameof(AutoGenerateColumnAttribute.Text), Localizer[nameof(Foo.DateTime)].Value)
                    });
                }
                else if (propertyName == nameof(Foo.Name))
                {
                    context.AddRequiredAttribute(nameof(Foo.Name), Localizer["Name.Required"]);
                    // 使用 Text 设置显示名称示例
                    col.Text = Localizer[nameof(Foo.Name)];
                }
                else if (propertyName == nameof(Foo.Count))
                {
                    context.AddRequiredAttribute(nameof(Foo.Count));
                    // 使用 DisplayNameAttribute 设置显示名称示例
                    context.AddDisplayNameAttribute(nameof(Foo.Count), Localizer[nameof(Foo.Count)].Value);
                }
                else if (propertyName == nameof(Foo.Complete))
                {
                    col.Filterable = true;
                    col.ComponentType = typeof(Switch);
                    // 使用 DisplayAttribute 设置显示名称示例
                    context.AddDisplayAttribute(nameof(Foo.Complete), new KeyValuePair<string, object?>[] {
                        new(nameof(DisplayAttribute.Name), Localizer[nameof(Foo.Complete)].Value)
                    });
                }
            });
        }

        private void InitDataTable()
        {
            UserData.Columns.Add(nameof(Foo.DateTime), typeof(DateTime));
            UserData.Columns.Add(nameof(Foo.Name), typeof(string));
            UserData.Columns.Add(nameof(Foo.Count), typeof(int));

            Foo.GenerateFoo(Localizer, 10).ForEach(f =>
            {
                UserData.Rows.Add(f.DateTime, f.Name, f.Count);
            });

            CreateContext();
        }

        private Task OnAddColumn()
        {
            if (!UserData.Columns.Contains(nameof(Foo.Complete)))
            {
                UserData.Columns.Add(nameof(Foo.Complete), typeof(bool));

                // 更新数据
                var fs = Foo.GenerateFoo(Localizer, 10);
                for (var i = 0; i < fs.Count; i++)
                {
                    UserData.Rows[i][nameof(Foo.Complete)] = fs[i].Complete;
                }
                CreateContext();
                StateHasChanged();
            }
            return Task.CompletedTask;
        }

        private Task OnRemoveColumn()
        {
            if (UserData.Columns.Contains(nameof(Foo.Complete)))
            {
                UserData.Columns.Remove(nameof(Foo.Complete));
                CreateContext();
                StateHasChanged();
            }
            return Task.CompletedTask;
        }
    }
}

B 站相关视频链接

[传送门]

交流群

QQ Group:BootstrapAdmin & Blazor 795206915( Full) 675147445 Welcome to join the group discussion
Themes
Bootstrap
Ant Design (完善中)
Bluma (完善中)
LayUI (完善中)
Motronic (已集成)
An error has occurred. This application may no longer respond until reloaded. Reload