Message Boards

Question to Filtering and Sorting Items with the Management Toolbar

Reiner Zufall, modified 3 Years ago.

Question to Filtering and Sorting Items with the Management Toolbar

New Member Posts: 22 Join Date: 7/6/20 Recent Posts
Hello there
i am trying to implement clay management tool bar. I use this reference https://help.liferay.com/hc/en-us/articles/360017886172-Filtering-and-Sorting-Items-with-the-Management-Toolbar- 
with searchcontainer
but i don't understand the example code.
i dont understand the varibale within the constructor.
[code] LiferayPortletRequest liferayPortletRequest,
 LiferayPortletResponse liferayPortletResponse,
 HttpServletRequest request, 
 SearchContainer searchContainer
How to create these and pass them to
MyManagementToolbarDisplayContext object.



thumbnail
Andre Kreienbring, modified 3 Years ago.

RE: Question to Filtering and Sorting Items with the Management Toolbar

Regular Member Posts: 152 Join Date: 12/18/06 Recent Posts
Basically you need to create a SearchContainer and optionally (recommended) a DisplayContext.
I do it like this:
Create the Searcontainer in the render phase of the MVC Portlet module and put it in the request:
PortletURL iteratorURL = PortletURLUtil.getCurrent(renderRequest, renderResponse);
_deviceDisplaySearchContainer = new&nbsp; SearchContainer<devicedisplay>(renderRequest, iteratorURL, null, "nothing found");
</devicedisplay>
Then in the JSP:
&lt;%
SearchContainer<devicedisplay> deviceDisplaySearchContainer = (SearchContainer<devicedisplay>) request.getAttribute("deviceDisplaySearchContainer");
[My]ManagementToolbarDisplayContext [my]ManagementToolbarDisplayContext = new [My]ManagementToolbarDisplayContext(request, liferayPortletRequest, liferayPortletResponse, deviceDisplaySearchContainer);
%&gt;
</devicedisplay></devicedisplay>
If you create the ManagementToolbar like this:
<clay:management-toolbar     displayContext="<%= [my]ManagementToolbarDisplayContext %>" />
You'll be able to access the Parameters from the ManagementToolbar in the methods of the DisplayContext. The Toolbar calls methods like
&nbsp;&nbsp;&nbsp; @Override
&nbsp;&nbsp; &nbsp;protected String getNavigation() {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; String navigation = ParamUtil.getString(liferayPortletRequest, getNavigationParam(), "all");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String orderByType = ParamUtil.getString(liferayPortletRequest, getOrderByTypeParam(), "asc");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String orderByCol = ParamUtil.getString(liferayPortletRequest, getOrderByColParam(), "[myDefaultOrder]");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String keywords = ParamUtil.getString(liferayPortletRequest, "keywords", null);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String displayStyle = ParamUtil.getString(liferayPortletRequest, "displayStyle", getDefaultDisplayStyle());
&nbsp;&nbsp; }
for filtering and
&nbsp;&nbsp;&nbsp; @Override
&nbsp;&nbsp; &nbsp;public String getSortingOrder() {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String orderByType = ParamUtil.getString(liferayPortletRequest, getOrderByTypeParam(), "asc");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String orderByCol = ParamUtil.getString(liferayPortletRequest, getOrderByColParam(), "[myDefaultOrder]");
&nbsp;&nbsp; }
for sorting.
Reiner Zufall, modified 3 Years ago.

RE: Question to Filtering and Sorting Items with the Management Toolbar

New Member Posts: 22 Join Date: 7/6/20 Recent Posts
thank you so much, you really helped me
but i have a few more question
do i have to override methods in "MyManagementToolbarDisplayContext" to make changes happend like ordering "asc" or "desc". And how do i know which method i have to override?
i use searchContainer tag in my .jsp file to display the grid. How can i change the display of the grid depending on the selected option i choose in clay management toolbar to e.g "filter by name"?
 
&nbsp;&nbsp;&nbsp; @Override
&nbsp;&nbsp; &nbsp;protected String getNavigation() {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; String navigation = ParamUtil.getString(liferayPortletRequest, getNavigationParam(), "all");
        
String orderByType = ParamUtil.getString(liferayPortletRequest, getOrderByTypeParam(), "asc");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String orderByCol ParamUtil.getString(liferayPortletRequest, getOrderByColParam(), "[myDefaultOrder]");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String keywords = ParamUtil.getString(liferayPortletRequest, "keywords", null);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String displayStyle = ParamUtil.getString(liferayPortletRequest, "displayStyle", getDefaultDisplayStyle());
&nbsp;&nbsp; }

An i have a question to this code snippet. sry but i don't understand how to use this related to clay management toolbar and what is the return type of this method?
And my last question, in clay management tool i have a tag named "getSortingURL"  for what do i need this
thumbnail
Andre Kreienbring, modified 3 Years ago.

