Simplified filter declaration

Mar 15, 2013 at 9:35 PM
Is there a way to add a filter without having to include
1) A checkbox for the filter
2) A select list for the filter condition (e.g. contains)
3) A text box for the filter
4) An on/off initializer for the filter.

Assuming I always want to perform a "contains" filter condition on a certain field, I want to turn this code:

<div id="auditLogsGridFilterContainer">
                        @{
                            var hName = Html.DataFilterClauseFor(
                                m => m.AuditLogFilter, fieldExpression,
                                "byNameFilter",
                                MVCControlsToolkit.Linq.FilterCondition.Contains);
                        }
                        @hName.CheckBoxFor(m => m.Selected,
                                           new {@class = "byNameFilter_checkbox"})
                        @*&nbsp; Filter by name *@
    
                        @*<span class="byNameFilter">*@

                        @hName.FilterClauseSelect(
                                hName.ViewData.Model.Condition,
                                expression,
                                conditions:
                                    CanSortAttribute.AllowedForProperty(typeof (AdmAuditLogModel),
                                                                        "PatientName") |
                                    FilterCondition.Contains)
                            @hName.TypedTextBoxFor(m => m.Search, watermarkCss: "watermark")

                        @*</span>*@
                        @hName.ViewsOnOff("byNameFilter", true)
                    </div>
Into something like this:
@Html.DataFilterTextBoxFor(
    m => m.AuditLogFilter, fieldExpression,
    MVCControlsToolkit.Linq.FilterCondition.Contains);
Coordinator
Mar 16, 2013 at 10:47 AM
A simple solution is simply substituting the checkbox with an hidden field with the 'True' value inside it, and doing a similar thing for the filter operator. However this is just a "dirty trick".
Obviously there are more general ways to define filters the way you prefer. The DataFilterClauseFor was designed specifically to allow the user to select the filter conditions to apply and which operator to apply.
If you use an ODataQueryable to send filtercondition to the user then you can simply use the jsQueryable fluent Api to define you filter in javascript by taking the search values from any textbox you have in the page.
If, instead, you just submit the form. you may replace the DataFilterClauseFor with the more general and flexible DataFilterBuilder helper that allows to build a filter in exactly the way you prefer. For more infos see the secon half this tutorial(the first half is about the DataFilterClauseFor helper): Advanced Data Filtering Techniques in the Mvc Controls Toolkit
Mar 18, 2013 at 6:11 PM
Do you have an example of using the "jsQueryable fluent Api to define you filter in javascript by taking the search values from any textbox you have in the page"? This is the route I'll have to go because my site uses Twitter Bootstrap, so the jQuery UI date picker MVCCT uses doesn't look right.
Coordinator
Mar 18, 2013 at 6:48 PM
If you tell me from what of my example project you took your code I will show you how to use it in your settings. I suppose you are using OData Queryable to page, filter, and sort data, so probably your solution already creates and uses an ODataQueryable to do its job.
Basically, when a ne paging, sorting or filtering operation is triggered the ODataQueryable imports all pager, sorter and filter settings. Now you have to chanege just the way filtering infos are imported and to substitute the _importClauses(filterId) method call with several addCondition(operator, value1, value2, enabled) , one for each search condition, where operator is the searc operator to be uses(see the link for alist of all available operators) value1 is the item property to be constrained by the filter (expressed as a string) and value2 the value that will constrain it.
Something like this queryable.resetFilter().addCondition(mvcct.$$.startswith, 'name', 'joh')........
The way to attach this to your code depends...on your code. However, consider that before submitting any operation to the server you have to "load" on the queryable all paging, sorting and filter information.