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

<Flex Justify="space-between" Align="center">
    <Flex Justify="flex-start" Align="center" Gap="small">
        <AntDesign.Input DefaultValue="@("mysite")">
            <AddOnBefore>编号</AddOnBefore>
        </AntDesign.Input>
        <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Search">搜索</Button>
        <Button Type="@ButtonType.Default" Icon="@IconType.Outline.Redo">重置</Button>
    </Flex>
    <Flex Justify="flex-end" Align="center" Gap="small">
        <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.CloudUpload">新增</Button>
        <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Delete" Danger>批量删除</Button>
    </Flex>
</Flex>
<Table @ref="table"
       TItem="CommunicationConfig"
       DataSource="@communicationConfigs"
       Total="_total"
       @bind-PageIndex="_pageIndex"
       @bind-PageSize="_pageSize"
       @bind-SelectedRows="selectedRows"
       OnChange="OnChange"
       Loading="loading"
       Size="TableSize.Small"
       RowKey="x=>x.Id">
    <Selection Key="@(context.Id.ToString())" />
    <PropertyColumn Property="c=>c.Id" Hidden 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" />
    <ActionColumn Title="操作">
        <Space>
            <SpaceItem>
                <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Edit" Size="@ButtonSize.Small">修改</Button>
                <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Delete" Size="@ButtonSize.Small" Danger>删除</Button>
            </SpaceItem>
        </Space>
    </ActionColumn>
</Table>

@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;
    }
}