Blazor 定时刷新Table功能

本文介绍了如何在Blazor应用中使用SynTable组件,展示了一个动态加载10000条数据的表格,并通过定时器每秒从数据库模拟获取10条数据,实时更新用户界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@page "/synTable"
@using BlazorApp.Data

<h3>SynTable</h3>

<Table TItem="Foo" Height="300" Items="Items" IsBordered="true" IsStriped="true">
    <TableColumns>
        <TableColumn @bind-Field="@context.DateTime" Width="180" />
        <TableColumn @bind-Field="@context.Name" />
        <TableColumn @bind-Field="@context.Address" />
    </TableColumns>
</Table>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using System.Net.Http;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.Web.Virtualization;
using Microsoft.JSInterop;
using BlazorApp;
using BlazorApp.Shared;
using BootstrapBlazor.Components;
using BlazorApp.Data;
using System.Diagnostics.CodeAnalysis;


namespace BlazorApp.Pages
{
    public partial class SynTable
    {
        [NotNull]
        private List<Foo>? DBList { get; set; }

        [NotNull]
        private List<Foo>? Items { get; set; }

        private System.Threading.Timer timer { get; set; }

        private int index = 1;

        /// <summary>
        /// OnInitialized
        /// </summary>
        protected override void OnInitialized()
        {
            base.OnInitialized();
            //模拟数据库
            DBList = Foo.GenerateFoo(10000);

            //初始化
            Items = Foo.GenerateFoo(10);

            //定时器
            timer = new System.Threading.Timer((args) =>
            {
                index++;

                //从数据库获取数据
                var list = DBList.Skip((index - 1) * 10).Take(10).ToList();
                // 得到的数据未0,代表数据加载完了
                if (list.Count == 0)
                {
                    timer.Dispose(); // 停止并释放定时器资源

                    return;//不执行后面的代码
                }
                foreach (var foo in list) {
                    Items.Add(foo);
                }

                // 通知Blazor更新UI
                InvokeAsync(() => { StateHasChanged(); });

            }, null, TimeSpan.Zero, TimeSpan.FromSeconds(1));//1秒执行一次


        }


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值