使用Razor在Telerik MVC3网格中选择过滤器选项

时间:2012-01-27 00:21:00

标签: asp.net-mvc-3 razor filtering telerik-grid telerik-mvc

我正在玩Telerik MVC3扩展,尤其是Grid,可以看到here

我正在使用自定义分页,并对过滤选项感兴趣。

使用以下内容轻松地允许每列过滤:

.Columns(x =>
{
    x.Bound(col => col.Id).Filterable(false);

然而,当我启用它时,似乎我无法选择向用户显示哪些选项。无论在哪个领域,我都会在过滤器下拉列表中获得以下内容:

Filter Options

理想情况下,我只想要'包含' - 这可能吗?

4 个答案:

答案 0 :(得分:4)

我的解决方案是这样的:

Telerik GRID 代码:

columns.Bound(o => o.MRUCode).HeaderHtmlAttributes(new { @class = "FilterOnlyOn_Contains" });


JavaScript 代码:

$(".FilterOnlyOn_Contains .t-filter").click(function () {
    setTimeout(function () {
        // Remove all existing options and add 'Contains'
        var filterOptions1 = $(".t-filter-operator").first();
        filterOptions1.empty();
        filterOptions1.append($("<option />").val("substringof").text("Contains"));

        // Remove second part (options + text + input)
        var filterOptions2 = $(".t-filter-operator").last();
        filterOptions2.hide();
        $(".t-filter-help-text").text("");
        $("input[type=text]").last().hide();
    });
});


结果看起来像:
enter image description here

答案 1 :(得分:2)

Stef提供的答案是我们的应用程序中实现的答案,但我注意到它对于多列可过滤的情况不起作用。我对JS进行了一些小改动,允许使用Stef的解决方案进行多列过滤。基本上,它是相同的代码(所以所有赞成Stef)。它只需要通过“t-animation-container”进行分组(因为这是过滤器渲染的元素),并按照Stef的代码对页面进行单独操作。

$(".FilterOnlyOn_Contains .t-filter").click(function () {
    setTimeout(function () {

        var $animationContainers = $(".t-animation-container");
        $animationContainers.each(function () {
            // Remove all existing options and add 'Contains'
            var filterOptions1 = $(this).find(".t-filter-operator").first();
            filterOptions1.empty();
            filterOptions1.append($("<option />").val("substringof").text("Contains"));

            // Remove second part (options + text + input)
            var filterOptions2 = $(this).find(".t-filter-operator").last();
            filterOptions2.hide();
            $(".t-filter-help-text").text("");
            $("input[type=text]").last().hide();
        });
    });
});

在提出这个解决方案之后,我发现了另一个问题。它不再以“t-animation-container”为目标,这些“t-animation-container”连接到“FilterOnlyOn_Contains”过滤器。它影响所有过滤器,因此编辑我们不希望它的元素。我不知道这种关系是如何形成的,我想这里有一些来自telerik js文件的js dom引用。

答案 2 :(得分:1)

我不知道如何删除其他选项,但您可以通过将以下代码放在grid_load客户端事件中来将“contains”选项设置为默认项:

  $("#gridId").find(".t-filter").click(function () {
        setTimeout(function () {
            $(".t-filter-operator").each(function () {
                $(this).val("substringof");
            });
        });

我认为您可以通过在FireBug或Chrome开发者工具中跟踪上述代码轻松删除其他项目。

答案 3 :(得分:0)

如果有可能我怀疑你需要查看telerik.grid.filtering.js javascript文件并根据你的意愿进行编辑。