c# - Telerik Kendo MVC Grid - 设置自定义过滤

标签 c# asp.net-mvc kendo-grid telerik-grid fluent

我公司的产品大量使用 Telerik MVC 网格(超过 100 个带有网格的网页),我们正在尝试找到方法,通过从我们的类中设置这些部分来最大程度地减少自定义这些网格部分所需的工作量已调用“设置”。

这是一个网格示例。您可以看到我们已经成功地使用 .PageSize() 和 .Pageable(p.PageSizes()) 设置。但是,.Filterable(f.Operators()) 给我带来了麻烦,我希望有人能指出我做错了什么。

网格

@(Html.Kendo().Grid(Model)
    .Name("grid")
    .DataSource(ds => ds
        .Ajax()
        .Filter(filter => filter.Add(group => group.Active).IsEqualTo(true))
        .Model(model =>
        {
            model.Id(m => m.Id);
            model.Field(m => m.Id).DefaultValue(new Guid());
        })
        .PageSize(Settings.DefaultPageSize)  //WORKING
        .Read(read => read.Action("Read", "Users"))
        .Events(events => { events.Error("onDSErrors"); events.RequestEnd("onDSRequestEnd"); })
    )
    .Pageable(p => p
        .PageSizes(Settings.MvcGridPageSizes)  //WORKING
        .Refresh(true)
    )
    .Filterable(f => f
        .Extra(true)
        .Operators(Settings.MVCGridFilterOperators)  //NOT WORKING
    )
    .Columns(columns =>
    {
        columns.Bound(u => u.UserName)
            .Title("Username")
            .Width(200);
        ...
        ...
        ...
    })
)

设置类 (这是我想做的,但还不能开始工作...)

public static class Settings
{

    ...

    public static Action<FilterableOperatorsBuilder> MVCGridFilterOperators
    {
        get
        {
            FilterableOperators operators = new FilterableOperators();
            FilterableOperatorsBuilder builder = new FilterableOperatorsBuilder(operators);

            builder.ForString(x => x
                .Contains("Contains")
                .DoesNotContain("Does not contain")
                .StartsWith("Starts with")
                .EndsWith("Ends with")
                .IsEqualTo("Is equal to")
                .IsNotEqualTo("Is not equal to")
                .IsNull("Is null")
                .IsNotNull("Is not null")
            );

            return builder;
            //Error message I see when hovering over "return builder;":
            //   Cannot implicitly convert type ‘Kendo.Mvc.UI.Fluent.FilterableOperatorsBuilder’ to ‘System.Action<Kendo.Mvc.UI.Fluent.FilterableOperatorsBuilder>’
        }
    }

    ...

}

目前,如果需要任何更改,我们所有网格中的可过滤运算符部分都必须在 100 多个网格中的每一个中更新,因为它们看起来像这样:

.Operators(o => o
    .ForString(str => str
        .Clear()
        .Contains("Contains")
        .DoesNotContain("Does not contain")
        .StartsWith("Starts with")
        .EndsWith("Ends with")
        .IsEqualTo("Is equal to")
        .IsNotEqualTo("Is not equal to")
    )
)

由于我们使用了如此多的网格,因此更新它们需要一段时间,因为它需要对每个网格进行复制/粘贴或查找和替换。预先感谢您能为我提供的任何帮助!

最佳答案

经过一些研究,我发现这是可能的,并且为 kendo mvc 网格上的预设设置开辟了很多可能性。您需要创建一个与此类似的类文件:

public static class CustomGridHelperExtensions
{
    public static Kendo.Mvc.UI.Fluent.GridBuilder<T> MyGrid<T>(this HtmlHelper helper)
        where T : class
    {
        return helper.Kendo().Grid<T>()
            .Filterable(filterable => filterable
                .Extra(true)   //This extended "filterable" section of code makes the contains filter the default string filter for these grids
                .Operators(operators => operators
                    .ForString(str => str
                        .Clear()
                        .Contains("Contains")
                        .DoesNotContain("Does not contain")
                        .StartsWith("Starts with")
                        .EndsWith("Ends with")
                        .IsEqualTo("Is equal to")
                        .IsNotEqualTo("Is not equal to")
                        .IsNull("Is null")
                        .IsNotNull("Is not null")
                    )
                )
            );
    }

然后,代替设置网格的正常方法,您改为执行此操作

       @(Html.MyGrid<model>() 

                     .DataSource(ds => ds
                        .Ajax()
                        .Filter(filter => ...'

希望对您有所帮助。请注意,我从 telerek 的论坛中获得了一些信息。

关于c# - Telerik Kendo MVC Grid - 设置自定义过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42726199/

相关文章:

javascript - 在 Kendo UI 中将网格 pageSize 设置为变量

javascript - 仅在分层 Kendo Ui Grid 的列标题中显示垂直网格线

Jquery日期时间选择器日期格式问题

asp.net-mvc - 编辑器模板 LabelFor - 显示值而不是名称

kendo-ui - 如何在 Kendo Grid 的保存事件中撤消 Preventdefault

c# - 如何处理 GetDataPresent 让它接受所有派生类型

c# - 使用 Microsoft.Web.Administration : Invalid index. 在 IIS7 中以编程方式创建远程站点(HRESULT 异常:0x80070585)

c# - ASP.NET Web API 2 - 序列化后字符串中的双反斜杠

c# - 如何在 Windows 10 上调试丢失的 dll

asp.net-mvc - 使用 MVC 框架 RC1 更新断开连接的 LINQ 对象