How to: Make the FullTextSearch Action Search Within Required Properties How to: Use the full text search full-text search button in the properties needed in

This topic demonstrates how to customize the FullTextSearch Action's behavior. This Action filters the current List View by setting criteria for its collection data source. According to the criteria, the object's properties must contain individual words from the word combination typed by an end-user. Reference several techniques on how to modify the Action's behavior, in the FilterController.FullTextFilterAction member description. Here, you will see how to use one of these techniques. We will specify a custom list of the properties that will be used to generate the filter criterion for the current List View.

This topic shows how to customize the behavior of full-text search operation. This operates to filter the current list view by setting conditions for a set of data sources. Depending on conditions, attributes of the objects must contain a single word the end user typed a word combination. In .FullTextFilterAction members described filter controller, cited several techniques on how to modify the operating behavior. Here you will see how to use one of these technologies. We will specify the current will be used to generate a custom list of attributes filter criteria list view.

Tip Tip
A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E231
Complete sample project can be found in the code sample database DevExpress, http://www.devexpress.com/example=E231

.

To specify a custom list of the properties that will be used in the search, the Filter Controller, which contains the FullTextSearch Action, exposes the FilterController.CustomGetFullTextSearchProperties event. To handle this event, we will add a new View Controller, and subscribe to its Controller.Activated event.

To specify the attributes to be used in a custom list search, the controller comprises a filter "full text search" operation to filter controller is disclosed. To handle this event, we will add a new view controller, and subscribe to its controller. Activate event.

 

In the CustomGetFullTextSearchProperties event handler, assign a list of any required properties to the CustomGetFullTextSearchPropertiesEventArgs.Properties parameter. In addition, set the CustomGetFullTextSearchPropertiesEventArgs.Handled parameter to true, to indicate that other properties must not be added to the list. In this example, we will add only the "LastName" property of the Person class. So, we will activate our Controller for Person List Views only. The MyFilterController is shown below:

In the "Custom Full text search Properties" event handler, a list of any required attributes assigned to the "Custom Full text search attribute event" parameter. In addition, the "Custom Full text search property of the event" parameter set to true, to indicate that will not add additional properties to the list. In this example, we will only add to "name" attribute of the Person class. Therefore, we will only activate the list of people view controller. "My Filter Controller" as follows:

using System;
using System.Collections.Generic;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.SystemModule;
//...
public partial class MyFilterController : ViewController {
    public MyFilterController() {
        InitializeComponent();
        RegisterActions(components);
        this.TargetObjectType = typeof(DevExpress.Persistent.BaseImpl.Person);
    }
    private void MyFilterController_Activated(object sender, EventArgs e) {
        FilterController standardFilterController = Frame.GetController<FilterController>();
        if(standardFilterController != null) {
            standardFilterController.CustomGetFullTextSearchProperties += new 
EventHandler<CustomGetFullTextSearchPropertiesEventArgs>(standardFilterController_CustomGetFullTextSearchProperties);
        }
    }
    void standardFilterController_CustomGetFullTextSearchProperties(object sender, 
CustomGetFullTextSearchPropertiesEventArgs e) {
        foreach(string property in GetFullTextSearchProperties()) {
            e.Properties.Add(property);
        }
        e.Handled = true;
    }
    private List<string> GetFullTextSearchProperties() {
        List<string> searchProperties = new List<string>();
        searchProperties.Add("LastName");
        return searchProperties;
    }
}

 

The following image demonstrate that the MyFilterController works:

The following figure illustrates the principle of "My filter controller":

FilterByTextAction_CustomGetFullTextSearchProperties

Filtration may be impossible in case one or more property is of a particular type. For example, in Server mode, filtering by properties of the DateTimetype, which are stored in a database as columns of the datetime date type, may cause exceptions. To avoid them, ensure that the inputed text can be converted to the corresponding type and this converted value is within the acceptable range, and exclude a property from the default filter list if necessary. The following code demonstrates the Controller implementing this logic.

If one or more attributes of a particular type, the filter may not be possible. For example, in a server mode, filter press DateTimetype properties (attributes such as the date of the date column type stored in the database) may cause an exception. To avoid them, ensure that the text input may be converted to the corresponding type, and this conversion value is within the acceptable range, and if necessary, from the default list of filters to exclude properties. The following code demonstrates this logic controllers.

using System;
using System.Collections.Generic;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp.DC;
using DevExpress.Data.Summary;
using System.Data.SqlTypes;
//...
public class MyFilterController : ViewController<ListView> {
    private static readonly DateTime minDate;
    private static readonly DateTime maxDate;
    static MyFilterController() {
        minDate = (DateTime)SqlDateTime.MinValue;
        maxDate = (DateTime)SqlDateTime.MaxValue;
    }
    private Boolean CanFilter(string propertyName, string filterText) {
        IMemberInfo memberInfo = View.ObjectTypeInfo.FindMember(propertyName);
        if (SummaryItemTypeHelper.IsDateTime(memberInfo.MemberType)) {
            DateTime? convertedFilter = null;
            try {
                convertedFilter = Convert.ChangeType(filterText, typeof(DateTime)) as DateTime?;
            }
            catch {
                return false;
            }
            if (convertedFilter.HasValue) {
                if ((convertedFilter.Value < minDate) || (convertedFilter.Value > maxDate)) {
                    return false;
                }
            }
        }
        return true;
    }
    private ICollection<string> GetProcessedRequiredProperties(ICollection<string> searchProperties, 
string filterText) {
        List<string> result = new List<string>();
        foreach (string propertyName in searchProperties) {
           if (CanFilter(propertyName, filterText)) {
               result.Add(propertyName);
            }
        }
        return result;
    }
    private void FilterController_CustomGetFullTextSearchProperties(object sender, 
CustomGetFullTextSearchPropertiesEventArgs e) {
        string filterText = ((FilterController)sender).FullTextFilterAction.Value as string;
        if (!string.IsNullOrEmpty(filterText)) {
            ICollection<string> searchProperties = 
GetProcessedRequiredProperties(((FilterController)sender).GetFullTextSearchProperties(), filterText);
            e.Properties.AddRange(searchProperties);
            e.Handled = true;
        }
    }
    protected override void OnActivated() {
        base.OnActivated();
        FilterController filterController = Frame.GetController<FilterController>();
        if (filterController != null) {
            filterController.CustomGetFullTextSearchProperties += 
FilterController_CustomGetFullTextSearchProperties;
        }
    }
    protected override void OnDeactivated() {
        FilterController filterController = Frame.GetController<FilterController>();
        if (filterController != null) {
            filterController.CustomGetFullTextSearchProperties -= 
FilterController_CustomGetFullTextSearchProperties;
        }
        base.OnDeactivated();
    }
}

 

Guess you like

Origin www.cnblogs.com/foreachlife/p/How-to-Make-the-FullTextSearch-Action-Search-Within-Required-Properties.html