How to get the preview string?

4 posts, 0 answers
  1. Bruce
    Bruce avatar
    12 posts
    Member since:
    Oct 2010

    Posted 06 Nov 2010 Link to this post

    Hi,

    Does the RadFilter control provide any function to return the filter string? for example:

    (
    ShipCity
    = 'Irvine' AND OrderDate = '11/8/2010 12:00:00 AM')

    I tried SaveSettings but it returns a unreadable string.

    Additionally, if I have a string, how can I recreate the filter using my string? Is there any API I can use?

    Thanks and have a great day.
    Bruce
  2. Marin
    Admin
    Marin avatar
    1043 posts

    Posted 09 Nov 2010 Link to this post

    Hi Bruce,

    There is no property in the current version of RadFilter that returns the string representation of the filter clause. You can modify and get reference to the RadFilter expressions using RootGroup property and from there access the expressions, filter functions, values etc. for the filter control.
    string fieldName = RadFilter1.FieldEditors[0].FieldName; // name of the field that we are filtering on
                    RadFilterFunction filterFunction = RadFilter1.RootGroup.Expressions[0].FilterFunction; // the filter function that was used for filtering
                    RadFilterNonGroupExpression nonGroupExpression = RadFilter1.RootGroup.FindByFieldName(fieldName); 
                    RadFilterGreaterThanFilterExpression<int> expression = nonGroupExpression as RadFilterGreaterThanFilterExpression<int>;
                    int value = expression.Value; //the value that we filtered on (e.g. 10250)


    Kind regards,
    Marin
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. avdhut salvi
    avdhut salvi avatar
    2 posts
    Member since:
    May 2010

    Posted 13 Jan 2011 Link to this post

    I was looking for the similar functionality. It'll be great if its added in future releases.

    However, there is an easy workaround to this limitation. If you observe the generated HTML, filter query is displayed in div element having class "rfPreview".

    I've used a simple java-script to fetch the query & save it in server side hidden-field.
    function getFilterCtrlQuery() {
        var arrDiv = document.getElementsByTagName("div"); //query is displayed in div element having class as rfPreview
     
        if (arrDiv != null) {
             for (i = 0; i < arrDiv.length; i++) {
                if (arrDiv[i].className == 'rfPreview') {
                    document.getElementById('hdnQuery').value = arrDiv[i].innerText; } } }
        return true;
    }
  5. Brian Pratt
    Brian Pratt avatar
    28 posts
    Member since:
    Apr 2010

    Posted 17 Jun 2011 Link to this post

    This is not completely finished, but I was happy to get this far.  I still need to handle all FilterFunction states that do not have Values, but this captures most of them.  This would be applied on the ApplyExpression button click.

          /// <summary>
        /// when the RadFilter Apply Expression button is clicked
        /// </summary>
        protected void RadFilter1_ApplyExpressions(object sender, RadFilterApplyExpressionsEventArgs e)
        {
            HiddenField1.Value = "Apply";
            // this is the expression in string form (similar to the Preview String in html)
            string s = WriteExpressions(e.ExpressionRoot);
        }
     
        /// <summary>
        /// Uses reflection to get the property Value, since the expression could be of many different generic types. I always assume there is a value for now.
        /// This may not be the case depending on the FilterFunction picked, but it is a start.
        /// </summary>
        /// <param name="src">The object to the the property from</param>
        /// <param name="propName">The property name of the value you are looking for</param>
        /// <returns>the value of src.propName</returns>
        public static object GetPropValue(object src, string propName)
        {
            return src.GetType().GetProperty(propName).GetValue(src, null);
        }
     
        /// <summary>
        /// Recursive function to traverse expression groups.  You would normally start with e.ExpressionRoot.
        /// </summary>
        /// <param name="group">The expression group to start at</param>
        /// <returns>string representing this expression groups preview string </returns>
        private string WriteExpressions(RadFilterGroupExpression group)
        {
     
            string result = "("; // start this group
            // we need to know the last one to figure out if we should write the conjunction
            object last = group.Expressions[group.Expressions.Count - 1];
             
            // traverse the expressions
            foreach (var expression in group.Expressions)
            {
                if (expression is RadFilterNonGroupExpression)
                {
                    // this is a regular NAME == VALUE string
                    RadFilterNonGroupExpression nge = expression as RadFilterNonGroupExpression;
                    result += nge.FieldName;
                    result += " ";
                    result += nge.FilterFunction.ToString();
                    result += " ";
                    result += GetPropValue(nge, "Value").ToString();
                }
                else if (expression is RadFilterGroupExpression)
                {
                    // this is another group expression, recurse
                    result += WriteExpressions(expression as RadFilterGroupExpression);
                }
     
                // if it is not the last, print the conjunction
                if (expression != last)
                {
                    result += " ";
                    result += group.GroupOperation.ToString();
                    result += " ";
                }
            }
            result += ")"; // end this group
     
            return result;
        }


    Thanks,
    Brian
Back to Top