How to get checkboxes values for JSP page? - html

I can get a single value for an item in jsp page by id. But I can't able to get checkboxes value. How can I get checkboxes value by id or anything else. when i will click in order page it will collect all products name and product price
Screenshot
Code is:
<table class="table table-hover table-striped">
<thead>
<tr>
<th>ID</th>
<th>Choose Product</th>
<th>Product Name</th>
<th>Product Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<%
String Host = "jdbc:mysql://localhost:3306/shopbilling";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(Host, "root", "");
statement = connection.createStatement();
String query = request.getParameter("q");
String data;
if(query != null)
{
data = "select * from products_tbl where product_name like '%"+query+"%' or product_price like '%"+query+"%'";
}
else
{
data = "select * from products_tbl";
}
rs = statement.executeQuery(data);
while (rs.next()) {
%>
<tr>
<td><%=rs.getString("id")%></td>
<td> <input type="checkbox" /> </td>
<td><%=rs.getString("product_name")%></td>
<td><%=rs.getString("product_price")%></td>
<td class="text-center" width="250">
Edit
Delete
</td>
</tr>
<%
}
%>
</tbody>
</table>
Order

Here you can use value tag of <input type="checkbox"> to put the data to user.Then you need to give a attribute 'name' to all of these check-boxes, which you can keep same and then you can grab all the selected values as an array.
Here is a changes you need to make in this code::
<table class="table table-hover table-striped">
<thead>
<tr>
<th>ID</th>
<th>Choose Product</th>
<th>Product Name</th>
<th>Product Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<%
String Host = "jdbc:mysql://localhost:3306/shopbilling";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(Host, "root", "");
statement = connection.createStatement();
String query = request.getParameter("q");
String data;
if(query != null)
{
data = "select * from products_tbl where product_name like '%"+query+"%' or product_price like '%"+query+"%'";
}
else
{
data = "select * from products_tbl";
}
rs = statement.executeQuery(data);
while (rs.next()) {
%>
<tr>
<td><%=rs.getString("id")%></td>
<td> <input type="checkbox" name="products" value ="<%=rs.getString("id")%>" /> </td>
<td><%=rs.getString("product_name")%></td>
<td><%=rs.getString("product_price")%></td>
<td class="text-center" width="250">
Edit
Delete
</td>
</tr>
<%
}
%>
</tbody>
</table>
Order
And while receiving the result you need to use something like this::
<%
String products[] = request.getParameterValues("products");
if (products!= null && products.length != 0) {
out.println("You have selected: ");
for (int i = 0; i < products.length; i++) {
out.println(products[i]);
}
}
%>
Hope this helped you!!
Happy Coding:)

Related

GEt element By id angular 7

