thymeleaf :: populate the form with data from the service - javascript

I have a controller as below
#RequestMapping(value = "/user/{userId}/personal", method = RequestMethod.GET)
public String getUserPersonalInfo(#PathVariable("userId") Integer userId, Model model) throws ServiceBusinessException {
UserBO userBO = userServiceImpl.findUserByUserId(userId);
model.addAttribute("user", userBO);
model.addAttribute("userId", userId);
model.addAttribute("genders", EnumSet.allOf(Gender.class));
model.addAttribute("maritalStatuses", EnumSet.allOf(MaritalStatus.class));
model.addAttribute("ethnicities", EnumSet.allOf(Ethnicity.class));
System.out.println(EnumSet.allOf(Ethnicity.class));
return "personal";
}
Please note the following :
this is a GET call made from the UI to the controller, the controller then processes the request and returns the user along with the other information to the view (personal.html).
Now on the personal.html i need to have a way to edit this user information. thus I am thinking of binding this user data in a th:form tag
this form would then be used to send a post request to the controller to save the changes made by the user.
The problem is that I have not seen any code examples where this has been done. All examples which I have seen, the form is empty and the page is then loaded. user fills up the empty form and click on submit, this information is then stored in the db.
I however on the other hand, is doing something opposite. I need to populate the form with the user object on page load and then on submit this form should be saved in the database.
Can someone guide me to this example please ?

#user641887
While populating the user details on page load, bind data using th:field for a read-only form and when user clicks on edit button make the form fields which are allowed to edit as editable. Now user can edit the required information and save the details.
Sample thymeleaf form code
<input type="text" th:field="*{datePlanted}" />

Related

Making Request to Controllers - ASP.NET Core

I have a form to fetch data from my database based on the asp-action. The form looks like below
HTML
<form id="user__collection" asp-action="Admins" asp-controller="User" method="get">
<input name="collection"
type="text"
id="user_search"
/>
<button type="submit">
</button>
</form>
Now i have two menu tabs named Admins and Users . When i am in the admin tab and i search for a surname, it fetches from the database and display both Admins and Users under the respective tabs, that is the logic and works as expected
Now when i am in users tab and i search for a surname, after the page reloads, it goes back to the Admin tab, and i have to select the Users tab to see the results.
My issue here is, is there a way i can make sure after the page reloads, it stays in the users tab without having to go to the Admin tab first since that is where i made the request from.
My idea was to find a way to use JS to set asp-action based on the URL.
Admin Menu url - localhost:8000/admins
Users Menu url - localhost:8000/users
UserController
public async Task<IActionResult> Users([BindRequired] string collection)
{
//fetch users from database and display
}
public async Task<IActionResult> Admins([BindRequired] string collection)
{
//fetch admins from database and display
}
U can set a Referrer link to take U back to the previous page .
Proceed as below :
ViewBag.Url = System.Web.HttpContext.Current.Request.UrlReferrer;
And then in the controller use this :
return Redirect(ViewBag.Url);
And if U are using html tabs ,the solution may not be working because html tabs don't have urls.
To solve this U have to Create partial views and now U have their urls.
Good Luck
Maybe you can use at controller:
return RedirectToAction("Users");

Posting to Javascript from php without form action

