WorkpieceModelPage.razor
6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
@page "/Basic/WorkpieceModel"
@using System.Text.Json;
@using AntDesign.TableModels
@using System.ComponentModel
@using DataAcquisition.DataAccess
@using DataAcquisition.Models
@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<DataContext> dbContextFactory;
@inject ModalService _modalService
@inject IMessageService _message
<Flex Justify="flex-end" Align="center">
<Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Plus" OnClick="@(()=>{ _visible_add = true; })">新增</Button>
<Button Danger Type="@ButtonType.Primary" Icon="@IconType.Outline.Delete" OnClick="BatchDelete">删除</Button>
</Flex>
<Table @ref="table"
TItem="WorkpieceModel"
Loading="loading"
DataSource="@dataItems"
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" Hidden />
<PropertyColumn Title="编号" Property="c=>c.Code" />
<PropertyColumn Title="名称" Property="c=>c.Name" />
<PropertyColumn Title="标识" Property="c=>c.Flag" />
<PropertyColumn Title="备注" Property="c=>c.Remark" />
<ActionColumn Title="操作">
<Space>
<SpaceItem>
<Button Type="@ButtonType.Primary"
Icon="@IconType.Outline.Edit"
Size="@ButtonSize.Small"
OnClick="()=>OpenEditMode(context.Id)">
编辑
</Button>
</SpaceItem>
</Space>
</ActionColumn>
</Table>
<Modal Title="新增" @bind-Visible="@_visible_add" OkText="@("保存")" OnOk="AddActionSaveChange">
<Form Model="@model">
<FormItem Label="编码">
<Input @bind-Value="@context.Code" />
</FormItem>
<FormItem Label="名称">
<Input @bind-Value="@context.Name" />
</FormItem>
<FormItem Label="标识">
<Input @bind-Value="@context.Flag" />
</FormItem>
<FormItem Label="备注">
<Input @bind-Value="@context.Remark" />
</FormItem>
</Form>
</Modal>
<Modal Title="编辑" @bind-Visible="@_visible_edit" OkText="@("保存")" OnOk="EditActionSaveChange">
<Form Model="@editModel">
<FormItem Label="编码">
<Input @bind-Value="@context.Code" />
</FormItem>
<FormItem Label="名称">
<Input @bind-Value="@context.Name" />
</FormItem>
<FormItem Label="标识">
<Input @bind-Value="@context.Flag" />
</FormItem>
<FormItem Label="备注">
<Input @bind-Value="@context.Remark" />
</FormItem>
</Form>
</Modal>
@code {
bool loading = true;
bool _visible_add = false;
bool _visible_edit = false;
private WorkpieceModel model = new WorkpieceModel();
private WorkpieceModel editModel = new WorkpieceModel();
List<WorkpieceModel> dataItems = new List<WorkpieceModel>();
IEnumerable<WorkpieceModel> selectedRows = new List<WorkpieceModel>();
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;
}
public void AddActionSaveChange()
{
try
{
using var dbContext = dbContextFactory.CreateDbContext();
var item = model;
dbContext.WorkpieceModels.Add(item);
dbContext.SaveChanges();
_message.Success($"新增成功!");
table.ReloadData();
model = new WorkpieceModel();
}
catch (Exception ex)
{
_message.Error($"操作失败,{ex.Message}");
}
}
public void OpenEditMode(int id)
{
try
{
var dbContext = dbContextFactory.CreateDbContext();
editModel = dbContext.WorkpieceModels.Where(x => x.Id == id).First();
_visible_edit = true;
}
catch (Exception ex)
{
_message.Error($"操作失败,{ex.Message}");
_visible_edit = false;
}
}
public void EditActionSaveChange()
{
try
{
var dbContext = dbContextFactory.CreateDbContext();
dbContext.WorkpieceModels.Update(editModel);
dbContext.SaveChanges();
_message.Success($"修改数据成功");
table.ReloadData();
}
catch (Exception ex)
{
_message.Error($"操作失败,{ex.Message}");
}
}
public void BatchDelete()
{
if (!selectedRows.Any())
{
_message.Warning($"请勾选需删除的数据后再进行操作!");
return;
}
_modalService.Confirm(new ConfirmOptions
{
Title = "确定删除选中的数据?",
Icon = icon,
// Content = "Some descriptions",
OnOk = (e) =>
{
try
{
using var dbContext = dbContextFactory.CreateDbContext();
var ids = selectedRows.Select(x => x.Id).ToList();
var count = dbContext.WorkpieceModels.Where(x => ids.Contains(x.Id)).ExecuteDelete();
_message.Success($"成功删除{count}条数据");
table.ReloadData();
}
catch (Exception ex)
{
_message.Success($"操作失败,{ex.Message}");
}
return Task.CompletedTask;
},
OkType = "danger",
});
}
Func<ModalClosingEventArgs, Task> onOk = (e) =>
{
Console.WriteLine("Ok");
return Task.CompletedTask;
};
Func<ModalClosingEventArgs, Task> onCancel = (e) =>
{
Console.WriteLine("Cancel");
return Task.CompletedTask;
};
RenderFragment icon = @<Icon Type="exclamation-circle" Theme="outline"></Icon>;
}