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

<Spin Spinning="loading">
    <Table @ref="table"
           TItem="CommunicationConfig"
           DataSource="@communicationConfigs"
           Total="_total"
           @bind-PageIndex="_pageIndex"
           @bind-PageSize="_pageSize"
           @bind-SelectedRows="selectedRows"
           OnChange="OnChange"
           Size="TableSize.Small"
           RowKey="x=>x.Id">
        @* <Selection Key="@(context.Id.ToString())" /> *@
        <PropertyColumn Property="c=>c.Id" Sortable />
         <PropertyColumn Title="编号" Property="c=>c.Code"/>
         <PropertyColumn Title="名称" Property="c=>c.Name" />
         <PropertyColumn Title="通信方式" Property="c=>c.CommunicationType" />
         <PropertyColumn Title="设备型号" Property="c=>c.EquipmentModel" />
         <PropertyColumn Title="IP地址" Property="c=>c.IpAddress" />
         <PropertyColumn Title="端口" Property="c=>c.Port" />
         <PropertyColumn Title="是否启用" Property="c=>c.Enable">
             <Switch @bind-Value="@context.Enable" Disabled="true"></Switch>
         </PropertyColumn>
         <PropertyColumn Title="备注" Property="c=>c.Remark" />
     </Table>
 </Spin>

 @code {
    bool loading = true;

    List<CommunicationConfig> communicationConfigs = new List<CommunicationConfig>();

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

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

    public async Task OnChange(QueryModel<CommunicationConfig> queryModel)
    {
        loading = true;
        using var dbContext = dbContextFactory.CreateDbContext();
        var query = dbContext.CommunicationConfigs;
        communicationConfigs = 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;
    }
}