i am working with html table, when i check the checkbox i want to compare td values in a every tr, this condition is working fine for 1st tr from 2nd tr the condition is not working.
HTML Code -
<form role="form" name="conForm" id="conForm">
<span id="error" class="text-danger"></span>
<div class="table-responsive">
<table id="myPTable" class="table table-xss table-hover table-bordered">
<thead>
<tr>
<th><input class="checkall" type="checkbox" name="productcheckbox"> All</</th>
<th class="control-label paddingtop">SI No</th>
<th class="control-label paddingtop">Products</th>
<th class="control-label paddingtop">Pending Qty</th>
<th class="control-label paddingtop">Quantity</th>
<th class="control-label paddingtop">Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td><input class="checkbox checkproduct" type="checkbox" name="check"></td>
<td>1</td>
<td>HMIS HMIS HMIS HMIS</td>
<td class="availableQty">10</td>
<td><input type="number" name="select[]" class="enterQty" value="10" style="width:50px;"></td>
<td>3000</td>
</tr>
<tr>
<td><input class="checkbox checkproduct" type="checkbox" name="check"></td>
<td>2</td>
<td>ERP</td>
<td class="availableQty">1</td>
<td><input type="number" name="select[]" class="enterQty" value="1" style="width:50px;" ></td>
<td>9000</td>
</tr>
<tr>
<td><input class="checkbox checkproduct" type="checkbox" name="check"></td>
<td>3</td>
<td>Inventory</td>
<td class="availableQty">10</td>
<td><input type="number" name="select[]" class="enterQty" value="10" style="width:50px;"></td>
<td>13000</td>
</tr>
<tr><td><button class="btn-info">Save</button></td></tr>
</tbody>
</table>
</form>
Here is the jquery code -
$(".btn-info").on("click",function(){
var check = $('.checkproduct');
if(check.is(':checked')){
var pending="";
$('#myPTable tr').each(function() {
pending += $(this).find(".availableQty").html();
console.log(pending);
});
var enterqty ="";
$('#myPTable tr').each(function() {
enterqty += $(this).find(".enterQty").val();
console.log(enterqty);
});
if(enterqty > pending){
$("#error").html("<p>Quantity must be less than or equal to Pending Qty</p>");
}else if(enterqty== 0){
$("#error").html("<p>you have checked the product please enter quantity</p>");
}
else{
var checked = $('.checkproduct:checked').size();
}
the jquery what i wrote is working for only first row, but it need to be work for every work which will be checked.now only you guys can help me, thanks in advance.
I've reformed your code a bit, but i believe this is what you want.
$(".btn-info").on("click", function() {
var pending =0;
var enterqty=0;
$('#myPTable .checkproduct:checked').each(function() {
pending += (+$(this).closest('tr').find(".availableQty").text());
enterqty += (+$(this).closest('tr').find(".enterQty").val());
});
if (enterqty > pending) {
$("#error").html("<p>Quantity must be less than or equal to Pending Qty</p>");
} else if (enterqty == 0) {
$("#error").html("<p>you have checked the product please enter quantity</p>");
} else {
//var checked = $('.checkproduct:checked').size();
}
});
Demo
$(".btn-info").on("click", function() {
var pending =0;
var enterqty=0;
$('#myPTable .checkproduct:checked').each(function() {
pending += (+$(this).closest('tr').find(".availableQty").text());
enterqty += (+$(this).closest('tr').find(".enterQty").val());
});
if (enterqty > pending) {
$("#error").html("<p>Quantity must be less than or equal to Pending Qty</p>");
} else if (enterqty == 0) {
$("#error").html("<p>you have checked the product please enter quantity</p>");
} else {
//var checked = $('.checkproduct:checked').size();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form role="form" name="conForm" id="conForm">
<span id="error" class="text-danger"></span>
<div class="table-responsive">
<table id="myPTable" class="table table-xss table-hover table-bordered">
<thead>
<tr>
<th><input class="checkall" type="checkbox" name="productcheckbox"> All</</th>
<th class="control-label paddingtop">SI No</th>
<th class="control-label paddingtop">Products</th>
<th class="control-label paddingtop">Pending Qty</th>
<th class="control-label paddingtop">Quantity</th>
<th class="control-label paddingtop">Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td><input class="checkbox checkproduct" type="checkbox" name="check"></td>
<td>1</td>
<td>HMIS HMIS HMIS HMIS</td>
<td class="availableQty">10</td>
<td><input type="number" name="select[]" class="enterQty" value="10" style="width:50px;"></td>
<td>3000</td>
</tr>
<tr>
<td><input class="checkbox checkproduct" type="checkbox" name="check"></td>
<td>2</td>
<td>ERP</td>
<td class="availableQty">1</td>
<td><input type="number" name="select[]" class="enterQty" value="1" style="width:50px;"></td>
<td>9000</td>
</tr>
<tr>
<td><input class="checkbox checkproduct" type="checkbox" name="check"></td>
<td>3</td>
<td>Inventory</td>
<td class="availableQty">10</td>
<td><input type="number" name="select[]" class="enterQty" value="12" style="width:50px;"></td>
<td>13000</td>
</tr>
<tr>
<td><button type="button" class="btn-info">Save</button></td>
</tr>
</tbody>
</table>
Related
when I tick the checkbox and click on the validate button, I want the checkbox to become an validation icon within the table like the example below.
Hello, when I tick the checkbox and click on the validate button, I want the checkbox to become an validation icon within the table like the example below.
Here is my HTML:
<table class="table table-bordered" id="mytable">
<tr>
<th><input type="checkbox" id="check_all"></th>
<th>matricule</th>
<th>salary</th>
<th>number day</th>
<th>premium</th>
</tr>
<tr>
<td><input type="checkbox" class="checkbox"></td>
<td>1</td>
<td>5000</td>
<td>2</td>
<td><input type="text" name="prime" class="form-control" value="0"></td>
</tr>
<tr>
<td><input type="checkbox" class="checkbox"></td>
<td>2</td>
<td>6000</td>
<td>2</td>
<td><input type="text" name="prime" class="form-control" value="0"></td>
</tr>
<tr>
<td><input type="checkbox" class="checkbox"></td>
<td>1</td>
<td>7000</td>
<td>1</td>
<td><input type="text" name="prime" class="form-control" value="0"></td>
</tr>
</table>
<div class="form-group col-md-offset-5 ">
<button class="btn btn-success add-all" type="submit" id="hide">Pointage men</button>
</div>
Here is my JQuery:
$(document).ready(function() {
$('#check_all').on('click', function(e) {
if ($(this).is(':checked', true)) {
$(".checkbox").prop('checked', true);
} else {
$(".checkbox").prop('checked', false);
}
});
$('.checkbox').on('click', function() {
if ($('.checkbox:checked').length == $('.checkbox').length) {
$('#check_all').prop('checked', true);
} else {
$('#check_all').prop('checked', false);
}
});
$("#hide").click(function() {
$("tr").each(function(i, r) {
if (i > 0 && $(r).find("input").first().prop("checked")) {
}
});
});
})
If i got you clearly , you want to replace the input checked to "validate icon" like ✔
so we will use jQuery replaceWith() Method and replace input to UTF-8 Miscellaneous Symbols : ✔
like:
$("#hide").click(function() {
$("tr").each(function(i, r) {
if (i > 0 && $(r).find("input").first().prop("checked")) {
$(r).find("input").first().replaceWith('<span style="color: green;font-weight: bolder;">✔</span>');//✓ -
}
});
});
I have a table in razor and I sent the data by ViewBag from controller
<table>
<thead>
<tr>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th>Count</th>
</tr>
</thead>
<tbody>
#foreach(var item in ViewBag.Products)
{
<tr>
<td>#item.Name</td>
<td>#item.Category</td>
<td>
<input type="text" class="form-control" value="#item.Price" />
</td>
<td>
<input type="text" class="form-controll" value="#item.Count" />
</td>
</tr>
}
</tbody>
</table>
<input type="text" class="form-control" value="#Model.TotalPrice" />
I want to multiple count and price of each row and put it in another input using javascript. and when the user change the value of input, that input that holds the value could change automatically.
if anyone can help me i would be appreciated.
If you are using jquery then it can be achieve as below.
You can update your total on every key press in price or count input.
Add some class to your input. In my example I've took class as price, and class total for display sum.
Add keyup event as below. Also trigger it on $(document).ready to initially set the total value.
$('.price').on('keyup', function() {
var val = +$(this).val();
var valSibling = +$(this).parent().siblings('td').find('.price').val();
if (isNaN(val) || isNaN(valSibling))
return;
$(this).parent().siblings('td').find('.total').val(val * valSibling);
var finaltotal = 0;
$('.total').each(function() {
if(!isNaN($(this).val()))
finaltotal += Number($(this).val());
});
$('.finaltotal').val(finaltotal);
});
$(document).ready(function(){
$('.price').trigger('keyup');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<table>
<thead>
<tr>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th>Count</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name1</td>
<td>Category1</td>
<td><input type="text" class="price form-control" value="40" /></td>
<td><input type="text" class="price form-control" value="4" /></td>
<td><input type="text" class="total form-control" /></td>
</tr>
<tr>
<td>Name2</td>
<td>Category2</td>
<td><input type="text" class="price form-control" value="20" /></td>
<td><input type="text" class="price form-control" value="2" /></td>
<td><input type="text" class="total form-control" /></td>
</tr>
</tbody>
</table>
<input type="text" class="finaltotal form-control" />
var inputs = $('#container input');
inputs.keyup(function() {
var arr = inputs.toArray();
var sum = 0;
for (var i = 0; i < arr.length / 2; i++)
sum += arr[i * 2].value * arr[i * 2 + 1].value;
$('#result').val(sum);
})
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<thead>
<tr>
<th>Count</th>
<th>Price</th>
<tr>
</thead>
<tbody id='container'>
<tr>
<td><input type='number' value='1' /></td>
<td><input type='number' value='2' /></td>
</tr>
<tr>
<td><input type='number' value='10' /></td>
<td><input type='number' value='20' /></td>
</tr>
</tbody>
</table>
Total: <input type='number' readonly id='result' readonly value='202' />
I want to multiple count and price of each row and put it in another input using javascript.
You can add another td in each tr. Then loop through all the tbody tr to calculate the value:
var tr = document.querySelectorAll('tbody tr');
function calculate(){
tr.forEach(function(el){
var td = el.querySelectorAll('td');
// If there is invalid number in input then no change in total.
if (isNaN(td[2].querySelector('input').value) || isNaN(td[3].querySelector('input').value))
return;
td[4].querySelector('input').value = td[2].querySelector('input').value * td[3].querySelector('input').value;
});
}
calculate();
.form-control{
width: 50px;
}
<table>
<thead>
<tr>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th>Count</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name1</td>
<td>Category1</td>
<td><input type="text" oninput="calculate()" class="form-control" value="40" /></td>
<td><input type="text" oninput="calculate()" class="form-control" value="4" /></td>
<td><input type="text" class="form-control" /></td>
</tr>
<tr>
<td>Name2</td>
<td>Category2</td>
<td><input type="text" oninput="calculate()" class="form-control" value="20" /></td>
<td><input type="text" oninput="calculate()" class="form-control" value="2" /></td>
<td><input type="text" class="form-control" /></td>
</tr>
<tr>
<td>Name3</td>
<td>Category3</td>
<td><input type="text" oninput="calculate()" class="form-control" value="30" /></td>
<td><input type="text" oninput="calculate()" class="form-control" value="3" /></td>
<td><input type="text" class="form-control" /></td>
</tr>
</tbody>
</table>
I have added new rows dynamically with jQuery and applied a function but I want to apply this differently for each row. right now its applying commonly for every rows.
HTML code:
<button type="button" id="addBankRow" >add</button>
<table class="table table-bordered" id="dynamic_field_bank">
<thead>
<tr>
<th width="10%">Deposit Date</th>
<th width="10%">Deposit Method</th>
<th width="25%">Bank Title</th>
<th width="25%">Account No</th>
<th width="20%">Deposit Amount</th>
<th width="10%">#</th>
</tr>
</thead>
<tbody>
<tr class="bank_deposit">
<td><p>12/10/17</p></td>
<td>
<select class="form-control" id="deposit_method">
<option>Bank</option>
<option>Vault</option>
</select>
</td>
<td><input id="bank_title" name="bank_title" required="required" type="text"></td>
<td>
<select class="form-control" id="bank_ac_no">
<option>151035654646001</option>
<option>151035654646002</option>
<option>151035654646003</option>
</select>
</td>
<td><input id="deposit_amount" name="deposit_amount" required="required" type="number" min="0"></td>
<td>
</td>
</tr>
</tbody>
</table>
Jquery Codes:
var i=1;
$('#addBankRow').click(function(){
i++;
$('#dynamic_field_bank').append('<tr id="row'+i+'" class="dynamic-added" ><td><p>12/10/17</p></td><td><select class="form-control" id="deposit_method"><option>Bank</option><option>Vault</option></select></td><td><input id="bank_title" name="bank_title" required="required" type="text"></td><td><select class="form-control" id="bank_ac_no"><option>151035654646001</option><option>151035654646002</option><option>151035654646003</option></select></td><td><input id="deposit_amount" name="deposit_amount" required="required" type="number" min="0"></td><td>X</td></tr>');
});
$(document).on('click', '.btn_remove', function(e){
e.preventDefault();
var button_id = $(this).attr("id");
$('#row'+button_id+'').remove();
});
$("tbody").on('change', "#deposit_method", function() {
if ($(this).val() == 'Vault'){
$('#bank_title, #bank_ac_no').hide();
}
else if ($(this).val() == 'Bank'){
$("tr.bank_deposit").each(function (){
$('#bank_title, #bank_ac_no').show();
});
}
});
When I am changing the deposit method I want to hide/show the two fields in each row but its applying in every row.
see the demo with codes:
https://jsfiddle.net/wasid/33qp9ewn/3/
You can add an attribute (like row-id) for inputs to determine which row has been processed. I modified HTML and Javascript codes. Also, you can take a look modifed codes as Fiddle Demo
HTML
<button type="button" id="addBankRow" >add</button>
<table class="table table-bordered" id="dynamic_field_bank">
<thead>
<tr>
<th width="10%">Deposit Date</th>
<th width="10%">Deposit Method</th>
<th width="25%">Bank Title</th>
<th width="25%">Account No</th>
<th width="20%">Deposit Amount</th>
<th width="10%">#</th>
</tr>
</thead>
<tbody>
<tr class="bank_deposit">
<td><p>12/10/17</p></td>
<td>
<select class="form-control" id="deposit_method" row-id="0">
<option>Bank</option>
<option>Vault</option>
</select>
</td>
<td><input id="bank_title" name="bank_title" required="required" type="text" row-id="0"></td>
<td>
<select class="form-control" id="bank_ac_no" row-id="0">
<option>151035654646001</option>
<option>151035654646002</option>
<option>151035654646003</option>
</select>
</td>
<td><input id="deposit_amount" name="deposit_amount" required="required" type="number" min="0" row-id="0"></td>
<td>
</td>
</tr>
</tbody>
</table>
Javascript;
var i=1;
$('#addBankRow').click(function(){
i++;
$('#dynamic_field_bank').append('<tr id="row'+i+'" class="dynamic-added" ><td><p>12/10/17</p></td><td><select class="form-control" id="deposit_method" row-id="'+i+'"><option>Bank</option><option>Vault</option></select></td><td><input id="bank_title" name="bank_title" required="required" type="text" row-id="'+i+'"></td><td><select class="form-control" id="bank_ac_no" row-id="'+i+'"><option>151035654646001</option><option>151035654646002</option><option>151035654646003</option></select></td><td><input id="deposit_amount" name="deposit_amount" required="required" type="number" min="0" row-id="'+i+'"></td><td>X</td></tr>');
});
$(document).on('click', '.btn_remove', function(e){
e.preventDefault();
var button_id = $(this).attr("id");
$('#row'+button_id+'').remove();
});
$("tbody").on('change', "#deposit_method", function() {
var rowId = $(this).attr("row-id");
if ($(this).val() == 'Vault'){
$("#bank_title[row-id='"+rowId+"'], #bank_ac_no[row-id='"+rowId+"']").hide();
}
else if ($(this).val() == 'Bank'){
$("#bank_title[row-id='"+rowId+"'], #bank_ac_no[row-id='"+rowId+"']").show();
}
});
I have a table inside a form, the rows and data inside the rows are generated from the back end automatically. Well all the columns are disabled, so that the user can not edit it but one column is editable. on submit compare the value from editable column and value in quanity column and display a alert message if value of editable is more.
function validateAvaliable(){
var aproducts = document.getElementById("available-quanity").value;
var sproducts = document.getElementById("send-quanity").value;
console.log(aproducts);
console.log(sproducts );
if (aproducts < sproducts) {
alert("send products are more");
return false;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="container-fluid">
<form onsubmit="validateAvaliable()" class="available-products-table" id="available-products-table" name="available-products">
<table class="table">
<fieldset>
<legend>Avaliable Products</legend>
<thead>
<tr>
<th>S.no</th>
<th>Product Name</th>
<th>Product ID</th>
<th>Quanity</th>
<th>Brand</th>
<th>Color</th>
<th>Status</th>
<th>Quanity</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" name="available-sno" id="available-sno" disabled value="1"></td>
<td><input type="text" name="available-name" id="available-name" disabled value="shoes"></td>
<td><input type="text" name="available-id" id="available-id" disabled value="123"></td>
<td><input type="number" name="available-quanity" id="available-quanity" disabled value="50"></td>
<td><input type="text" name="available-brand" id="available-brand" disabled value="adidas"></td>
<td><input type="text" name="available-color" id="available-color" disabled value="black"></td>
<td><input type="checkbox" name="product-status" id="product-status"></td>
<td><input type="number" name="send-quanity" id="send-quanity" required></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>Enter Franchise ID</td>
<td><input type="number" name="send-franchise-is" id="product-status" required></td>
<td><input type="submit" name="submit" value="submit" class="btn btn-primary"></td>
</tr>
</tbody>
</fieldset>
</table>
</form>
</div>
When I try to debug it it's getting the values but if statement is skipping.
I need the code in javascript or Jquery.
Thanks in Advance
Added parseInt() and it did the trick:
function validateAvaliable() {
var aproducts = parseInt(document.getElementById("available-quanity").value);
var sproducts = parseInt(document.getElementById("send-quanity").value);
console.log(aproducts);
console.log(sproducts);
if (aproducts < sproducts) {
alert("send products are more");
return false;
}
return true;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="container-fluid">
<form onsubmit="return validateAvaliable()" class="available-products-table" id="available-products-table" name="available-products">
<table class="table">
<fieldset>
<legend>Avaliable Products</legend>
<thead>
<tr>
<th>S.no</th>
<th>Product Name</th>
<th>Product ID</th>
<th>Quanity</th>
<th>Brand</th>
<th>Color</th>
<th>Status</th>
<th>Quanity</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" name="available-sno" id="available-sno" disabled value="1"></td>
<td><input type="text" name="available-name" id="available-name" disabled value="shoes"></td>
<td><input type="text" name="available-id" id="available-id" disabled value="123"></td>
<td><input type="number" name="available-quanity" id="available-quanity" disabled value="50"></td>
<td><input type="text" name="available-brand" id="available-brand" disabled value="adidas"></td>
<td><input type="text" name="available-color" id="available-color" disabled value="black"></td>
<td><input type="checkbox" name="product-status" id="product-status"></td>
<td><input type="number" name="send-quanity" id="send-quanity" required></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>Enter Franchise ID</td>
<td><input type="number" name="send-franchise-is" id="product-status" required></td>
<td><input type="submit" name="submit" value="submit" class="btn btn-primary"></td>
</tr>
</tbody>
</fieldset>
</table>
</form>
</div>
I've following HTML table:
<table id="blacklistgrid_1" class="table table-bordered table-hover table-striped">
<thead>
<tr id="jumbo">
<th style="vertical-align:middle">Products</th>
<th style="vertical-align:middle">Pack Of</th>
<th style="vertical-align:middle">Quantity</th>
<th style="vertical-align:middle">Volume</th>
<th style="vertical-align:middle">Unit</th>
<th style="vertical-align:middle">Rebate Amount</th>
</tr>
</thead>
<tbody class="apnd-test">
<tr id="reb1_1">
<td><input type="text" name="pack[1]" id="pack_1" value="" class="form-control" size="8"/></td>
<td><input type="text" name="quantity[1]" id="quantity_1" value="" class="form-control" size="8"/></td>
<td><input type="text" name="volume[1]" id="volume_1" value="" class="form-control" size="8"/></td>
<td><input type="text" name="amount[1]" id="amount_1" value="" class="form-control" size="9"/></td>
</tr>
</tbody>
<tfoot>
<tr id="reb1_2">
<td><button style="float:right; margin-bottom: 20px" class="products" type="button" class="btn btn-default" onclick=""> Add</button></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tfoot>
</table>
Now I want to get the id of first row from . The id in this case is reb1_1. For getting it I tried below code but it didn't work.
$(document).ready(function() {
$('.products').click(function () {
var row = $(this).closest('table tbody:tr:first').attr('id');
alert(row);
});
});
Thank you.
first go to table tbody then find tr then filter first row like below
var row = $(this).closest('table').find(' tbody tr:first').attr('id');
or can try
var row = $(this).closest('table').find(' tbody tr:eq(0)').attr('id');
reference :first and :eq
Make changes as shown in demo :-
http://jsfiddle.net/avmCX/38/
$(document).ready(function() {
$('.products').click(function () {
var row = $(this).closest('table').find('tbody tr:first').attr('id');
alert(row);
});
});
For more info have a look here :-
http://api.jquery.com/first/
change your function to this:
$(document).ready(function() {
$('.products').click(function () {
var id = $(this).closest("table").find("tbody>tr").first().attr("id");
alert(id);
});
});
this will show you the id.