hello I need to retrieve the values entered by a form after the post method so I inserted at the service level this code :
list:Client[];
constructor(private http:HttpClient) { }
postClient(formData:Client){
return this.http.post(this.rootURL+'/Clients/',formData);
}
putClient(formData:Client){
return this.http.put(this.rootURL+'/Clients/'+formData.Engagement,formData);
}
getClient(formData:Client){
return this.http.get(this.rootURL+'/Clients/GetClientByName/'+formData.Engagement);
}
and at the component level like this :
getClient(form:NgForm){
this.clientservice.getClient(form.value).subscribe(
res =>{this.client = res as Client}
)
}
and in the HTML code this:
<table class="table table-hover">
<tr>
<th class="tname">Client</th>
<th class="tname">Enagement</th>
<th class="tname">ERP</th>
</tr>
<tr *ngFor="let clt of client">
<td >{{clt.Clientname}}</td>
<td >{{clt.Engagement}}</td>
<td >{{clt.ERP}}</td>
and I can’t get the values by ID with the get I don’t know what is the problem I have neither result or error message
I think your http service(this.rootURL+'/Clients/GetClientByName/) return Client[] not Client.
So, You should cast like this.
this.clientservice.getClient(form.value).subscribe(
res =>{this.client = res as Client[]}
)
But, your response is fixed
{"Engagement":"56789","Clientname":"ClLIENT","ERP":"ERP"}
at component level, no need to edit
getClient(form:NgForm){
this.clientservice.getClient(form.value).subscribe(
res =>{this.client = res as Client}
)
}
you should edit html file.
<table class="table table-hover">
<tr>
<th class="tname">Client</th>
<th class="tname">Enagement</th>
<th class="tname">ERP</th>
</tr>
<tr>
<td >{{client.Clientname}}</td>
<td >{{client.Engagement}}</td>
<td >{{client.ERP}}</td>
</tr>
---------------------------
or need to use ngFor loop, edit component level. and no need to edit component level.
client: Client[] = [];
getClient(form:NgForm){
this.clientservice.getClient(form.value).subscribe(
res =>{
const cli = res as Client;
this.client.length = 0;
Array.prototype.push.apply(this.client, cli)
}
)
}

html scrollable tbody with fixed thead not working

I have a jsp that has multiple <table> in a page. I want only one particular table to be of fixed height and have a scrollable <tbody> and fixed <thead>.
For that I have written a custom CSS as a class so that I can use it in that paricular table. But its not working.
Here is the CSS:
.scroll-tbody{
display: block;
height: 460px;
overflow-y: auto;
}
And here is the JSP snippet:
<table class="table table-hover table-condensed">
<thead>
<tr>
<td><b>&nbsp &nbsp Name</b></td>
<td><b>Phone</b></td>
<td><b>Total Exp.</b></td>
<td><b>Location</b></td>
<td><b>Profile Type</b></td>
<td><b>Domain</b></td>
<td><b>Assigned To</b></td>
<td><b>Status</b></td>
<td><b>Date</b></td>
</tr>
</thead>
<tbody class="scroll-tbody"> <!-- HERE IS THE CUSTOM CSS FOR SCROLLING -->
<%
if(fullList.size()>0)
{
Iterator itr = fullList.iterator();
while(itr.hasNext())
{
FileService fs = new FileService();
File prf = (File)itr.next();
String prfData = prf.getAbsolutePath() + "#";
prfData += fs.readData(prf.getAbsolutePath(),"","profiledata");
prfData = prfData.replace("\\","/");
String[] data = prfData.split("#");
String name = data[1].replace("_"," ");
String phone = data[2];
String totExp = data[3];
String location = data[5];
String prfType = data[6];
String domain = data[7];
String assignedTo = data[8];
String status = data[9];
String prfDate = data[12];
prfData = prfData.replace("'","\\'");
prfData = prfData.replace("\"","\\'");
prfData = prfData.replace("\r\n","^");
System.out.println("---->JSP Data: "+prfData+"\n");
session.setAttribute("dataToEdit",prfData);%>
<tr>
<td><a data-toggle="modal" data-target="#editProfileModal" onClick="getData('<%=prfData%>')"><i class="fa fa-fw fa-file"></i><font size="2"><%=name%></font></a></td>
<td><font size="2"><%=phone%></font></td>
<td><font size="2"><%=totExp%></font></td>
<td><font size="2"><%=location%></font></td>
<td><font size="2"><%=prfType%></font></td>
<td><font size="2"><%=domain%></font></td>
<td><font size="2"><%=assignedTo%></font></td>
<td><font size="2"><%=status%></font></td>
<td><font size="2"><%=prfDate%></font></td>
</tr>
<% }
} %>
</tbody>
</table>
I dont want to write the CSS like: table{...} thead{...} tbody{...} as it will impact all the tables present in that page.
Am I missing something?
Thanks in advance.

Checking all checkboxes of table body with checkbox in table head angularjs

<table id="table" class="table table-bordered font" style="width: 100%; padding-top:10px;">
<thead>
<tr class="bg-primary textalign">
<th>Details</th>
<th>SonVin Id</th>
<th>Date</th>
<th>Venue Name</th>
<th>City</th>
<th>Area</th>
<th>Amount</th>
<th><input type="checkbox" ng-model="checkall" /><span style="margin-left:10px;">Add Invoice</span></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="data in getdataintable">
<td>Details</td>
<td>{{data.sonvinid}}</td>
<td>{{data.date}}</td>
<td>{{data.venuename}}</td>
<td>{{data.location}}</td>
<td>{{data.area}}</td>
<td>{{data.amount}}</td>
<td><input type="checkbox" ng-model="check" /></td>
</tr>
</tbody>
</table>
This is my table,
here I have this line( <th><input type="checkbox" ng-model="check" /><span style="margin-left:10px;">Add All Invoice</span></th>).
This indicates that if a user checked this checkbox, all the other checkboxes should also be checked.
Now what I want is if a particular user clicks on a checkbox on thead, all the checkboxes of td also be check and all the data of <td>{{data.sonvinid}}</td>
should be stored in the array in my controller of angularjs.
$scope.storeall=[];
I want to do this with angularjs,
anyone here who can help me?
make inside your controller some methods like this
method to select All
method for deselect All
method for toggle between above
var app = angular.module('app',[]) // the app module
app.controller('tableCtrl',function($scope){
$scope.storeall = [];
$scope.selectAllFlag = false;
$scope.getdataintable = [
{sonvinid:1, date : "string" , venuename: "String Name" },
{sonvinid:2, date : "string" , venuename: "String Name" }
];
$scope.checkAllToggle = funciton(){
if(!$scope.selectAllFlag){
$scope.selectAll();
$scope.selectAllFlag = true;
}else {
$scope.deselectAll();
$scope.selectAllFlag = false;
}
};
$scope.selectAll = function(){
for (var i = 0 ; i < $scope.data.length ; i++){
$scope.getdataintable[i].selected = true;
$scope.storeall.push($scope.data[i].sonvinid);
}
};
$scope.deselectAll = function(){
for (var i = 0 ; i < $scope.data.length ; i++){
$scope.getdataintable[i].selected = false;
$scope.storeall = [];
}
};
});
to Use it on HTML:
<table id="table" class="table table-bordered font" ng-controller="tableCtrl" style="width: 100%; padding-top:10px;">
<thead>
<tr class="bg-primary textalign">
<th>Details</th>
<th>SonVin Id</th>
<th>Date</th>
<th>Venue Name</th>
<th>City</th>
<th>Area</th>
<th>Amount</th>
<th><input type="checkbox" ng-model="checkAllToggle()" /><span style="margin-left:10px;">Add Invoice</span></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="data in getdataintable">
<td>Details</td>
<td>{{data.sonvinid}}</td>
<td>{{data.date}}</td>
<td>{{data.venuename}}</td>
<td>{{data.location}}</td>
<td>{{data.area}}</td>
<td>{{data.amount}}</td>
<td><input type="checkbox" ng-checked="data.selected" /></td>
</tr>
</tbody>
</table>

How to declare pagination pagesize in web.config or to my view

I have implemented paging using PageList.MVC. Now I need to that i can change the pagesize from my web.config. Any idea ....
Here is my controller:
public ActionResult UsersWhoHaveConsumedFreeCredit(int Id = 1)
{
var result = Manager.GetUsersWhoHaveConsumedFreeCredit();
JavaScriptSerializer serializer = new JavaScriptSerializer();
var model = serializer.Deserialize<List<CallHistory>>(result);
int pageSize = 100;
//int pageNumber = (page ?? 1);
return View(model.ToPagedList(Id, pageSize));
}
And this is my view
#model PagedList.IPagedList<MyYello.Admin.Models.CallHistory>
#{
ViewBag.Title = "UsersWhoHaveConsumedFreeCredit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Users Who Have Consumed Free Credit</h2>
#*<table class="table table-striped table-bordered tablesorter" style="display: block">
<thead>
<tr>
<th>Login</th>
<th>FirstName</th>
<th>LastName</th>
<th>Email</th>
<th>Country</th>
<th>TarrifDesc</th>
<th>CalledNum</th>
<th>CallStart</th>
<th>CallEnd</th>
</tr>
</thead>*#
#foreach (var group in Model.GroupBy(dialed => dialed.Login))
{
var item = group.First();
{
<table>
<tbody>
<th class="custom-padding">Login Id</th>
<th class="custom-padding">Phone</th>
<th class="custom-padding">First Name</th>
<th class="custom-padding">Last Name</th>
<th class="custom-padding">Email</th>
<th class="custom-padding">Country</th>
<tr>
<td class="custom-padding">#item.Login </td>
<td class="custom-padding">#item.Phone</td>
<td class="custom-padding">#item.FirstName</td>
<td class="custom-padding">#item.LastName</td>
<td class="custom-padding">#item.Email</td>
<td class="custom-padding">#item.Country</td>
<th class="custom-padding">Dialed Calls:-</th>
<td class="custom-padding">#string.Join(" - ", group.Select(dialed => dialed.DialedNumber))</td> </tr>
#*<td>#item.FirstName</td>
<td>#item.LastName</td>
<td>#item.Email</td>
<td>#item.Country</td>*#
#*<td>#string.Join(" - ", group.Select(history => history.Phone))</td>*#
<tr> #*<td>#item.TariffDescription</td>*#
</tr>
</tbody>
</table>
<hr />
}
}
#* #Html.PagedListPager( (IPagedList)ViewBag.pageNumber, page => Url.Action ("UsersWhoHaveConsumedFreeCredit", new {page}));*#
<div class="paged-list">
#Html.PagedListPager(Model, Id => Url.Action("UsersWhoHaveConsumedFreeCredit", new { Id }), PagedListRenderOptions.Classic)
</div>
#if (!Model.Any())
{
<h2>No Record Found</h2>
}
I don't think you want this to be controlled from web.config however, you can do this by having a key inside appsettings section in web.config file.
<appSettings>
<key name="pageSize" value="10"/>
</appSettings>
And then you can access inside your code as
int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["pageSize"])
Typically you would want users to control pageSize from the view.

