How To implement a tag field |
Many applications require tagging data in a convenient way. This tutorial explains how to easily achieve such functionnality within Packflow, using modeling features with minimal customization.
A simple Todo application is used as an example.
We'll model a content-type to store Tags, add a choice targeting it in Todo content-type. Then, in custom code, we'll add the 'Tag' field in every quick search query, to ensure convenient search within tags.
This topic contains the following sections:
Quick Search is the search feature available in all grid views and navigation pages. By default, it searches through a set of pre-defined columns (Id, Title) plus the displayed columns.
It is possible to override this behavior on any view, to add or remove columns from the default selection. We'll use this API feature to add the 'Tag' column to every quick search made on TodoItem content-type.
Override the GetQuickSearchFields method to add the 'Tag' field when applicable.
public partial class ViewOfApplication_Todo : PFView { public override List<PFField> GetQuickSearchFields() { List<PFField> quickSearchFields = base.GetQuickSearchFields(); //If view targets Todo content-type, and Tag field is not selected, add Tag //to quick search fields. PFContentType todoCt = this.ContentTypes.FirstOrDefault(ct => ct.Name == Application_Todo.ContentTypeName_TodoItem); if (todoCt != null) { PFField tagField = todoCt.Fields.GetByName(ContentType_TodoItem.PFFieldName_Tag); //Names may not be unique because of multiple possible target content-types. //Modeling guid is a good reference in this case. if (!quickSearchFields.Any(field => field.ModelColumnGuid == tagField.ModelColumnGuid)) quickSearchFields.Add(tagField); } return quickSearchFields; } }
Since every view in this application uses this class, now every quick search targetting 'Todo' content-type will consume this implementation and look in 'Tag' field. This remains true for runtime views.
When editing a Todo item, it possible to conveniently select and add tags.
When searching for tag terms in navigation or grid views, the 'tag' field is always selected by default.
Below, you can see the effect of a quick search filter in navigation. Note the 'Tag' field is not in the view's columns.
After applying quick search, our initial todo 'Have a walk' is found in view.