【问题标题】:Server-side filtering with Angular 6 ag-grid and ASP.NET and EF Core 2.1使用 Angular 6 ag-grid 和 ASP.NET 和 EF Core 2.1 进行服务器端过滤
【发布时间】:2019-02-22 17:03:29
【问题描述】:

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

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

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

谢谢, 马里奥

【问题讨论】:

    标签: asp.net-core ag-grid ag-grid-ng2 ef-core-2.1


    【解决方案1】:

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

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

    最后,在服务器端需要对 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);
        }
    

    【讨论】:

    • 你能分享你的 GridOperationsModel 吗?
    • @ArielMoraes - 抱歉耽搁了,我为我使用的所有模型添加了代码
    • 这对我帮助很大。您是否尝试过分组和旋转?
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      由于 Ag-grid Official docs 没有给我们任何 ASP.NET Core 的示例,所以我想我可以在 Dotnet 开发人员的支持下做一个示例。

      这是我的Github

      【讨论】:

        猜你喜欢
        • 2022-09-28
        • 2022-10-17
        • 2021-02-25
        • 2020-04-11
        • 2019-04-02
        • 2021-02-13
        • 2020-02-26
        • 2019-12-28
        • 2019-03-17
        相关资源
        最近更新 更多