I have a datatable inside a modal form and I need load it dynamically in serverside, I have a input where I catch value and need to send it via javascript (or other method different than get it from modelAttribute object)to load the table.
this is the datatable, I need the value stored in #idCProp input
<table th:if="${entity.formulario == 'formPerson'}" class="table dataTable" dt:deferLoading="10" id="propDT" dt:reloadSelector="#reloadProponent" dt:table="true" dt:sortable="true" dt:displaylength="10" dt:dom='ftip' dt:url="#{/person/loadProponents/__${entity.id}__/ **VALUE FROM INPUT**}" dt:serverside="true">
<thead>
<tr>
<th dt:property="entity.id" dt:renderFunction="nameAndLastName">Persona</th>
<th dt:property="entity.id" dt:sortable="false" dt:renderFunction="getProposalPost">Cargo</th>
<th dt:property="entity.id" dt:sortable="false" dt:renderFunction="getProposalInstitution">Institución</th>
<th dt:property="sendMethod.name" dt:sortable="false">Método de envío</th>
<th class="operations" dt:sortable="false" dt:property="id" dt:renderFunction="idToUrlProp"></th>
</tr>
</thead>
</table>
<input type="hidden" id="idCProp"/>
and this is the controller method
#RequestMapping(value = "/person/loadProponents/{idPerson}/{idCandidature}", method = RequestMethod.GET)
public #ResponseBody DatatablesResponse<Proposal> loadProponents(#PathVariable("idPerson") Long idPerson,#PathVariable("idCandidature") String idCandidature,
#DatatablesParams DatatablesCriterias criterias, HttpServletRequest request) {
//Do some stuff...
}
How can I send idCandidature variable?
I think the better way it's using dataTables API and request variables to do it.
Your view code would be :
<form id="searchForm">
<input type="hidden" id="idCProp"/>
<button class="btn btn-primary" type="button"
th:text="#{proposals.find}"
onclick="findProposals();"></button>
</form>
<table th:if="${entity.formulario == 'formPerson'}" class="table dataTable" dt:deferLoading="10" id="propDT" dt:reloadSelector="#reloadProponent" dt:table="true" dt:sortable="true" dt:displaylength="10" dt:dom='ftip' dt:url="#{/person/loadProponents/__${entity.id}__}" dt:serverside="true">
<thead>
<tr>
<th dt:property="entity.id" dt:renderFunction="nameAndLastName">Persona</th>
<th dt:property="entity.id" dt:sortable="false" dt:renderFunction="getProposalPost">Cargo</th>
<th dt:property="entity.id" dt:sortable="false" dt:renderFunction="getProposalInstitution">Institución</th>
<th dt:property="sendMethod.name" dt:sortable="false">Método de envío</th>
<th class="operations" dt:sortable="false" dt:property="id" dt:renderFunction="idToUrlProp"></th>
</tr>
</thead>
</table>
<script th:inline="javascript">
function findProposals() {
oTable_propDT.columns(2).search($("#idCProp").val());
... // more parameters if you need
oTable_propDT.columns(n).search($("#yoursearchparameterfrominput").val());
oTable_propDT.draw();
}
</script>
In your controller you already have everything you need :
#RequestMapping(value = "/person/loadProponents/{idPerson}", method = RequestMethod.GET)
public #ResponseBody DatatablesResponse<Proposal> loadProponents(#PathVariable("idPerson") Long idPerson, HttpServletRequest request) {
DatatablesCriterias criterias = DatatablesCriterias.getFromRequest(request);
DataSet<Proposal> dataset = proposalService.getProposalsDataset(criterias);
return DatatablesResponse.build(dataset, criterias);
}
Finally you just need to write proposalService#getProposalsDataset(DatatablesCriterias criterias) method that builds Dataset, I think it's nothing special.
Related
I need get data from html to controller with press button also Html data as list.
<thead>
<tr>
<th>Company Name</th>
<th>Book Name</th>
<th>Author</th>
<th>Price</th>
<th>Order</th>
</tr>
</thead>
<tbody>
<tr th:each="Books: ${Books }">
<td th:text="${Books.CompanyName}" />
<td th:text="${Books.BookName}" />
<td th:text="${Books.AuthorName}" />
<td th:text="${Books.Price}" />
<td th:button class="Order-Button" type="button" >Order</button> </td>
Also this is my Controller
#Autowired
private BookRep bookRep;
#RequestMapping(value = "/BookOrder")
ModelAndView submitBookOrder(){
ModelAndView mav = new ModelAndView("BookOrder");
mav.addObject("Books", bookRep.findAll());
return mav;
}
My expect to when user press take order button it will get row datas in controller
I wrote demo code for you. I think it will be useful for you. You can understand what you want by looking at the code
#GetMapping("/Books/{id}")
public String books(#PathVariable("id") Long id, Model model){
model.addAttribute("Books", bookService.getById(id));
return "book-list";
}
<-td> <-a th:href="#{/Books/{id}(id = ${book.id})}">
<-button type="submit" class="btn btn-info col-2" style="min-width: fit-content"> Books List<-/button>
I try to send a parameter applicationNo wrapped inside my form tag.The data inside input generated via javascript snippet. I need to pass this data to my controller, but it always throw null pointer exception, I am not able to figure it out what is the problem.
So kindly suggest me the best way to achieve. I also attached required source code and snapshot.
I want to send the data highlighted in red area in below screenshot.
JSP CODE:
<form action="${baseURL}view_grp_conn_applications" id="grpCreationForm" method="post" commandName="command" >
<div class="col-sm-12">
<table id="addAppTable" class="table table-responsive table-bordered table-striped text-center" id="newField">
<thead>
<tr>
<th>#</th>
<th>Application No.</th>
<th>Name</th>
<th>Mobile No.</th>
<th>E-mail</th>
</tr>
</thead>
<tbody>
<c:set var="i" value="0"></c:set>
<tr>
<td valign="center"><input name="workOrderPostSps[${i}]" id="workOrderPostSps[${i}]" type="checkbox" value="${wo.woPostIdEnc}" onclick="highlightrow(this);" /></td>
<td align="center"><b><input onkeypress="show_list('${i}');" id="appNo1${i}" name="applicationNo" class="form-control start" autocomplete="off" data-validate="required" required="true" placeholder="press key on keyboard"/></b></td>
<td align="left"><span id="appName1${i}"></span></td>
<td align="left"><span id="appContact${i}"></span></td>
<td align="left"><span id="email1${i}"></span></td>
</tbody>
</table>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-turquoise pull-right no-margin " name="saveBtn" id="saveBtn" value="Next >>" onclick="myfunction()">
</div>
</form>
JAVA CODE:
This method open the JSP from where I wish to send the applicationNo
#RequestMapping(value = "/create_group_connection")
public ModelAndView createConnection(Model model) {
ModelAndView mav = new ModelAndView("user/create_group_connection");
Application application = new Application();
mav.addObject("command", application);
return mav;
}
This method will open the jsp where i need to extract that applicationNo
#RequestMapping(value = "/view_grp_conn_applications", method = RequestMethod.POST)
public ModelAndView viewApplications(#ModelAttribute("command")Application application,HttpServletRequest request, HttpSession session) {
ModelAndView mav = new ModelAndView("user/grp_conn_applications");
try {
System.out.println("inside view group applications");
String[] applicationNo = request.getParameterValues("applicationNo");
System.out.println("inside " + applicationNo[0]);
// for (int i = 0; application.length > 0; i++) {
// System.out.println("application number is" + application[i]);
// }
} catch (Exception e) {
System.out.println("Exception occured");
e.printStackTrace();
}
return mav;
}
<td><b><input onkeypress="show_list('${i}');" id="appNo1${i}" name="applicationNo" class="form-control start" autocomplete="off" data-validate="required" required="true" placeholder="press key on keyboard"/></b></td>
you just use the below instead of above
<input type='hidden' id="applicationNo" name="applicationNo" /></b></td>
you just set the application number using Javascript through id of hidden field on any event like onBlur, onKeyPress,Your Controller code is Correct
Controller.java
#Controller
public class DashboardController {
#RequestMapping(value = "/delete", method = RequestMethod.GET)
public String deleteRow(#RequestParam("vehicleNo") int vehicleNo) {
userService.deleteRow(vehicleNo);
System.out.println("delete... " + vReg.getVehicleNo());
return ("/dashboard");
}
}
DaoImpl
public void deleteRow(Integer vehicleNo) {
String Sql = null;
Sql = "delete from vehiclereg where vehicleNo= " + vehicleNo;
jdbcTemplate.update(Sql, vehicleNo);
System.out.println("Deleted Record with vehicleNo = " + vehicleNo);
}
I think no need to passing 2 arguments to this function. Try this one...
jdbcTemplate.update(Sql);
same value returning,
JSP HomePage
<table id="myTable" border="1" cellspacing="10" >
<thead align="center" >
<tr>
<th data-priority="6">ID</th>
<th data-priority="5">Owner Name</th>
<th data-priority="4">Vehicle No</th>
<th data-priority="3">License No.</th>
<th data-priority="2">Address</th>
<th data-priority="1">Pollution Date </th>
<th data-priority="6">Action</th>
</tr>
</thead>
<c:forEach var="vreg" items="${vRegList}" varStatus="loop">
<tbody>
<tr>
<th scope="row">${loop.index+ 1}</th>
<td>${vreg.fullname}</td>
<td>${vreg.vehicleNo}</td>
<td>${vreg.licenceNo}</td>
<td>${vreg.address}</td>
<td>${vreg.date3}</td>
<td> <a class="glyphicon glyphicon-pencil" href=edit?vehicleNo=${vreg.vehicleNo}></a> |
<a class="glyphicon glyphicon-trash" onclick="return confirm('Are you sure you want to delete?')" href="delete?vehicleNo=${vreg.vehicleNo}"></a>|
<a class="glyphicon glyphicon-envelope" href="sms"></a> </td>
</tr>
</tbody>
I have the following form in my Thymleaf page:
<div class="panel-body">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Issue Date</th>
<th>Payment Schedule</th>
<th>Total</th>
<th>Status</th>
<th>Start Date</th>
<th>End Date</th>
<th>Send Invoice</th>
</tr>
</thead>
<tbody>
<tr class="table-row" th:each="p : ${POList}">
<td th:text="${p.issueDate}"></td>
<td th:text="${p.paymentSchedule}"></td>
<td th:text="${p.total}"></td>
<td th:text="${p.status}"></td>
<td th:text="${p.rentalPeriod.startDate}"></td>
<td th:text="${p.rentalPeriod.endDate}"></td>
<td>
<form style='float:left; padding:5px; height:0px' th:object="${po}" th:method="post" th:action="#{'/dashboard/makeAndSendInvoice(email=${po.Email})'}">
<button class="btn btn-default btn-xs" type="submit">Send Invoice</button>
</form>
</td>
</tr>
</tbody>
</table>
</div>
I have tried to send the value of po.Email to the method.
I thought the th:action="#{'/dashboard/makeAndSendInvoice(email=${po.Email})'}"
will make a link like dashboard/makeAndSendInvoice/{Email}
So i tried to get it in the method like this:
#RequestMapping(method=POST, path="makeAndSendInvoice")
public String makeAndSendInvoice(#PathVariable("email") String email){
System.out.println("Invoice is sent to..................."+email);
return "Invoice";
}
but it seems to me it does not work, since it does not recognize my method.
So how can recieve the po.Email in my method
Change th:action to:
th:action="#{/dashboard/makeAndSendInvoice/{email}(email=${po.Email})}"
and add the PathVariable in the RequestMapping value like:
#RequestMapping(method=POST, path="/dashboard/makeAndSendInvoice/{email:.+}")
public String makeAndSendInvoice(#PathVariable("email") String email) {
From Thymeleaf - Link URLs:
Variable templates are also allowed in URL paths, like
#{/order/{orderId}/details(orderId=${orderId})}
<!-- Will produce '/gtvg/order/details?orderId=3' (plus rewriting) -->
view
<!-- Will produce '/gtvg/order/3/details' (plus rewriting) -->
view
From Spring - URI Template Patterns:
In Spring MVC you can use the #PathVariable annotation on a method
argument to bind it to the value of a URI template variable:
#RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(#PathVariable String ownerId, Model model) {
Owner owner = ownerService.findOwner(ownerId);
model.addAttribute("owner", owner);
return "displayOwner";
}
I have a page that show all lists from "dataLansia" table. Like this one :
http://i61.tinypic.com/fliwcm.jpg
And then, I want to make a little improvement. When I pick a date from that date input and I press 'Cari' button, the page show me only the result from 'dataLansia' table where the attribute of 'Tanggal Data' match with the date I pick before. How to do that ?
Here's my stuff (controller, model, view) :
public class Application extends Controller {
//the other code
public static void rekap(){
List rekapProfil=dataLansia.findAll();
render(rekapProfil);
}
}
public class dataLansia extends Model{
public String tensi;
public int guladarah;
public int kolesterol;
public double asamurat;
#Unique
public java.util.Date tglData;
#Lob
public String keluhan;
#ManyToOne(cascade=CascadeType.ALL)
public potensi lans;
#ManyToOne(cascade=CascadeType.ALL)
public lansia lansia;
}
public class lansia extends Model {
#Unique
public int idLansia;
public String namaLansia;
public int umur;
#Lob
public String alamat;
#OneToMany(mappedBy="lansia",cascade=CascadeType.ALL)
public Collection<dataLansia> dataLansia;
public String toString(){
return namaLansia;
}
}
#{extends 'Template/Dashboard.html' /}
#{set title:messages.get('rekap.title') /}
<div class="row">
<input type="date" name="rekapTgl.tglData.format('yyyy-MM-dd')" /> <input type="submit" class="btn btn-info" value="Cari" />
<table class="table table-bordered table-striped responsive">
<thead>
<tr>
<th class="text-center">Tanggal Data</th>
<th class="text-center">ID Lansia</th>
<th class="text-center">Nama Lansia</th>
<th class="text-center">Umur</th>
<th class="text-center">Alamat</th>
<th class="text-center">Tensi<br />(mmHg)</th>
<th class="text-center">Gula Darah<br />(mg/dL)</th>
<th class="text-center">Kolesterol<br />(mg/dL)</th>
<th class="text-center">Asam Urat<br />(mg/dL)</th>
<th class="text-center">Keluhan</th>
<th class="text-center">Potensi / Tidak Potensi</th>
</tr>
</thead>
<tbody>
#{list items:rekapProfil, as:'rD'}
<tr>
<td class="text-center">${rD.tglData.format('dd-MM-yyyy')}</td>
<td class="text-center">${rD.lansia.idLansia}</td>
<td class="text-center">${rD.lansia.namaLansia}</td>
<td class="text-center">${rD.lansia.umur}</td>
<td class="text-center">${rD.lansia.alamat}</td>
<td class="text-center">${rD.tensi}</td>
<td class="text-center">${rD.guladarah}</td>
<td class="text-center">${rD.kolesterol}</td>
<td class="text-center">${rD.asamurat}</td>
<td class="text-center">${rD.keluhan}</td>
<td class="text-center">${rD.lans.namaPotensi}</td>
</tr>
#{/list}
</tbody>
</table>
#{else}
<br /><center><b><i>Belum ada lansia terimunisasi.</i></b></center>
#{/else}
</div>