Change the url of a pagination in grails - url

Can anyone help me how to change the url displayed in the address bar in grails.
Here I have a data table. Once i click on the pagination list displayed at the bottom, lets say 2, then the url changes to
http://localhost:8080/test/account/list?searchText=%25&paginationNumber=10&advancedSearchText=&searchCriteria=&searchOperator=&offset=10&max=10
from
http://localhost:8080/test/account/list
Now I need the same url
http://localhost:8080/test/account/list
even i navigate to different pages.
Whether it is possible to override the url in grails.

It looks like your original page was the result of a form POST, with these additional parameters as fields on the form. When you use <g:paginate> it generates a normal <a> link for each button, which means a GET request, so the params go into the URL.
You might be able to get it to do what you want with a bit of JavaScript, wrapping a POST form around the paginate buttons with hidden fields for the additional data, and attach an event handler to the pagination links to trigger a submit of the form.
Alternatively, store the extra parameters server side in the session instead of passing them back and forth, and accept the offset and max in the URL.

Related

Query Parameters in URL from a Rails form

I have a filter sidebar that starts with a Rails form_tag and contains a range slider and a bunch of check_box_tag.
It posts and filters fine. It even persists on the next page as I render it with the checkbox values.
However, if you refresh the page, or send a link to someone, the filters are lost.
The only way I've seen how to do it is to use redirect_to and merge the params, but I'd rather not make a second call.
How can I pass all the options as query params?
As you're not creating anything I would recommend using GET requests with query string parameters. So the query can be shared with the url.
Url for your example image would be something like:
http://www.yourwebsite.com/?max_price=5
Which gives you a params[:max_price] in controller.

Struts 2 - Tiles-Result with HTML Anchor

is it somehow possible to add an anchor (or hash) to a Struts 2 Action-URL? To be specific:
I have a html form which can be extended with more fields if the user clicks a button "add more fields". This Button sends a html submit to the backend (Action "thismyaction") where a list object is filled with another set of input fields. The action then returns to a tile "thisismyform" which loads the same jsp as before, where the new fieldset is visible.
(Unfortunately there is no way to achieve that via ajax / JS in this project at the time. I know that you usually add fields that way, but i got the project as it is.)
Each fieldset is counted (fieldset-0, if user adds more fields another set is added "fieldset-1"). The sets always contain the same fields, but enumerated.
What happens here? There is a post to the action which generates another fieldset and redirects back to the same page, where it renders all fieldsets. Important: the result type is "tiles"! I guess this is what makes it difficult.
Now I want to dynamically add an anchor to this URL "thisismyaction.do", like e.g. "thisismyaction.do#fieldset-1". Use Case: User adds another fieldset, post to the action => result type="tiles" => JSP, user sees reload of the page with the new, second fieldset and gets "scrolled" to the second fieldset via the anchor. Is that possible?
I hope I could describe it properly, what I want to achieve... If there are questions, feel free to ask.
Simplest way I can think of right away is
<script type="text/javascript">
window.location.hash = "fieldset-<s:text name="latestIdOfFieldSet" />";
</script>
If you can figure out what's gonna be latestIdOfFieldSet before you render your jsp and set it in your action method. you should get what you are trying to do.

Link to route while keeping user input

I have made an app with jQuery mobile and laravel.
On the first page "mobilepage1" if have a form with a next button.
Also some of the form elements are dropdown menu's. The dropdown select options I get from a table in the database.
In the form on mobilepage1 I have a dropdown list with all department names:
I get this list from my controller like so:
$departmentlist = Company::find($usercompanyid)->departments;
This is how I display the list in the mobilepage1 form:
{{ Form::label('department', 'Department:')}}
<?php
foreach($afdelingslijst as $item) {
$afdelingsarray[$item->afdelingen] = $item->afdelingen;
}
?>
{{Form::select('size',$afdelingsarray)}}
On the second page I want to make a back button.
I tried the jQuery mobile way with a link with: data-rel="back"
But this does not work. I get a page error, it's missing the departmentlist used in mobilepage1. I can make a link to a route to mobilepage1, but I think all the information the user entered in mobilepage1 would be gone.
I see this url in the address bar when I am on mobilepage2 (this is after I filled in the form on mobilepage1 and clicked next).
/public/mobilepage2?size=Personeelszaken&size=32&directechef=Tineke&size=1&size=1&size=1&date-2=&size=1&time=&omschrijving=
Is there a way I can link to the the mobilepage1 route and still keep the information the user entered?
It looks like it would work just by hitting the back button if you passed in the department list with a view composer rather than through a route. Remove it from your route and place this in a file that's being autoloaded...
View::composer('mobilepage2', function($view)
{
$departmentlist = Company::find($usercompanyid)->departments()->lists('afdelingen','afdelingen');
$view->with('departmentlist', $departmentlist);
});
I'm not sure how you are finding $usercompanyid but you should be able to find it within the closure or pass it in by adding use ($usercompanyid) right after the function($view) part.
Also using the lists() function like that will build your array for you so you don't need to worry about the foreach loop in your view.
Since the values are getting passed back and forth as get variables, I think if you use Form::model() instead of Form::open(), it should place all those values back into the input fields for you.

rails best way to handle optional parameter

I want to display a list of stocks when the user logs in (default parameters). This smells like an index action. However, I will also make a form for the user to select the market capitalization of the stocks (that is to refine the universe). The submission of the form will send parameters to some action (I am not sure if I should use the same index action) and then do an ajax update of the list. This ajax doesn't do anything to the database, it just updates the parameters for the database query.
The first time the user visits the site, they will see the default parameters for the query, but I also want them to be able to change the parameters later and renew the list according to their parameter through ajax.
Yep, a list of stocks sure sounds like an index action. Do you mean that you want to filter the list depending on some form input?
You can make the form send to the index action with a get method, and in your controller, just read the params, and query your #stocks however you want your filter to work.
This is the non-Ajax solution, so users without Javascript will be happy. When you get this working, you can easily AJAXify it.

Rails - Reload action with new value in params

I have the following relation in my model:
A user can have several consumers
According to that, I call several actions from different controllers that depend on that consumer_id in the URL. So, I do stuff like:
/:consumer_id/products/all
/:consumer_id/locals/all?params...
I want to be able to given that I am in a particular view, let's say /3/products/all, be able to refresh, redirect or whatever is the best to /4/products/all, with a form that shows the different consumers attached to that user.
I know how to display the form, but I fail to see which action should I put given that I want to load the current controller, current action and the current params, except that I want to change the params[:consumer_id] and the session[:consumer_id] to the one chosen by the user in the form of the view.
What's the best or appropriate way of doing so?
This is a link to the same page with the same parameters and one additional parameter:
<%= link_to "Show more results", url_for(params.merge(:per => 100)) %>
If you want to modify a parameter rather than add a new one, you can modify the params hash just as you would modify any hash, and use url_for again.
Inside the view you can access both the current controller using controller_name and the current action using action_name
Your question is not clear. You want to choose consumer_id from your form, then use that consumer id inside the same page or for links to other pages?
If you want to use the consumer id for the current page, you need to use javascript or JQuery to update all attributes of the current page.
If you want to use that consumer id for links,
you may also use JQuery to update all links on your page
Or, you can submit the form to the server with new consumer_id.

Resources