WorkpieceModelPage.razor 1.67 KB
@page "/Basic/WorkpieceModel"
@using System.Text.Json;
@using AntDesign.TableModels
@using System.ComponentModel
@using DataAcquisitionServer.DataAccess
@using DataAcquisitionServer.Models
@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<DataContext> dbContextFactory;

<Table @ref="table"
       TItem="WorkpieceModel"
       Loading="loading"
       DataSource="@dataItems"
       Total="_total"
       @bind-PageIndex="_pageIndex"
       @bind-PageSize="_pageSize"
       OnChange="OnChange"
       Size="TableSize.Small"
       RowKey="x=>x.Id">
    <PropertyColumn Property="c=>c.Id" />
    <PropertyColumn Title="编号" Property="c=>c.Code" />
    <PropertyColumn Title="名称" Property="c=>c.Name" />
    <PropertyColumn Title="标识" Property="c=>c.Flag" />
    <PropertyColumn Title="备注" Property="c=>c.Remark" />
</Table>

@code {
    bool loading = true;

    List<WorkpieceModel> dataItems = new List<WorkpieceModel>();

    IEnumerable<WorkpieceModel> selectedRows = null!;
    ITable table = null!;

    int _pageIndex = 1;
    int _pageSize = 10;
    int _total = 0;

    public async Task OnChange(QueryModel<WorkpieceModel> queryModel)
    {
        loading = true;
        using var dbContext = dbContextFactory.CreateDbContext();
        var query = dbContext.WorkpieceModels;

        dataItems = await query.OrderBy(x => x.Id).Skip(((queryModel.PageIndex - 1) * queryModel.PageSize)).Take(queryModel.PageSize).AsNoTracking().ToListAsync();

        _total = await query.CountAsync();
        loading = false;
    }

    public void RemoveSelection(int id)
    {
        var selected = selectedRows.Where(x => x.Id != id);
        selectedRows = selected;
    }
}