passing a list of objects from jsf page to another - jsf-2

I am new at JavaServer Faces (JSF).
I have a list of cars and after pressing the commandButton of my first page I want to show the list in the other page (the backing beans are RequestScoped).
I have seen examples where you can only pass String values.

if you have list of cars, it means you have data table or some another list which is coming from managed beans. if you want show whole list in the next page, you need just call the same list from next page.
if you want pass only one row /object, you should pass row/item id via f:param , in the next page from another managed bean you should handle id and call from database getItemById(id) and in the page show item.

Related

Pre-populate ListBox / MultiSelectList with selected items

Is there a way to pre-populate a MultiSelectList with selected items?
Example:
I have a single View that has the following ListBoxFor that will cause the page to update what it's displaying by allowing filtering of Model.Companies.
#Html.ListBoxFor(m => m.SelectedCompanies, new MultiSelectList(Model.Companies, "IdString", "CompanyName")
What I'd like to have happen is after the update, the MultiSelectList will have the items that were selected before the page updated and refreshed. Does that mean I need to return SelectedCompanies with what was selected, or is there another way?
I am using the javascript library Chosen for usability of the ListBox on the client side, but I don't think that this affects what I'm trying to do.
Sometimes, JS libaries can interfere with your desired results. I can't speak for Chosen JS library, but inspect the markup and see how it renders. As long as it still has the listbox on the client (it must have some input element defined somewhere; my guess it hides it and updates the values as they are selected), then yes it should integrate fine.
However, when the controller posts back, you have to repopulate the Model.SelectedCompanies property with whatever values came back from the POST operation to the controller. The property should still have the selected companies if you return a View from the POST operation. If you are using a RedirectToAction instead, you'd have to store the selections in TempData.

How to retain values of dropdown with selected values in MVC?

The scenario is - There are two view in my mvc application. One is Index and other is for adding payment details. in index form I fetch details of a perticular client in that I have cascading dropdown one for selecting type and one for according to type select client in another dropdown, both are coming from DB's and on d same view I have an ActionLink for going on Payments view "for adding the payment details for the same client that is been selected on Index views second dropdown (first is for Type)".
My problem is that when I am going on Payments view and clicking back button of browser to go back on my index page it is clearing the second dropdown. and all other data is safe on the form. So can you please help me to avoid this so that all the data of my Index view should retain throughout the execution.
This also in case when I add Payment for that client and click submit it should redirect on the Index view with retaining all its previous data. that we have fetched on Index page.
with a message box Payment Added successfully.
You can save the current selected values of dropdown1 and dropdown2 into one of the followings:
URL
Session
Cookie
Then in the Index page you always check to read those values if they are existed to setup the dropdowns in the first request. Then do update those values again whenever user selects to update the dropdowns.

Passing a selected row from a GSP's table to a Controller in Grails 3.0

I have one controller that sends a list of users data (UserID, FirstName, LastName, email) to a GSP.
The GSP displays the data into a table of 5 columns where the 5th column is a submit button so one particular row in the table can be selected. That selected row of data is then passed back to the same controller for further processing.
I am new to Grails 3 and have not found a similar use case.
It seems to me that Grails does not have an equivalent of the JSF dataTable. I need pointers on how to do this without using JavaScript or JQuery.
Thanks in advance.
You can use grails http://grails.github.io/grails-doc/2.2.1/ref/Tags/remoteFunction.html which can be assigned to Dom event to call the remote method. For example If you have a regular button you can add the following code:
onclick="<g:remoteFunction action='modify' id='${user.id}'/>"
Another option to use would be http://grails.github.io/grails-doc/2.3.7/ref/Tags/submitToRemote.html which creates a button that submits the containing form as a remote Ajax call. Here url parameter can be used as per documentation:
url - The url to submit to, either a map contraining keys for the action, controller and id or a string value
Hope this helps.

Displaying Grails domains in jquery tabs