I'm working on a child theme that uses ultimate member and redefines some of it's functions. Previously it redefined the follow button and sent the info to a payment form and then the form sent the info to a javascript file. The form is no longer used so we are trying to send the info directly to a javascript file and I haven't worked with php in awhile so I'm a little lost. The only options I find are form action and method post which aren't working,
The function is
<div class="um-followers-btn">
<?php
if (in_array("um_customer", $res->roles)) {
$nav_link = UM ()->permalinks()->get_current_url(get_option('permalink_structure'));
$nav_link = remove_query_arg('um_action', $nav_link);
$nav_link = remove_query_arg('subnav', $nav_link);
$nav_link = add_query_arg('profiletab', $id, $nav_link);
And previously the trigger was
echo '<a href="" id="cm_follow_button" data-toggle="modal" data- target="#payment_modal" class="um-button" >Follow</a>';
Obviously this doesn't work so any help would be appreciated.
Apologies, the echo puts forth a "follow" button, what the button appears but what I"m trying to accomplish is for the values, such as user1 and user2 to go to another php file which inserts them into the database and changes the button to request sent this is allhandled in the other php file by the previous team, I'm having trouble sending it there, the previous method was going to a javascript file from the last line above, this line produced the button which when pressed called forth a php form that took payment info, and then sent that to a js form which in turn took theinfo fand verified the payment format, changed the "follow" to "Follow request sent" and sent the rest to a php form that as I said, put the ifo in the mysql database and gave the 2nd user the option of accepting the follow request. Ultimate Member doesn't offer this option which is why it was modified in a child theme like this. We aren't processing payment like this we just want the button to send the info to the php form and I'm having that issue with doing it, via this button. I hope that helps

Get elements of a query-set with a jquery get function. Django

In my django app I would like to display a text when a user clicks on a button, without refreshing the page. When the user clicks again, I would like an other text to be displayed, still without refreshing the page.
The informations (texts) I wan't to display are in a query set, named "information".
So I would like to know how to accomplish that.
Here is the way I try to do it:
I create a view where I store my query set:
def get_information (request):
information= Information.objects.filter(object1__id= X)
Then I create a jquery get function (in the template of the page where the user clicks) in order to get informations on this list:
function get_info(){
$.get('/mysite/get_information', {'information':information}, function(data) {
$('.information').html(data);
});
};
And then I render it in my template with a submit button and a onclick="get_info".
But I don't know how to make the request get a different information at each request, in order that the user does not get the same information twice.
Thank you a lot for your help.
Take a look in to Django Pagination. You could create the Paginator object in your view and then you can display message from the queryset based on the request from the client side. Let me know in case of any issues.

How to pass form parameters to Action class when opening a jsp in a new window through Javascript

Below is the Javascript function call in which I am calling a struts action which in turn resolves to a jsp .In the Jsp page which is opened in a separate window I have a form with a few text boxes and drop down and a submit button.When I submit the filled form I am unable to get all these form fields although those instance variables are defined in the action class.Is there something that i am missing here????
function editQuestions(val) {
window.open("fetchQuestionAction.action?"+
"quesId="+val,'mywindow','width=400,height=200');
}
For get the data in the new window you can:
POST the data to the Different action that will put it to the session. Then open the window. The POST action should be done via AJAX to not reload the page unneccessary.
Add the data from the Form fields to the query string for the URI for the window.open first parameter. Then you could use this data on the server to render the new page or just send it in hidden fields and use on the client.
Can you read the data from the form fields and append them as request params to the URL. This way the params should be available on the opened window.

Rails nested form, create and then update new associated models without page reload

Relevant info
I have a User model with plenty of attributes and several has_many associations.
I have a BIG form to edit existing user info, including nested fields.
I have divided the form into several forms in different divs and show only one form at a time. (i.e. tabs in UI)
User can save info by clicking Save button, which submits currently visible part of form via Ajax. No page reload. Just updating a status box with rjs.
I use Ryan Bates example to dynamically add fields for nested models.
Problem
If a user adds fields for associated models using js
and submits form n times
n new associated models are created
this happens because js-added fields have no hidden_field for id attribute and every time form is summited, parameters do not include id for that specific nested record. i.e. first time it is ok, after second ajax post request (reminder - page does not reload) that record should be updated, but instead new record is being created because there were no id parameter passed for this already existing record.
Question
Should I make dirty hacks in controller to find out if new records should be saved and if they are - update dynamically added form in view by adding hidden field with newly saved record id value. The question is - how do I do it gently, writing the most maintainable and robust code possible?
At the moment update action is dead simple, with ideas of dirty hacks commented
def update
#user = current_user
respond_to do |wants|
wants.js {
#analyze parameters to find out which
#nested model is going to be saved (not updated)?
#also save identifier of nested record in view i.e. {...."324324234" => {:a => 1, :b => 2, .... etc}}
#user.update_attributes(params[:user])
# check if the record of the model was saved and take it's id;
# use identifier 324324234 to find nested form and insert hidden
# field with record id using rjs
}
end
end
I hope I have defined my problem clearly. I would really like to avoid such hacks and write pretty code. I also wouldn't like to reload the page or part of it with ajax. Any suggestions?

Categories

Resources