Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Kendo Grid Custom Filter

Basically, I am looking for the MVC version of this demo:

http://demos.telerik.com/kendo-ui/grid/filter-menu-customization

Here is what I currently have:

.Columns(columns =>
        {
            columns.Bound(e => e.ID)
                .Hidden();
            columns.Bound(e => e.SearchFunctionCode)
                .Hidden();
            columns.Bound(e => e.SearchFunctionDesc)                
                .Title("Search Function")
                .Filterable( *** WHAT GOES HERE? *** )
                .HtmlAttributes(new { style = "text-align: center" })
                .HeaderHtmlAttributes(new { style = "text-align: center" });

Do I still reference the javascript, or is there another approach?

<script type="text/javascript">
    function SearchFunctionFilter(element) {
        element.kendoDropDownList({
            dataSource: searchfunctions(),
            optionLabel: "--Select Value--"
        });
    }
</script>
like image 433
littleGreenDude Avatar asked May 29 '14 18:05

littleGreenDude


People also ask

How do I set the default filter in kendo grid?

Solution. On the dataBound event of the grid, find the filter dropdown and select() the desired default filter option. On the filter event of the Grid, if the filter is cleared, select the desired default filter option.

How do I add a search box in kendo grid?

To add a search box into a kendo grid we need to add Toolbar and search properties while initializing the kendo grid. Fields is an array of each field defined for the kendo grid. From the above snippet, the search will be applied for the name and age field of the grid.


2 Answers

Yes we need to define specified filter functions in javascript like below.

.Columns(columns => {
        columns.Template(@<text>@item.FirstName  @item.LastName</text>)
                .ClientTemplate("#=FirstName# #=LastName#")
                .Title("Name");
        columns.Bound(e => e.City)
                .Filterable(filterable => filterable.UI("cityFilter"))
                .Width(200);
        columns.Bound(e => e.Title)
                .Filterable(filterable => filterable.UI("titleFilter"))
                .Width(350);   
    })    
    .Filterable(filterable => filterable
        .Extra(false)
        .Operators(operators => operators
            .ForString(str => str.Clear()
                .StartsWith("Starts with")
                .IsEqualTo("Is equal to")
                .IsNotEqualTo("Is not equal to")
            ))
        )   
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("FilterMenuCustomization_Read", "Grid"))
     )
)

<script type="text/javascript">
    function cityFilter(element) {
        element.kendoDropDownList({
            dataSource: {
                transport: {
                    read: "@Url.Action("FilterMenuCustomization_Cities")"
                }
            },
            optionLabel: "--Select Value--"
        });
    }

    function titleFilter(element) {
        element.kendoAutoComplete({
            dataSource: {
                transport: {
                    read: "@Url.Action("FilterMenuCustomization_Titles")"
                }
            }
        });
    }
</script>

see this

http://demos.telerik.com/aspnet-mvc/grid/filter-menu-customization

like image 81
malkam Avatar answered Oct 04 '22 11:10

malkam


Malkan's answer should work. All you need to do is have a separate filter on each column. just replace the column "filterable" with whatever you like like this:

    .Filterable(filterable => filterable
    .Extra(false)
    .Operators(operators => operators
        .ForString(str => str.Clear()
            .StartsWith("Starts with")
            .IsEqualTo("Is equal to")
            .IsNotEqualTo("Is not equal to")
        ))
    )   
like image 42
John Lord Avatar answered Oct 04 '22 12:10

John Lord