While there are a lot of easy ways to include searching with your Mendix widgets, you may want to consider building your own search functionality to have total control over the UI/UX and to take advantage of some of the fantastic Marketplace widgets. Creating custom search functionality is simple and can result in engaging reusable components for your application.
Consider the following domain model:
In this example I am creating a custom search feature for an Order Overview page. I want to implement a custom search because I want full control over the way the search is presented, so this method allows me to lay out and style my search fields in any way I choose. My page starts with a datasource microflow to bring in Search and nested within the Search dataview is a datagrid of Orders to show the results of my search. I have laid out my search fields and added a couple of helpful widgets to handle my reference set associations.
When the Search button is clicked, a microflow is called to evaluate my current search criteria and return a list of Orders. In essence, this microflow performs a retrieval of Order from the database with XPath. To make it easier to understand for my less experienced colleagues, I have created a couple of variables to help make the XPath easier to read and understand.
Each variable helps to pre-evaluate the associations that can be made from my Search entity and check to see if those associations are empty or not. When empty, my variable will be True, and when at least one record is selected over association it will be False. This will help me later when I am writing my XPath constraint.
When writing my XPath constraints, I make use of the Boolean variables with an OR so that if the variable is True if will skip to the next constraint, OR if false then it will constrain my Order results appropriately. While the variables are not necessary because you could simply check for the empty associations in the constraint, they do make it easier to read and explain.
The resulting OrderList is returned and I use a Change Object action so that I can set the Order_Results association to the returned OrderList. Adding the Refresh In Client option ensures that my datagrid updates with the new list of Order results.
The application now has a working custom search implementation that looks great and is very easy to extend with additional attributes or associations as the application develops further.
This blog was originally published by Kyle Kunzer on October 2nd 2023 here: https://email@example.com/diy-search-in-your-mendix-apps-787ed0898a44