Mysql/jsp Update query

what iam trying to do is get field parameter and run mysql query to update(decrement) value from field quantity, can any one tell me how to do that?
<form method="post">
<table class="bordered">
<thead>
<tr>
<th>No</th>
<th>Drug Name</th>
<th>Strength</th>
<th>Quantity</th>
<th>Dis Qty</th>
</tr>
</thead>
<% try {
String query="select * from drugs";
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(query);
while(rs.next()) {
%>
<tr>
<td><%=rs.getInt("drugid") %></td>
<td><%=rs.getString("drugname") %></td>
<td><%=rs.getString("strength") %></td>
<td><%=rs.getInt("drugquant") %></td>
<td><INPUT TYPE="TEXT" NAME="getint" SIZE="7">
<INPUT TYPE="SUBMIT" VALUE="Dispense" NAME="B1"></td> </tr>
<%
} %> </table>
<%
rs.close();
stmt.close();
conn.close();
} catch(Exception e) {
e.printStackTrace();
}
![enter image description here][1]
%>
</form>
I tryed to use code similar to this but it doesnt work for me :(
<%
String getint = request.getParameter("getint");
String getsize = "";
// check if Update button is clicked
if(request.getParameter("B1") != null) {
// get what user enterd in intbox
getint = request.getParameter("getint");
// DEBUG
PreparedStatement pstmt4;
pstmt4 = conn.prepareStatement("UPDATE drugs SET drugquant=drugquant - "+getint+" WHERE drugid=1");
pstmt4.executeUpdate();
}
%>
you should add and change some following line:
pstmt4 = conn.prepareStatement("UPDATE drugs SET drugquant=?"+"WHERE drugid=?");
INT drugquant = request.getParameter("drugquant");
INT drugid = request.getParameter("drugid");
pstmt4.setString(1, drugquant);
pstmt4.setString(2, drugid);
pstmt4.executeUpdate();
if you had any doubt you should follow this link and give me comment.