Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Scout/Tutorial/3.7/Minicrm/Add a search form"

< Scout‎ | Tutorial‎ | 3.7
(Adapt the outline service operation getCompanyTableData)
(Replaced content with "The Scout documentation has been moved to https://eclipsescout.github.io/.")
 
(22 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{note|Scout Tutorial|This page belongs to the {{ScoutLink|Tutorial|Minicrm Step-by-Step|Minicrm Step-by-Step Tutorial}}. It explains how to add a search form to a table page. You need to {{ScoutLink|Tutorial|Write The First Page|have a table page in your application}} in order to continue.}}
+
The Scout documentation has been moved to https://eclipsescout.github.io/.
 
+
Once we have '''created a table page''' and '''implemented the service''' to get the table data we can take advantage of a feature the Scout SDK offers: it can '''create a search form''' based on the information it already has.
+
 
+
The alternative, of course, is to create the search form manually.
+
 
+
== Create Search Form ==
+
 
+
Go to the ''CompanyTablePage'', right click and choose '''Create Search Form'''. The created Search Form will contain a field for each column of the table based on who it is created.
+
 
+
[[Image:Newsearchform.jpg]]
+
 
+
Use ''Company'' as the name and click ''Next''.
+
 
+
[[Image:Newsearchformwizard.jpg]]
+
 
+
{{note|Form Data|When creating a ''form'', Scout SDK will automatically create a ''form data'' class in the background. The ''form data'' class will be created in the '''shared''' plugin. It is the data transfer object use to communicate between client and server. Every form has a context menu called '''Update Form Data''' to regenerated the ''form data'' class based on the form.}}
+
 
+
Done! What did Scout SDK do?
+
 
+
# it created a '''CompanySearchForm''' in the client plugin
+
# it created a '''CompanySearchFormData''' in the shared plugin
+
# it set the '''Search Form property''' of the ''CompanyTablePage'' to ''CompanySearchForm''
+
 
+
{{note|How does it work?|The Scout runtime will create the ''search form'' when showing the ''company table page''. Using it will generate a ''SearchFilter'' object that is  passed on to the <tt>execLoadTableData</tt> method of the ''table page''. The search filter contains the ''form data'' of the ''SearchForm''.}}
+
 
+
== Adding Fields ==
+
 
+
Add a field to the ''search form'': right-click the ''CompanySearchForm'' and pick '''New Form Field...''' from the menu.
+
 
+
[[Image:Scout New Form Field.png]]
+
 
+
Pick '''String Field''' from the list and click ''Next''. Set the name to ''Name'' and click ''Finish''.
+
 
+
[[Image:Scout New String Form Field.png]]
+
 
+
{{warning|Do not forget to update the ''form data''!|You need to use the '''Update Form Data''' context menu on the ''CompanySearchForm''. If you don't, the ''form data'' will know nothing of the name field you just added.}}
+
 
+
This is already enough to show a functional ''search form'':
+
 
+
[[Image:Scout Minicrm With Search Form.png]]
+
 
+
'''Before we continue''', add a ''Short Name'' field to the ''company search form''!
+
 
+
Don't forget to '''update the form data''' after adding a new field! :)
+
 
+
There is one missing piece, obviously. The service that gets the actual data needs to build some sort of constraint (a WHERE clause to the SQL statement) to filter the rows returned.
+
 
+
== Adapt the outline service operation getCompanyTableData ==
+
 
+
In order to constrain the SQL statement for the Company Outline we have to adapt the service operation ''getCompanyTableData'' and add a parameter ''searchFilter'' of the type ''CompanySearchFormData'' to the method. Do that for both the service interface ''IStandardOutlineService'' and the service implementation ''StandardOutlineService''. There are two easy ways to find them: Ctrl+Shift+T, or find the ''StandardOutlineService'' on the server side in the ''Scout Perspective'' and click on the two links.
+
 
+
Interface:
+
 
+
<source lang="java">
+
public interface IStandardOutlineService extends IService {
+
  public Object[][] getCompanyTableData (CompanySearchFormData searchFilter) throws ProcessingException;
+
}
+
</source>
+
 
+
Implementation:
+
 
+
<source lang="java">
+
public Object[][] getCompanyTableData(CompanySearchFormData filter) throws ProcessingException {
+
  StringBuilder statement = new StringBuilder();
+
  statement.append(
+
    "SELECT COMPANY_NR, SHORT_NAME, NAME " +
+
    "FROM  COMPANY " +
+
    "WHERE  1=1 ");
+
  if(!StringUtility.isNullOrEmpty(filter.getShortname().getValue())){
+
    statement.append("AND UPPER(SHORT_NAME) LIKE UPPER(:shortname || '%')");
+
  }
+
  if(!StringUtility.isNullOrEmpty(filter.getName().getValue())){
+
    statement.append("AND UPPER(NAME) LIKE UPPER(:name || '%')");
+
  }
+
  return SQL.select(statement.toString(), filter);
+
}</source>
+
 
+
'''Prefixing of names with a colon''' automatically makes these instances '''bind variables'''. The Scout runtime will bind these ''bind variables'' to the appropriate fields from the ''form data'' object.
+
 
+
{{warning|Naming Convention|We're relying on a naming convention, here! If your form has a field called '''FooBar''', then the bind variable will be called ''':fooBar'''. Note the capitalization!}}
+
 
+
Now we need to forward the ''CompanySearchFormData'' to the server. Return to the method '''execLoadTableData''' in the '''CompaniesTablePage''' and add a new parameter to the service call according to its interface definition. The ''CompanySearchFormData'' required is available from the ''filter'' parameter:
+
 
+
<source lang="java">
+
  @Override
+
  protected Object[][] execLoadTableData(SearchFilter filter) throws ProcessingException {
+
    return SERVICES.getService(IStandardOutlineService.class).getCompanyTableData((CompanySearchFormData) filter.getFormData());
+
  }
+
</source>
+

Latest revision as of 07:24, 18 March 2024

The Scout documentation has been moved to https://eclipsescout.github.io/.

Back to the top