使用 Angular 6 ag-grid 和 ASP.NET 以及 EF Core 2.1 进行服务器端过滤

mar*_*iob 2 ag-grid asp.net-core ag-grid-ng2 ef-core-2.1

我正在尝试在 ag-grid(无限滚动模式)中实现服务器端过滤。

问题是 - 关于 filterModel 的文档非常晦涩,我正在慢慢发现使用 console.log 的事情,这变得令人沮丧,因为 filterModel 可以提供不同的信息,这也使得映射到服务器端类非常乏味。有没有人找到有关 filterModel 的正确文档?

另外,有没有人找到 ASP.NET Core 和 EF Core 的辅助方法来应用这个 filterModel?似乎有很多工作要涵盖所有可能的场景,而我目前的方法需要 System.DynamicLinq(不确定这是否是最佳解决方案)。

谢谢,马里奥

mar*_*iob 10

我已经整理好了,所以如果有人需要它,就在这里。

无限行模型需要我在 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);
                });


        }
    };
Run Code Online (Sandbox Code Playgroud)

然后 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);
}
Run Code Online (Sandbox Code Playgroud)

最后,在服务器端,它需要对 filterModel 和 sortModel 进行一些处理。下面的代码完全没有优化,是filterModel不同值的演示。例如,如果您在 ag-grid 中选择第二个逻辑运算符,JSON 会更改并包含具有 logicOperator 参数的条件 1 和条件 2 对象。此代码可能包含错误,因为我没有测试所有可能的组合。此外,代码使用 System.DynamicLinq。

        [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);
    }
Run Code Online (Sandbox Code Playgroud)


mar*_*iob 5

这是我用于网格的所有模型。

    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; }
}
Run Code Online (Sandbox Code Playgroud)