Asp.net core 使用Angular 6 ag网格、ASP.NET和EF Core 2.1进行服务器端过滤
我试图在ag网格(无限滚动模式)中实现服务器端过滤 问题是-关于filterModel的文档非常模糊,我正在慢慢地使用console.log发现一些问题,这让人感到沮丧,因为filterModel可以提供不同的信息,这也使得映射到服务器端类非常繁琐。 有人找到关于filterModel的正确文档吗 另外,是否有人找到ASP.NET Core和EF Core的帮助器方法来应用此filterModel? 覆盖所有可能的场景似乎需要做很多工作,我目前的方法需要System.DynamicIQ(不确定这是否是最佳解决方案) 谢谢,Asp.net core 使用Angular 6 ag网格、ASP.NET和EF Core 2.1进行服务器端过滤,asp.net-core,ag-grid,ag-grid-ng2,ef-core-2.1,Asp.net Core,Ag Grid,Ag Grid Ng2,Ef Core 2.1,我试图在ag网格(无限滚动模式)中实现服务器端过滤 问题是-关于filterModel的文档非常模糊,我正在慢慢地使用console.log发现一些问题,这让人感到沮丧,因为filterModel可以提供不同的信息,这也使得映射到服务器端类非常繁琐。 有人找到关于filterModel的正确文档吗 另外,是否有人找到ASP.NET Core和EF Core的帮助器方法来应用此filterModel? 覆盖所有可能的场景似乎需要做很多工作,我目前的方法需要System.DynamicIQ(不确定这
马里奥我把它整理好了,如果有人需要,就在这里 无限行模型需要我在onGridReady事件中定义的数据源,如下所示:
const dataSource = {
rowCount: null,
getRows: (params) => {
this.svc.GetDrivingData(params.startRow, params.endRow, params.sortModel, params.filterModel)
.subscribe((d) => {
// console.log(JSON.stringify(d, null, 4));
params.successCallback(d, null);
});
}
};
然后GetDrivingData调用Web Api:
GetDrivingData(startRow: number, endRow: number,
sortModel: any, filterModel: any): Observable<DrivingData[]>
{
const body = {
startRow,
endRow,
sortModel,
filterModel
};
return this.httpClient.post<DrivingData[]>(`${this.baseUrl}/api/carfleet/DrivingDataPocoLo/GetDrivingData`, body);
}
GetDrivingData(开始行:编号,结束行:编号,
sortModel:any,filterModel:any):可观察
{
常数体={
斯塔特罗,
endRow,
sortModel,
过滤器模型
};
返回这个.httpClient.post(`${this.baseUrl}/api/carfleet/drivingtatapocolo/GetDrivingData`,body);
}
最后,在服务器端需要对filterModel和sortModel进行一些处理。
下面的代码根本没有经过优化,它演示了filterModel的不同值。
例如,如果在ag网格中选择第二个逻辑运算符,JSON将发生变化,并包含带有logicOperator参数的condition1和condition2对象。
这段代码可能包含bug,因为我没有测试所有可能的组合。
此外,代码使用System.DynamicIQ
[HttpPost("[action]")]
public IActionResult GetDrivingData([FromBody] GridOperationsModel gom)
{
var query = ctx.DrivingData.AsQueryable();
Func<string, FilterModel, List<object>, string> getConditionFromModel =
(string colName, FilterModel model, List<object> values) =>
{
string modelResult = "";
switch (model.filterType)
{
case "text":
switch (model.type)
{
case "equals":
modelResult = $"{colName} = \"{model.filter}\"";
break;
case "notEqual":
modelResult = $"{colName} = \"{model.filter}\"";
break;
case "contains":
modelResult = $"{colName}.Contains(@{values.Count})";
values.Add(model.filter);
break;
case "notContains":
modelResult = $"!{colName}.Contains(@{values.Count})";
values.Add(model.filter);
break;
case "startsWith":
modelResult = $"{colName}.StartsWith(@{values.Count})";
values.Add(model.filter);
break;
case "endsWith":
modelResult = $"!{colName}.StartsWith(@{values.Count})";
values.Add(model.filter);
break;
}
break;
case "number":
switch (model.type)
{
case "equals":
modelResult = $"{colName} = {model.filter}";
break;
case "notEqual":
modelResult = $"{colName} <> {model.filter}";
break;
case "lessThan":
modelResult = $"{colName} < {model.filter}";
break;
case "lessThanOrEqual":
modelResult = $"{colName} <= {model.filter}";
break;
case "greaterThan":
modelResult = $"{colName} > {model.filter}";
break;
case "greaterThanOrEqual":
modelResult = $"{colName} >= {model.filter}";
break;
case "inRange":
modelResult = $"({colName} >= {model.filter} AND {colName} <= {model.filterTo})";
break;
}
break;
case "date":
values.Add(model.dateFrom);
switch (model.type)
{
case "equals":
modelResult = $"{colName} = @{values.Count - 1}";
break;
case "notEqual":
modelResult = $"{colName} <> @{values.Count - 1}";
break;
case "lessThan":
modelResult = $"{colName} < @{values.Count - 1}";
break;
case "lessThanOrEqual":
modelResult = $"{colName} <= @{values.Count - 1}";
break;
case "greaterThan":
modelResult = $"{colName} > @{values.Count - 1}";
break;
case "greaterThanOrEqual":
modelResult = $"{colName} >= @{values.Count - 1}";
break;
case "inRange":
values.Add(model.dateTo);
modelResult = $"({colName} >= @{values.Count - 2} AND {colName} <= @{values.Count - 1})";
break;
}
break;
}
return modelResult;
};
foreach (var f in gom.filterModel)
{
string condition, tmp;
List<object> conditionValues = new List<object>();
if (!string.IsNullOrWhiteSpace(f.Value.logicOperator))
{
tmp = getConditionFromModel(f.Key, f.Value.condition1, conditionValues);
condition = tmp;
tmp = getConditionFromModel(f.Key, f.Value.condition2, conditionValues);
condition = $"{condition} {f.Value.logicOperator} {tmp}";
}
else
{
tmp = getConditionFromModel(f.Key, f.Value, conditionValues);
condition = tmp;
}
if (conditionValues.Count == 0) query = query.Where(condition);
else query = query.Where(condition, conditionValues.ToArray());
}
foreach (var s in gom.sortModel)
{
switch (s.sort)
{
case "asc":
query = query.OrderBy(s.colId);
break;
case "desc":
query = query.OrderBy($"{s.colId} descending");
break;
};
};
if (gom.sortModel.Count() == 0)
{
query = query.OrderBy(x => x.Oid);
}
query = query
.Include(dd => dd.CarNavigation)
.Include(dd => dd.DriverNavigation)
.Skip(gom.startRow)
.Take(gom.endRow - gom.startRow);
var result = query
.AsNoTracking()
.ToArray();
return Ok(result);
}
[HttpPost(“[action]”)
公共IActionResult GetDrivingData([FromBody]GridOperationsModel gom)
{
var query=ctx.DrivingData.AsQueryable();
Func getConditionFromModel=
(字符串colName、FilterModel模型、列表值)=>
{
字符串modelResult=“”;
开关(型号filterType)
{
案例“文本”:
开关(型号)
{
案例“等于”:
modelResult=$“{colName}=\”{model.filter}\”;
打破
案例“notEqual”:
modelResult=$“{colName}=\”{model.filter}\”;
打破
案例“包含”:
modelResult=$“{colName}.Contains(@{values.Count})”;
添加(model.filter);
打破
案例“notContains”:
modelResult=$“!{colName}.Contains(@{values.Count})”;
添加(model.filter);
打破
案例“startsWith”:
modelResult=$“{colName}.StartsWith(@{values.Count})”;
添加(model.filter);
打破
案例“endsWith”:
modelResult=$“!{colName}.StartsWith(@{values.Count})”;
添加(model.filter);
打破
}
打破
案例“编号”:
开关(型号)
{
案例“等于”:
modelResult=$“{colName}={model.filter}”;
打破
案例“notEqual”:
modelResult=$“{colName}{model.filter}”;
打破
“lessThan”案:
modelResult=$“{colName}<{model.filter}”;
打破
案例“LessthanRequal”:
modelResult=$“{colName}{model.filter}”;
打破
“大于或等于”案例:
modelResult=$“{colName}>={model.filter}”;
打破
案例“inRange”:
modelResult=$“({colName}>={model.filter}和{colName}=@{values.Count-1}”;
打破
案例“inRange”:
添加(model.dateTo);
modelResult=$”({colName}>=@{values.Count-2}和{colName}这里是我用于网格的所有模型
public class SortModel
{
public string colId { get; set; }
public string sort { get; set; }
}
public class FilterModel
{
public FilterModel condition1 { get; set; }
public FilterModel condition2 { get; set; }
[JsonProperty("operator")]
public string logicOperator { get; set; }
public string type { get; set; }
public string filter { get; set; }
public string filterTo { get; set; }
public DateTime? dateFrom { get; set; }
public DateTime? dateTo { get; set; }
public string filterType { get; set; }
}
public class GridOperationsModel
{
public int startRow { get; set; }
public int endRow { get; set; }
public SortModel[] sortModel { get; set; }
public Dictionary<string, FilterModel> filterModel { get; set; }
}
公共类排序模型
{
公共字符串colId{get;set;}
公共字符串排序{get;set;}
}
公共类过滤器模型
{
公共筛选器模型条件1{get;set;}
公共筛选器模型条件2{get;set;}
[JsonProperty(“运营商”)]
公共字符串逻辑运算符{get;set;}
公共字符串类型{get;set;}
公共字符串筛选器{get;set;}
公共字符串筛选器到{get;set;}
公共日期时间?dateFrom{get;set;}
公共日期时间?日期到{get;set;}
公共字符串筛选器类型{get;set;}
}
公共类GridOperationsModel
{
public int startRow{get;set;}
public int endRow{get;set;}
公共排序模型[]排序模型{get;set;}
公共字典筛选器模型{get;set;}
}
你能分享你的GridOperationsModel吗?@ArielMoraes-很抱歉耽搁了,我为我使用的所有模型添加了代码。这对我帮助很大。你尝试过分组和旋转吗?