What would be the best way to display data from Grails database in JQuery UI tabs? What I would like is to have a tab interface and on each tab is a list of the records from a different domain. For instance, Tab1 displays the record list from Domain1, Tab2 displays the record list from Domain2, etc.
I have the JQuery UI tab interface set up and working and am currently using createLink to call the method from the controller to return the model of the appropriate domain. The tabs look like this:
<div id="tabs">
<ul>
<li>Hardware records</li>
<li>Model records</li>
<li>Building records</li>
</ul>
</div>
The method from the controller looks like this:
def listHardware() {
[hardwareList:Hardware.list(), hardwareInstanceTotal:Hardware.count()]
}
I've also played around with rendering a whole GSP within the tab by using "render(view:'HardwareList', model:[hardwareList:Hardware.list(), hardwareInstanceTotal:Hardware.count()]", but that takes a VERY long time (at least 5 seconds) to load each time the tab is selected and is not at all ideal, especially if it were to take that long for each tab.
UPDATE
As noted in one of my answers to Rimero's answer below, I was able to use templates to display tables of my domains' data. I'm also trying to implement pagination on each tab using the tag, but each time I click on one of the pages to view another page, it takes me to the full template itself outside of the tab interface. Any thoughts on how to format the tag so that everything stays within the tab??
Here's my suggestion:
You can fetch everything at once in your controller in your index method for example.
You can implement your tab contents as templates
(g render template). Each tab == 1 template.
You can fetch your domain objects buildingList,
etc. from the index method of your controller.
The g:render template code for each tab may only need to be passed a map or a collection for rendering.
In this case you don't need hyperlinks to controllers endpoints. You just keep anchors to the tab(div id) as in the default example here -> http://jqueryui.com/tabs/.
UPDATED ANSWER
As you said that sending all the data at once takes a long time, you could fetch it asynchronously. If possible populate the data only for the first tab directly.
Create a business method for each tab, that will return the model as JSON, data is only fetched if not already retrieved from the server (Need to keep state or see for example if the tab id has some DOM nodes.
Using JQuery, when the DOM is ready, get the current tab and if you didn't fetch the data for the first tab eagerly, fetch it at this moment with the busy image spinning.
As soon as you select a new tab, you need to check if the data was already fetched, if not, you send an ajax call and your callback function populate the data in the tab div container for example.
Hope it helps.

Divide a user interface into asp.net mvc views

I have a CSS #MainDiv containing a #TreeDiv on the left side and a #DataGridDiv on the right side.
The TreeDiv contains a Javascript Treeview with Department objects and the DataGridDiv
contains a Datagrid with Employee objects.
Changing the selection of a Department in the Treeview should change also the related employee objects in the DataGrid.
I have setup a DepartmentController. Both controls should be able to recieve data via ajax
independently from each other.
1.) What kind of object should my Index method return to display this aggregated data in the view?
2.) How should I divide my controls into what sort of views?
2.) How should I divide my controls into what sort of views?
Create a view with #MainDiv, #TreeDiv and #DatagridDiv. Let #TreeDiv host your tree control (You already know this). Create a partial view to display the datagrid with employee objects. Let #DatagridDiv host this partial view.
Now when a department is selected in tree control, you can make an ajax call to a controller method which accepts the department and returns the partial view containing the employee data. Update the #DatagridDiv with returned data.
Alternatively if you are comfortable with Json, your controller method could return the employee data in Json format (instead of partial view) and You can populate this into an html table inside #datagridDiv using javascript/jquery.
1.) What kind of object should my Index method return to display this aggregated data in the view?
In the Index method you can return your view which contains all 3 Divs and #TreeDiv populated with tree control. On the client side when page loads you can identify the selected department to make an ajax call and update #datagridDiv. This approach will have a it lag on the clientside, however you can use that to display some animation indicating the page is loading/div is updating.
If you dont want to add this lag period, identify the department that will be selected when tree view is loaded and populate the partial view for that department, add this to your #datagridDiv on the server side and deliver.

Resources