数据表格(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-11-27 19:42
1name13Male2024-11-26 19:42
2name24Male2024-11-25 19:42
3name35Famle2024-11-24 19:42
4name46Male2024-11-23 19:42
5name57Male2024-11-22 19:42
6name68Famle2024-11-21 19:42
7name79Male2024-11-20 19:42
8name810Male2024-11-19 19:42
9name911Famle2024-11-18 19:42

分页逻辑

Id名称年龄性别生日
0name02Famle2024-11-27 19:42
1name13Male2024-11-26 19:42
2name24Male2024-11-25 19:42
3name35Famle2024-11-24 19:42
4name46Male2024-11-23 19:42

自定义列

Id名称自定义头-年龄性别生日空行操作
0name0 自定义列,年龄:2Famle2024-11-27 19:42
1name1 自定义列,年龄:3Male2024-11-26 19:42
2name2 自定义列,年龄:4Male2024-11-25 19:42
3name3 自定义列,年龄:5Famle2024-11-24 19:42
4name4 自定义列,年龄:6Male2024-11-23 19:42

列排

Id 名称 年龄 性别生日
0name02Famle2024-11-27 19:42
1name13Male2024-11-26 19:42
2name24Male2024-11-25 19:42
3name35Famle2024-11-24 19:42
4name46Male2024-11-23 19:42

过滤

Id 名称 年龄性别 生日
0name02Famle2024-11-27 19:42
1name13Male2024-11-26 19:42
2name24Male2024-11-25 19:42
3name35Famle2024-11-24 19:42
4name46Male2024-11-23 19:42

展开行(详细行)

Id名称年龄性别生日
0name02Famle2024-11-27 19:42
1name13Male2024-11-26 19:42
2name24Male2024-11-25 19:42
3name35Famle2024-11-24 19:42
4name46Male2024-11-23 19:42

index列

Id名称年龄性别生日
10name02Famle2024-11-27 19:42
21name13Male2024-11-26 19:42
32name24Male2024-11-25 19:42
43name35Famle2024-11-24 19:42
54name46Male2024-11-23 19:42

单击选中行

Id名称年龄性别生日
0name02Famle2024-11-27 19:42
1name13Male2024-11-26 19:42
2name24Male2024-11-25 19:42
3name35Famle2024-11-24 19:42
4name46Male2024-11-23 19:42

多选行

Id名称年龄性别生日
0name02Famle2024-11-27
1name13Male2024-11-26

文档