I am using Thymeleaf and have a requirement of displaying labels dynamically based on database hit.
Suppose we have a column "Race" in our database and table name is "Table1" and the corresponding entity name is "Table1". When user access the html page (home.html) which will hit the table and return the values of field "Race". So if table has 2 values then 2 labels would get display on the page with the values on them and if there are 3 values then 3 labels respectively. I need a Thymeleaf or js code to implement this.
Please help and thanks in advance.
First of all you need proper controller:
#RequestMapping("/home")
public String showHome(Model model) {
List<Table1> list = //Invoke method to get proper rows from db for example use Spring CRUD Repository
model.addAttribute("list", list);
return "home"
}
Inside of your HTML put:
<label th:each="entity: ${list}" th:text="${entity.race}"></label>
Related
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.
I need solution for my problem on urgent basis, I am new with mvc, knockout please provide me sample code for my problem. any help will be highly appreciated.
suppose I have an observable array in my viewmodel i.e
var viewmodel = {
vendorproviders : ko.observablearray([])
}
where vendorproviders list consist of multiple attributes like id, name, country, address etc
I want to populate that array in my grid where each row will have a select button, when that button is clicked it should post the id to my controller action either by submitting or by ajax call.
Furthor more that grid should be searchable like if there is a separate text box, based on the value of text box grid should display matching providers else display all providers.
when user search for particular provider grid should populate from observable array instead of making call at server again and again to pupulate the observable array.
I would suggest starting here.
http://learn.knockoutjs.com/#/?tutorial=intro
What you are talking about is all the basic functionality of the tools you referenced.
I have never asked a question on StackOverflow before, and never wanted to, but I am desperate, so here we go: I cannot get a saved value to show up as the default value/display in a dropdown.
I set up the list in my controller:
public ActionResult Index()
{
//User Dropdown List
var users = Roles.GetUsersInRole("Manager");
SelectList list = new SelectList(users);
ViewBag.Users = list;
return View();
}
Then in the view an admin can then select one of these users and save it to my database via EF:
#Html.DropDownList("Users", ViewBag.Users as SelectList, "--Select Manager--")
This all works great, however, when you edit this entry, I want the dropdown list to show the current saved manager, not the first name in the list. I was hoping on my edit action that I could pull the current manager out of the database and pass it back into the dropdown as the default selected item, but no go:
public ActionResult Edit(int id = 0)
{
var theOwner = (from v in _db.Location where v.LocationID == id select v.Owner).FirstOrDefault();
var users = Roles.GetUsersInRole("Manager");
SelectList list = new SelectList(users, theOwner);
ViewBag.Users = list;
From all the examples I have read over the last 2 weeks, everyone has had 3 different values to work within their dropdowns, making it possible to use all the overloads in the SelectList method. However, my problem is that I just have this string list with only one item in it, so I can't utilize the overloads as I want.
So does anyone have an idea on how I can get this to work? Thanks a lot in advance for your time on this!
I'm pretty sure that if you modify the second parameter on the line where you create your SelectList, it should work -- it does for me.
Here is what I think the trouble is: Currently you are specifying the second parameter as 'theOwner', which is an object reference from the earlier Linq statement. But the SelectList contains a bunch of strings (the UserNames of the users which match the specified rolename). As a result, the SelectList doesn't 'know' how to match what you specified as the SelectedItem to something in the list of strings it contains.
But if you refine that second parameter so it specifies the USERNAME of the Owner that you just looked up, it should work. However I do not know what the correct property name is from your Location table. If the field you are currently selecting (v.Owner) contains the UserName itself rather than some Key then the syntax would be:
SelectList list = new SelectList(users, theOwner.Owner);
If that column actually contains a key for the User like an int or a Guid then you will have query for the UserName using the key, but the nature of the fix is the same.
Hope that helps.
A quick workaround is not to use #Html.DropDownList but plain html code.
As an example for your case, use the following html code in your View instead of Html.DropDownList helper:
<!-- NOTE: the ID and name attributes of "select" tag should be the same as
the name of the corresponding property in your Model in order for ASP.NET MVC
to edit your Model correctly! -->
<select id="User" name="User">
#foreach (var user in (SelectList)ViewBag.Users)
{
if (user == ViewBag.TheOwner)
{
<option value="#user" text="#user" selected = "selected" />
}
else
{
<option value="#user" text="#user" />
}
}
</select>
Also , for this to work you need to add one more line to your Edit method:
ViewBag.TheOwner = theOwner;
Another solution is also possible using #Html.DropDownListFor() however you haven't shown your model so I can't tell you what exactly to use. When DropDownListFor is used, ASP.NET MVC will select an option automatically based on the value in your model.
I am using Entity Framework Database First approach in ASP.NET MVC.
I have created models from an existing database and everything works fine.
I am using jqGrid and trying to create a drop down list for a column which is defined as an Int in the database.
In the database, the values are either 0 or 1, so in the dropdown list I will have to show two values such as "Import" or "Export" based on whether it is 0 or 1.
Would I be able to handle this scenario in the jqGrid?
Please post any suggestions if you have!
I think you can handle this thing in colModal itself. there's is one option editOption you can specify with you colModal and based on the value you are getting from data you can specfy the text property of that column
In this case, the data for the grid should contain “One” or “Two” to be set in the column myname.
Now, with this setting
<script>
jQuery("#grid_id").jqGrid({
...
colModel : [ {name:'myname', edittype:'select', formatter:'select', editoptions:{value:"1:One;2:Two"}} ... ]
...
});
</script>
the data should contain the keys (“1” or “2”), but the value (“One”, or “Two”) will be displayed in the grid.
check this link for further help. You will get your answer. Because you didn't post your code(atleast you should have done that for ur that particular column) i can not say you are implementing the same way.
If i wanted to store the struts2 tag checkboxlist elements in the database ,How do i save it in the database .I mean how do i work with the checkboxlist tag ,
like how do i declare it in the table .How do i use it ?
Thanks
Here are several different examples showing how to use the checkboxlist in your Action class and view.
Once the checkbox is submitted to your action, you'll have a List populated with the checked String values. You can then save those Strings in your database in an appropriate field type (i.e. for MySQL that could be CHAR or VARCHAR).