RE: Question to Filtering and Sorting Items with the Management Toolbar

Regular Member Posts: 152 Join Date: 12/18/06 Recent Posts
To 1: The best you can do is to look into the Liferay source code:
Your DixplayContext most likely extends "SearchContainerManagementToolbarDisplayContext" and that extends "BaseManagementToolbarDisplayContext". Find the methods you can override in these classes.
getSortingURL() is also defined there.
In getNavigation(): Look at the parameters that are passed from the ManagementToolbar and use the [My]LocalServiceUtil (created by Service Builder) to get your entities. Finally put the results in the SearchContainer.
Example:
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; _mySearchContainer.setTotal(_myList.size());
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;_mySearchContainer.setResults(_myList);
Reiner Zufall, modified 3 Years ago.

RE: Question to Filtering and Sorting Items with the Management Toolbar

New Member Posts: 22 Join Date: 7/6/20 Recent Posts
Thank you so much
i tried to sort the grid. But when i get the Params from the clay management toolbar, the sorting order is always "asc".
in MyManagementToolbarDisplayContext.java i have overwritten the getSortingOrder as follow

 @Override
&nbsp;&nbsp; &nbsp;public String getSortingOrder() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String orderByType = ParamUtil.getString(liferayPortletRequest, getOrderByTypeParam(), "desc");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//String orderByCol = ParamUtil.getString(liferayPortletRequest, getOrderByColParam());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(orderByType.equals("asc")) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;orderByType="desc";
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println("equals asc");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else if(orderByType.equals("desc")){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;orderByType="asc";
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println("equals desc");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;orderByType="desc";
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(Validator.isNull(orderByType)) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;orderByType = "desc";
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println(" orderByType " + orderByType);

&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return orderByType;
&nbsp;&nbsp; &nbsp;}

But everytime i refresh the site or press on the "sorting direction" button in clay management toolbar, i always get the output
System.out.println("equals asc");

how can i pass the current value to .jsp?
thumbnail
Andre Kreienbring, modified 3 Years ago.

RE: Question to Filtering and Sorting Items with the Management Toolbar

Regular Member Posts: 152 Join Date: 12/18/06 Recent Posts
For me it works like this:
When getNavigation() is called I fill my List accordingly to the set filter. The List is like:
List<mydisplay> myEntities = new ArrayList<mydisplay>();
</mydisplay></mydisplay>
When getSortOrder() is called I do:
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; mySearchContainer.setOrderByCol(orderByCol);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; mySearchContainer.setOrderByType(orderByType);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; mySearchContainer.setOrderByComparator(comparator);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;myEntities.sort(comparator);
For this you'll need a Comparator that sorts your entities. And as far as I understand it, the asc / desc setting comes from the Comparator.
BTW: If someone can please explain why we need to set the Comparator on the SearchContainer AND sort the List manually. Maybe there's a more efficient way to achive the sorting...
Reiner Zufall, modified 3 Years ago.

RE: Question to Filtering and Sorting Items with the Management Toolbar

New Member Posts: 22 Join Date: 7/6/20 Recent Posts
Thank you for your answer.
I got the asc/desc order by creating a Comparator object like this:
boolean asc = false;


OrderByComparator<entityname> obc = OrderByComparatorFactoryUtil.create("EntityName",columnName,asc);</entityname>

and in searchcontainer tag i user LocalserviceUtil as follow:
&lt;... results="&lt;%=LocalServiceUtil.getEnties(searchContainer.getStart(), searchContainer.getEnd(), obc)%&gt;"/&gt;

In ManagementToolbarDisplay i got the methods which i get the column name and asc and desc String.
in jsp i switch the boolean var depending on the value i got from ManagementToolbarDisplay.

I have an other question, when i got the Filter name returned by getNavigator, how i know which column i have to choose. When i use
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String orderByCol ParamUtil.getString(liferayPortletRequest, getOrderByColParam(), "[myDefaultOrder]");
twice, i cannot open the Clay managementToolbar dropdown Menu.
and should i create get-Methods in -service to find the columns?
thumbnail
Andre Kreienbring, modified 3 Years ago.

RE: Question to Filtering and Sorting Items with the Management Toolbar

Regular Member Posts: 152 Join Date: 12/18/06 Recent Posts
You can get the "navigation" from the request URL with
String navigation = ParamUtil.getString(liferayPortletRequest, getNavigationParam(), "all");
And of course you need to react on the filter and implement it somehow. [YourEntitiy]LocalServiceImpl may be a good place for the implementation. If you have your filtered List  ready just do:  searchcontainer.setResults(List<YourEntity>) again.
Reiner Zufall, modified 3 Years ago.

RE: Question to Filtering and Sorting Items with the Management Toolbar

New Member Posts: 22 Join Date: 7/6/20 Recent Posts
yes i did the navigation on the same way.
My filter and sorting function work.
Thank you so much