Liferay 7.2 - Refreshing view.jsp

Fabio Carvalho, modified 6 Years ago. Junior Member Posts: 81 Join Date: 6/25/19 Recent Posts
Hi,

I have the following code in my java class for my portlet:

@Override
public void doView(RenderRequest request, RenderResponse response) throws IOException, PortletException {
    ThemeDisplay theme = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
		
    long start = 0;
    if (request.getAttribute("start-date") != null) start = (long) request.getAttribute("start-date");
    request.removeAttribute("start-date");
	
    long end = 0;
    if (request.getAttribute("end-date") != null) end = (long) request.getAttribute("end-date");
    request.removeAttribute("end-date");

    List<object> objects = new ArrayList<object>();
    if (start == 0 &amp;&amp; end == 0) objects.addAll(getObjects());
		
    System.out.println(objects.toString());
    super.doView(request, response);
}

@ProcessAction(name="filterByDate")
public void filterByDate(ActionRequest request, ActionResponse response) throws ReadOnlyException {
    request.setAttribute("start-date", ParamUtil.getLong(request, "start-date"));
    request.setAttribute("end-date", ParamUtil.getLong(request, "end-date"));
}<br><br>Now in my view.jsp I have the following:<br><br><pre><code><portlet:defineobjects />
<portlet:actionurl name="filterByDate" var="filterURL" />

<input type="text" id="daterange">
<script>
    $(function () {
        $('#daterange').daterangepicker({
	    endDate: moment(),
	    maxDate: moment(),
	    locale: {
                "format": "DD/MM/YYYY",
                "separator": " - ",
            }, function (start, end) {
                var startDate = new Date(start.toString());
                var endDate = new Date(end.toString());
   			
                $.ajax({
                    url : '${filterURL}',
                    type : 'POST',
                    data : {
                        "<portlet:namespace/>start-date": startDate.getTime(),
                        "<portlet:namespace/>end-date": endDate.getTime()
                    }
                });
            });
	
	$('#daterange').on('apply.daterangepicker')
    });
</script></code></pre><br><br>Now, this is doing the following: when I start my portlet, the values for the "start" and "end" variables are 0, so I add all my objects and System.out.println them. Then I open my Date Picker, click on submit and the "filterByDate" method is called. After that the "doView" is called again and now the "start" and "end" variables are not 0 and the System.out.println prints an empty list. So this is working until here.<br><br>My problem is that the view.jsp is not refreshed with the new "doView". It is not supposed to refresh? How can I refresh my view with the new information after I submit an ActionURL?</object></object>
thumbnail
Mohammed yasin, modified 6 Years ago. Liferay Master Posts: 593 Join Date: 8/8/14 Recent Posts
Hi,
I think your calling action method through ajax that why its not refreshing , you can submit the form and try
Fabio Carvalho, modified 6 Years ago. Junior Member Posts: 81 Join Date: 6/25/19 Recent Posts
Hi Mohammed,

Thanks for your answer. Yes, I am calling action method through ajax. How can I submit the form?
thumbnail
Mohammed yasin, modified 6 Years ago. Liferay Master Posts: 593 Join Date: 8/8/14 Recent Posts
You can have that date picker inside a form and have a submit button something like

<aui:form action="${filterURL}">
<aui: input type="text" id="daterange" />
<aui:button type="submit" value="save" />
</aui:form>

If you want to have start and end date you can have hidden input which you can set in javascript and then submit from javascript also.
Fabio Carvalho, modified 6 Years ago. Junior Member Posts: 81 Join Date: 6/25/19 Recent Posts
You are a lifesaver Mohammed!

It is working! I ended with something like this:
@Override
public void processAction(ActionRequest request, ActionResponse response) throws IOException, PortletException {
&nbsp; &nbsp; request.setAttribute("start-date", ParamUtil.getLong(request, "startDate"));
&nbsp; &nbsp; request.setAttribute("end-date", ParamUtil.getLong(request, "endDate"));
&nbsp;&nbsp; &nbsp;super.processAction(request, response);
}

And in my view.jsp:
<portlet:defineobjects />
<portlet:actionurl var="filterURL" />

<aui:form name="filterForm" action="${filterURL}" method="post">
&nbsp; &nbsp; <aui:input id="startDate" name="startDate" type="hidden" />
&nbsp;&nbsp; &nbsp;<aui:input id="endDate" name="endDate" type="hidden" />
</aui:form>
​​​​​​​
<input type="text" name="dateFilter">
<script type="text/javascript">
    $(function() {
        $('input[name="dateFilter"]').daterangepicker({
            endDate: moment(),
            maxDate: moment()
        });
    
        $('input[name="dateFilter"]').on('apply.daterangepicker', function(ev, picker) {  
            var startDate = new Date(picker.startDate.toString());
            $("#<portlet:namespace/>startDate").val(startDate.getTime());
            
            var endDate = new Date(picker.endDate.toString());
            $("#<portlet:namespace/>endDate").val(endDate.getTime());
               
            $("#<portlet:namespace/>filterForm").submit();
        });
    });
</script>