Custom Filtering Column

3 posts, 1 answers
  1. Marian
    Marian avatar
    49 posts
    Member since:
    Sep 2008

    Posted 13 Dec 2010 Link to this post

    Hi all,
    I am implementing custom radGrid column with datefrom, dateto and datebetween filtering. I derived my column from GridBoundColumn similar as described in article http://www.telerik.com/help/aspnet-ajax/grdimplementingfilteringfortemplatecolumns.html. My column uses two datepickers for filtering. I provide filtering in registered clientscript. CurrentFilterValue is set correct whenever SetCurrentFilterValueToControl is invoked. Only one problem is that I am not able to detect CurrentFilterFunction in overriden method. It becomes always value NoFilter. How can I detect CurrentFilterFunction?
    -Thanks
    protected override void SetCurrentFilterValueToControl(TableCell cell)
    {
        base.SetCurrentFilterValueToControl(cell);
        if (!string.IsNullOrWhiteSpace(this.CurrentFilterValue))
        {
            // this.CurrentFilterFuncion always returns NoFilter
            switch (this.CurrentFilterFuncion)
                case GridKnownFunction.Between:
                   // ...
                case GridKnownFunction.LessThanOrEqualTo:
                   // ...
                case GridKnownFunction.GreatherThanOrEqualTo:
                   // ...
        }
    }
  2. Answer
    Veli
    Admin
    Veli avatar
    2002 posts

    Posted 14 Dec 2010 Link to this post

    Hi Marian,

    Yes, this is because CurrentFilterValue is always set before setting CurrentFilterFunction. SetCurrentFilterValueToControl() is called after setting the filter value but before setting the filter function. This is why CurrentFilterValue always has the default value of NoFilter in this method call.

    Veli
    the Telerik team
    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Marian
    Marian avatar
    49 posts
    Member since:
    Sep 2008

    Posted 14 Dec 2010 Link to this post

    Hello Veli,
    thank you for your answer. I solved my problem by using small trick in javascript. So my custom filter column need no CurrentFilterFunction. Bellow is my code. It works fine for me.  Maybe it will be helpful for someone.
    -Marian
    public partial class DateFilteringColumn : GridBoundColumn
        {
            public override string DataField
            {
                get
                {
                    object res = this.ViewState["_datafield"];
                    if (res != null)
                    {
                        return (string)res;
                    }
                    return String.Empty;
                }
                set
                {
                    this.ViewState["_datafield"] = value;
                }
            }
      
            protected override void SetupFilterControls(System.Web.UI.WebControls.TableCell cell)
            {
                base.SetupFilterControls(cell);
                  
                // SetUp custom behaviors of DatePickers (or expose them as Column Properties)
                RadDatePicker dpStart = new RadDatePicker();
                dpStart.Width = Unit.Pixel(90);
                dpStart.MinDate = DateTime.MinValue;
                dpStart.MaxDate = DateTime.MaxValue;
                dpStart.DateInput.DateFormat = "dd.MM.yyyy";
                dpStart.DateInput.EnabledStyle.HorizontalAlign = HorizontalAlign.Center;
                dpStart.Calendar.CultureInfo = new CultureInfo("sk-SK");
                dpStart.ClientEvents.OnDateSelected = "StartDateSelected_" + this.UniqueName;
      
                RadDatePicker dpEnd = new RadDatePicker();
                dpEnd.Width = Unit.Pixel(90);
                dpEnd.MinDate = DateTime.MinValue;
                dpEnd.MaxDate = DateTime.MaxValue;
                dpEnd.DateInput.DateFormat = "dd.MM.yyyy";
                dpEnd.DateInput.EnabledStyle.HorizontalAlign = HorizontalAlign.Center;
                dpEnd.Calendar.CultureInfo = new CultureInfo("sk-SK");
                dpEnd.ClientEvents.OnDateSelected = "EndDateSelected_" + this.UniqueName;
      
                cell.Controls.Clear();
                cell.Controls.AddAt(0, new LiteralControl(">= "));
                cell.Controls.AddAt(1, dpStart);
                cell.Controls.AddAt(2, new LiteralControl("<br /><= "));
                cell.Controls.AddAt(3, dpEnd);
      
                ClientScript(dpStart.ClientID, dpEnd.ClientID);
            }
      
            private void ClientScript(string StartClientID, string EndClientID)
            {
                StringBuilder sb = new StringBuilder();
                 
                sb.Append("function StartDateSelected_" + this.UniqueName + "(sender, args) {");
                sb.Append("    var EndPicker = $find('" + EndClientID + "');");
                sb.Append("    var startDate = sender.get_dateInput().get_selectedDate();");
                sb.Append("    var endDate = EndPicker.get_dateInput().get_selectedDate();");
                sb.Append("    SetFilter('" + this.UniqueName + "', startDate, endDate);");
                sb.Append("}");
                sb.Append("function EndDateSelected_" + this.UniqueName + "(sender, args) {");
                sb.Append("    var StartPicker = $find('" + StartClientID + "');");
                sb.Append("    var startDate = StartPicker.get_dateInput().get_selectedDate();");
                sb.Append("    var endDate = sender.get_dateInput().get_selectedDate();");
                sb.Append("    SetFilter('" + this.UniqueName + "', startDate, endDate);");
                sb.Append("}");
      
                this.Owner.OwnerGrid.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "FilterScript_" + this.UniqueName, sb.ToString(), true);
                if (!this.Owner.OwnerGrid.Page.ClientScript.IsClientScriptBlockRegistered("FilterScript_Common"))
                {
                    sb.Clear();
                    sb.Append("function FormatDateTime(date, endofday) {");
                    sb.Append("    if (date != null) {");
                    sb.Append("        return endofday ? date.format(\"yyyy-MM-dd'T'23:59:59.999\") : date.format(\"yyyy-MM-dd't'00:00:00.000\");"); // Trick with T
                    sb.Append("    }");
                    sb.Append("    return null;");
                    sb.Append("}");
                    sb.Append("function SetFilter(column, fromDate, toDate) {");
                    sb.Append("    var tableView = $find('" + this.Owner.OwnerGrid.MasterTableView.ClientID + "');");
                    sb.Append("    if (fromDate == null && toDate == null) {");
                    sb.Append("       tableView.filter(column, '', Telerik.Web.UI.GridFilterFunction.NoFilter);");
                    sb.Append("    }");
                    sb.Append("    else if (fromDate != null && toDate == null) {");
                    sb.Append("       tableView.filter(column, FormatDateTime(fromDate, false), Telerik.Web.UI.GridFilterFunction.GreaterThanOrEqualTo);");
                    sb.Append("    }");
                    sb.Append("    else if (fromDate == null && toDate != null) {");
                    sb.Append("       tableView.filter(column, FormatDateTime(toDate, true), Telerik.Web.UI.GridFilterFunction.LessThanOrEqualTo);");
                    sb.Append("    }");
                    sb.Append("    else if (fromDate != null && toDate != null) {");
                    sb.Append("       tableView.filter(column, FormatDateTime(fromDate, false) + ' ' + FormatDateTime(toDate, true), Telerik.Web.UI.GridFilterFunction.Between);");
                    sb.Append("    }");
                    sb.Append("}");
                    this.Owner.OwnerGrid.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "FilterScript_Common", sb.ToString(), true);
                }
            }
      
            public override GridColumn Clone()
            {
                return base.Clone();
            }
      
            protected override string GetFilterDataField()
            {
                return this.DataField;
            }
      
            protected override void SetCurrentFilterValueToControl(TableCell cell)
            {
                base.SetCurrentFilterValueToControl(cell);
      
                if (!string.IsNullOrWhiteSpace(this.CurrentFilterValue))
                {
                    #region Trick with T
                    // I need to discover CurrentFilterFunction, because always NoFilter is returned
                    if (this.CurrentFilterValue.Contains("t") && this.CurrentFilterValue.Contains("T"))
                        this.CurrentFilterFunction = GridKnownFunction.Between;
                    else if (this.CurrentFilterValue.Contains("t"))
                        this.CurrentFilterFunction = GridKnownFunction.GreaterThanOrEqualTo;
                    else if (this.CurrentFilterValue.Contains("T"))
                        this.CurrentFilterFunction = GridKnownFunction.LessThanOrEqualTo;
                    #endregion
      
                    RadDatePicker dpStart = cell.Controls[1] as RadDatePicker;
                    RadDatePicker dpEnd = cell.Controls[3] as RadDatePicker;
      
                    switch (this.CurrentFilterFunction)
                    {
                        case GridKnownFunction.Between:
                            dpStart.DbSelectedDate = DateTime.Parse(this.CurrentFilterValue.Split(' ')[0]);
                            dpEnd.DbSelectedDate = DateTime.Parse(this.CurrentFilterValue.Split(' ')[1]);
                            break;
                        case GridKnownFunction.GreaterThanOrEqualTo:
                            dpStart.DbSelectedDate = DateTime.Parse(this.CurrentFilterValue);
                            dpEnd.DbSelectedDate = null;
                            break;
                        case GridKnownFunction.LessThanOrEqualTo:
                            dpStart.DbSelectedDate = null;
                            dpEnd.DbSelectedDate = DateTime.Parse(this.CurrentFilterValue);
                            break;
                        default:
                            break;
                    }
                }
            }
      
            protected override string GetCurrentFilterValueFromControl(TableCell cell)
            {
                RadDatePicker dpStart = cell.Controls[1] as RadDatePicker;
                RadDatePicker dpEnd = cell.Controls[3] as RadDatePicker;
                  
                if (dpStart.DbSelectedDate == null && dpEnd.DbSelectedDate == null)
                    return string.Empty;
                else if (dpStart.DbSelectedDate != null && dpEnd.DbSelectedDate == null)
                    return GetFilterString(dpStart, false);
                else if (dpStart.DbSelectedDate == null && dpEnd.DbSelectedDate != null)
                    return GetFilterString(dpEnd, true);
                else
                    return GetFilterString(dpStart, false) + " " + GetFilterString(dpEnd, true);
            }
      
            private string GetFilterString(RadDatePicker dp, bool endofday)
            {
                DateTime dt = (DateTime)dp.DbSelectedDate;
                if (endofday)
                {
                    dt = dt.AddMilliseconds(-1);
                    dt = dt.AddDays(1);
                }
                return dt.ToString(string.Format("yyyy-MM-dd'{0}'HH:mm:ss", endofday ? "T" : "t"));
            }
      
            public override bool SupportsFiltering()
            {
                return true;
            }
        }

    Markup:

    <!-- Bound to sql2008 date type -->
    <MM:DateFilteringColumn DataField="DateOfBirth" DataFormatString="{0:dd.MM.yyyy}" DataType="System.DateTime" HeaderText="Date Of Birth" SortExpression="DateOfBirth" UniqueName="DateOfBirth" />
      
    <!-- Bound to sql2008 datetime type -->
    <MM:DateFilteringColumn DataField="CreatedDateTime" DataFormatString="{0:dd.MM.yyyy HH:mm}" DataType="System.DateTime" HeaderText="Record created" SortExpression="CreatedDateTime" UniqueName="CreatedDateTime" />
Back to Top