Telerik Kendo MVC网格 - 设置自定义过滤

时间:2017-03-10 19:22:27

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

我公司的产品大量使用Telerik MVC网格(超过100个带网格的网页),我们正在尝试通过设置这些网格来最小化自定义部分网格所需的工作量。我们称之为设置。

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

GRID

@(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")
    )
)

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

1 个答案:

答案 0 :(得分:0)

经过一些研究,我发现这是可能的,并且为在剑道mvc网格上预先设置设置开辟了很多可能性。您需要创建一个类似于此的类文件:public static class CustomGridHelperExtensions { public static Kendo.Mvc.UI.Fluent.GridBuilder<T> MyGrid<T>(this HtmlHelper helper, string name) 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>("grid") //"grid" is name of grid.  You could leave this out
                     .DataSource(ds => ds
                        .Ajax()
                        .Filter(filter => ...'

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