数据表格(DataTable)

一个使用Data属性绑定数据源的简单表格


下面例子中的数据模型

using System;
using System.Collections.Generic;
using System.Linq;
using BulmaRazor.Components;

namespace BulmaRazorServer.Pages.docs.components.datatable
{
    public enum Gender
    {
        Famle,
        Male
    }

    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public Gender Gender { get; set; }
        public DateTime Birthday { get; set; }

        public Person(int id, string name, int age, DateTime birthday, Gender gender)
        {
            Id = id;
            Name = name;
            Age = age;
            Birthday = birthday;
            Gender = gender;
        }

        public Person()
        {
        }


        //表示从后台获取数据
        static List<Person> allData;

        static Person()
        {
            allData = new List<Person>();
            for (int i = 0; i < 1983; i++)
            {
                allData.Add(new Person()
                {
                    Id = i,
                    Name = "name" + i,
                    Birthday = DateTime.Now.AddDays(-i),
                    Age = i + 2,
                    Gender = i % 3 == 0 ? Gender.Famle : Gender.Male
                });
            }
        }

        internal static IEnumerable<Person> GetPageData(string kw, int pageIndex, int pageSize, out int totalCount)
        {
            IEnumerable<Person> list = allData;
            if (!string.IsNullOrEmpty(kw))
            {
                list = allData.Where(x => x.Name.Contains(kw));
            }

            totalCount = list.Count();
            return list.Skip((pageIndex - 1) * pageSize).Take(pageSize);
        }
    }
}
Show Code

基础功能

Id名称年龄性别生日
0name02Famle2024-10-25 18:58
1name13Male2024-10-24 18:58
2name24Male2024-10-23 18:58
3name35Famle2024-10-22 18:58
4name46Male2024-10-21 18:58
5name57Male2024-10-20 18:58
6name68Famle2024-10-19 18:58
7name79Male2024-10-18 18:58
8name810Male2024-10-17 18:58
9name911Famle2024-10-16 18:58

分页逻辑

Id名称年龄性别生日
0name02Famle2024-10-25 18:58
1name13Male2024-10-24 18:58
2name24Male2024-10-23 18:58
3name35Famle2024-10-22 18:58
4name46Male2024-10-21 18:58

自定义列

Id名称自定义头-年龄性别生日空行操作
0name0 自定义列,年龄:2Famle2024-10-25 18:58
1name1 自定义列,年龄:3Male2024-10-24 18:58
2name2 自定义列,年龄:4Male2024-10-23 18:58
3name3 自定义列,年龄:5Famle2024-10-22 18:58
4name4 自定义列,年龄:6Male2024-10-21 18:58

列排

Id 名称 年龄 性别生日
0name02Famle2024-10-25 18:58
1name13Male2024-10-24 18:58
2name24Male2024-10-23 18:58
3name35Famle2024-10-22 18:58
4name46Male2024-10-21 18:58

过滤

Id 名称 年龄性别 生日
0name02Famle2024-10-25 18:58
1name13Male2024-10-24 18:58
2name24Male2024-10-23 18:58
3name35Famle2024-10-22 18:58
4name46Male2024-10-21 18:58

展开行(详细行)

Id名称年龄性别生日
0name02Famle2024-10-25 18:58
1name13Male2024-10-24 18:58
2name24Male2024-10-23 18:58
3name35Famle2024-10-22 18:58
4name46Male2024-10-21 18:58

index列

Id名称年龄性别生日
10name02Famle2024-10-25 18:58
21name13Male2024-10-24 18:58
32name24Male2024-10-23 18:58
43name35Famle2024-10-22 18:58
54name46Male2024-10-21 18:58

单击选中行

Id名称年龄性别生日
0name02Famle2024-10-25 18:58
1name13Male2024-10-24 18:58
2name24Male2024-10-23 18:58
3name35Famle2024-10-22 18:58
4name46Male2024-10-21 18:58

多选行

Id名称年龄性别生日
0name02Famle2024-10-25
1name13Male2024-10-24

文档