I am trying to sum the value of selected checkbox in a table. it works well when i selected the row value indivially but when i check select all checkbox it sum the whole column instead of one specific row value.
My code:
Javascript:
$(document).ready(function()
{
$("#selectAll").click(function()
{
var checked_status = this.checked;
var ntot = 0;
$("input[id=P102]").each(function() //i used column value here.
i want to use row value
{
if (checked_status) ntot += parseInt($(this).val());
this.checked = checked_status;
});
$("#total").val(ntot);
});
});
$(document).ready(function() {
function updateSum() {
var total = 0;
$(".sum:checked").each(function(i, n) {total += parseInt($(n).val());})
$("#total").val(total);
}
$("input.sum").change(updateSum);
updateSum();
});
function SelectAll(obj) {
var table = $(obj).closest('table');
var th_s = table.find('th');
var current_th = $(obj).closest('th');
var columnIndex = th_s.index(current_th)-2;
console.log('The Column is = ' + columnIndex);
table.find('td:nth-child('+(columnIndex)+') input').prop("checked",obj.checked);
}
HTML:
<div class="container">
<div class="row">
<div class="col-md-2">
<?php
$i=1;
$sql="select colStudentNameEnglish from
`tbl_newstudentregistration`
where EXTRACT(YEAR FROM colRegisteredDate)= 2015";
$result = mysql_query($sql) or die(mysql_error());
$data=array();
?>
<style>
table, th, td {
border: 1px solid black;
padding: 5px;
}
table {
border-spacing: 15px;
}
th[colspan="11"] {
text-align: center;
}
</style>
<table style="width:100%;">
<tr align="center" >
<th style="padding:2.5px; width: 10%;" rowspan="2">S.NO</th>
<th style="padding:2.5px; width: 55%;" rowspan="2">StudentID</th>
<th style="padding:2.5px;" colspan="5" style="text-align:center;">subject</th>
<th style="padding:2.5px;" rowspan="2">Sum</th>
</tr>
<tr>
<th>s1 <input type="checkbox" id="selectAll" onclick="SelectAll(this)" /></th>
<th>s2 <input type="checkbox" id="selectAll" onclick="SelectAll(this)"/></th>
<th>s3 <input type="checkbox" id="selectAll"onclick="SelectAll(this)" /></th>
<th>s4 <input type="checkbox" id="selectAll" onclick="SelectAll(this)" /></th>
<th>s5 <input type="checkbox" id="selectAll" onclick="SelectAll(this)"/></th>
</tr>
<li>
<?php
while ($row = mysql_fetch_array($result)){
//$row = mysql_fetch_assoc($result);
$data[]=$row['colStudentNameEnglish'];
$number=$row['colStudentNameEnglish'];
$_SESSION['Roll']=$number;
$str = implode("\n", $data);
$_SESSION['value']=$number;
$_SESSION['url']="index.php?StudentID=$number";
?>
<?php
?>
<tr>
<td><?php echo $i; ?></td>
<td><a href="#" data-id='<?php echo $number; ?>' class="link" id="link">
<?php
echo "$number";
?>
</li>
</a></td>
<td><input type="checkbox" name="checkbox" id="P102"
class="sum" value="10" data-toggle="checkbox"><br></td>
<td><input type="checkbox" name="checkbox" id="P202"
class="sum" value="10" data-toggle="checkbox"><br></td>
<td><input type="checkbox" name="checkbox" id="C031"
class="sum" value="10" data-toggle="checkbox"><br></td>
<td><input type="checkbox" name="checkbox" id="P104"
class="sum" value="10" data-toggle="checkbox"><br></td>
<td><input type="checkbox" name="checkbox" id="P204"
class="sum" value="10" data-toggle="checkbox"><br></td>
<td><input type="text" id="total"><br></td>
</tr>
</div>
<?php $i=$i+1; }?>
</div>
</div>
If SelectAll checkbox is checked,i want to add each row sum to each textfield (as 10 in Image) AnyBody Help Me to Solve this issue???
Thanks in Advance...
Aucun commentaire:
Enregistrer un commentaire