Table edit button in loop is hiding and showing the first row of table with each edit button click. I need to hide and show each row in loop with its own edit button.
<?php
$counter = 0;
foreach($db->getRecordSet($sqlRecord) as $row){ $counter += 1;
?>
<tr id="rowDetails">
<td> <?php echo($counter); ?> </td>
<td > <?php echo($row['item_code']); ?> </td>
<td > <?php echo($row['item_name']); ?> </td>
<td > <?php echo($row['description']); ?> </td>
<td > <?php echo($row['quantity']); ?> </td>
<td > <?php echo($row['p_cost']); ?> </td>
<td ><input type="button" name="edit" id="edit" value="edit" onClick="hideRow()" /></td>
</td>
</tr>
<tr id="editContent" style="display:none;">
<td class="w10" id="row1"><input type="text" class="form-control" name="item_code" id="item_code" value="<?php echo($row['item_code']); ?>" required="required" /></td>
</tr>
<?php
}
?>
</tr>
<?php } ?>
</table>
-------------------------
function hideRow(){
if(
document.getElementById('editContent').style.display=='none') {
document.getElementById('editContent').style.display='';
document.getElementById('rowDetails').style.display='none';
} else {
document.getElementById('editContent').style.display='none';
document.getElementById('rowDetails').style.display='';
}
try to use jquery bro.
change this line of code:
<td ><input type="button" name="edit" id="edit" value="edit" onClick="hideRow()" /></td>
to :
<td ><input type="button" name="edit" class="hide_button" /></td>
then change :
function hideRow(){
if(
document.getElementById('editContent').style.display=='none') {
document.getElementById('editContent').style.display='';
document.getElementById('rowDetails').style.display='none';
} else {
document.getElementById('editContent').style.display='none';
document.getElementById('rowDetails').style.display='';
}
To:
$( document ).ready(function() {
$(".hide_button").click(function(){
$(this).parents('tr').hide();
});
});
Related
<?php
$con = mysql_connect('localhost','root','');
$db = mysql_select_db("demo",$con);
?>
<!doctype html>
<html>
<head>
<title>Demo</title>
<!--JAVASCRIPT -->
<script src="http://code.jquery.com/jquery-1.9.1.min.js" ></script>
<script>
$(document).ready(function(){
$("#myTable td").click(function() {
var column_num = parseInt( $(this).index() ) + 1;
var row_num = parseInt( $(this).parent().index() )+1;
//$("#result").html( "Row_num =" + row_num + " , Column_num ="+ column_num );
alert(row_num);
});
});
</script>
</head>
<body>
<!--<div id="result"> </div>-->
<table id="myTable" border="1" style="border-collapse: collapse;" cellpadding="8">
<tr>
<th>Name</th>
<th>Qty</th>
<th>Amount</th>
<th>Total</th>
</tr>
<?php
$sQ = "select * from tbl_demo";
$eQ = mysql_query($sQ);
while($fQ=mysql_fetch_array($eQ))
{
$name = $fQ['Name'];
$qty = $fQ['Qty'];
$amt = $fQ['Amount'];
//$total
$total = $fQ['Total'];
?>
<tr>
<td><input type="text" name="name" value="<?php echo $name; ?>" onchange="add($qty,$amt)"/> </td>
<td><input type="number" name="name" value="<?php echo $qty; ?>"/></td>
<td><input type="number" name="name" value="<?php echo $amt; ?>"/></td>
<td><input type="number" name="name" value="<?php echo $total; ?>"/></td>
</tr>
<?php
}
?>
<tr>
<td colspan="3" style="padding-left:400px"> Total </td>
<td class="navigateTest"> <input type="number" name="name" value=""/> </td>
</tr>
<tr>
<td colspan="4" class="navigateTest"> <input type="button" name="submit" id="submit" value="Submit"/> </td>
</tr>
</table>
</body>
</html>
Here is my code. I get index value of particular row nd column bt i can not find value of that cell.. I tried to solve that problem bt i can not understand code which i found..which are the different ways for that??
How i get data of particular cell?????
plz redirect me at right direction...
I am trying to get a filter working for a table. What I would like is checkboxes to filter (hide) rows based on whether they're checked or not and if the value is present in the table cell. I have it somewhat working, but it will only sort one of the checkboxes. Also, if there are multiple values in the table cell being filtered, that row is hidden. Thoughts? Thank you in advance for any help. I'm really struggling with this for some reason!
Here is the code I have been using:
$("input:checkbox").click(function () {
var showAll = true;
$('tr').not('.first').hide();
$('input[type=checkbox]').each(function () {
if ($(this)[0].checked) {
showAll = false;
var status = $(this).attr('rel');
var value = $(this).val();
$('td.' + 'status' + '[rel="' + value + '"]').parent('tr').show();
}
});
if(showAll){
$('tr').show();
}
});
The markup is as follows (please excuse some messiness, its Wordpress)
<div class="grants-filter">
<h3 class="filter-title">Filter Scholarships</h3><br/>
<h3>Year of Study:</h3>
<input type="checkbox" name="chx" rel="status" value="Freshman">Freshman
<input type="checkbox" name="chx" rel="status" value="Sophmore">Sophmore
<input type="checkbox" name="chx" rel="status" value="Junior">Junior
<input type="checkbox" name="chx" rel="status" value="Senior">Senior
<br/><br/><h3>Duration:</h3>
<input type="checkbox" rel="duration" value="Summer">Summer
<input type="checkbox" rel="duration" value="Semester">Semester
<input type="checkbox" rel="duration" value="Full Year or More">Full Year or More
</div>
<div class="grants-listing">
<table border="1" id="table" class="grants-table">
<thead>
<tr class="first">
<th>Title</th>
<th>Description</th>
<th>Field</th>
<th>Duration</th>
<th>Year of Study</th>
<th>Other</th>
<th>Procedure</th>
<th>URL</th>
</tr>
</thead>
<tbody>
<?php
$grargs = array ( 'post_type' => 'scholarship');
$grant_query = new WP_Query( $grargs );
while ( $grant_query -> have_posts() ):
$grant_query -> the_post(); ?>
<tr class="grant-detail">
<td>
<?php the_title(); ?>
</td>
<td><?php echo wp_trim_words(get_the_content(), 25 ); ?></td>
<td class="fields" rel="<?php echo get_field('fields'); ?>"><?php echo the_field('fields'); ?></td>
<td class="duration" rel="<?php echo the_field('desired_duration'); ?>"><?php echo the_field('desired_duration'); ?></td>
<td class="status" rel="<?php echo the_field('year-of-study'); ?>"><?php echo the_field('year-of-study'); ?></td>
<td class="other" rel="<?php echo the_field('other'); ?>"><?php echo the_field('other'); ?></td>
<td class="procedure" rel="<?php echo the_field('procedure'); ?>"><?php echo the_field('procedure'); ?></td>
<td class="url"><?php echo get_field('url'); ?></td>
</tr>
<?php endwhile;
wp_reset_postdata(); //reset the first query
?>
</tbody>
</table>
As I think you just are calling the first checkbox [0]
if ($(this)[0].checked) {
call all by inserting someting like
$('input[type=checkbox]').each(function () {
if (this.checked) {
console.log($(this).val());
}
});
Your code works fine, here is a simplified version
$("input:checkbox").click(function() {
var showAll = true;
$('tr').not('.first').hide();
$('input:checkbox:checked').each(function() {
showAll = false;
var status = $(this).attr('rel');
var value = $(this).val();
$('td.' + 'status' + '[rel="' + value + '"]').parent('tr').show();
});
if (showAll) {
$('tr').show();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="grants-filter">
<h3 class="filter-title">Filter Scholarships</h3>
<br/>
<h3>Year of Study:</h3>
<input type="checkbox" name="chx" rel="status" value="Freshman">Freshman
<input type="checkbox" name="chx" rel="status" value="Sophmore">Sophmore
<input type="checkbox" name="chx" rel="status" value="Junior">Junior
<input type="checkbox" name="chx" rel="status" value="Senior">Senior
</div>
<div class="grants-listing">
<table border="1" id="table" class="grants-table">
<thead>
<tr class="first">
<th>Title</th>
<th>Description</th>
<th>Fields</th>
<th>Status</th>
<th>Procedure</th>
</tr>
</thead>
<tbody>
<tr class="grant-detail">
<td>
Name
</td>
<td>Description</td>
<td class="fields" rel="Freshman">Freshman</td>
<td class="status" rel="Freshman">Freshman</td>
<td class="procedure" rel="Freshman">Freshman</td>
</tr>
<tr class="grant-detail">
<td>
Name
</td>
<td>Description</td>
<td class="fields" rel="Sophmore">Sopho</td>
<td class="status" rel="Sophmore">Sopho</td>
<td class="procedure" rel="Sophmore">Sopho</td>
</tr>
<tr class="grant-detail">
<td>
Name
</td>
<td>Description</td>
<td class="fields" rel="Junior">Junior</td>
<td class="status" rel="Junior">Junior</td>
<td class="procedure" rel="Junior">Junior</td>
</tr>
</tbody>
</table>
I would like to ask, why I can't able to refresh two html tables? only 1? is there something wrong with the Javascript code? I already tried to declare 2 variables and tried to create to script for each and still no luck and I tried to change the variables name to unique one but still no luck. My full code below:
<!-- SALES.HTML -->
<div class="col-sm-5 shadow">
<p>
<h3><center>Hyukies</center></h3>
<center>Catbalogan, City</center>
<center><span class="glyphicon glyphicon-time"></span> <?php include('time.php'); ?></center> <br />
<div id="transactions_left">
<?php include_once('salestable.php')?>
</div>
</p>
<div class="row">
<div class="col-sm-12"><b>
<p>
<?php include_once('sales_count.php')?>
<script>
var table = $("#salestableid");
var refresh = function() {
table.load("salestable.php", function() {
setTimeout(refresh, 1);
});
};
refresh();
var table1 = $("#sales_countid");
var refresh1 = function() {
table.load("sales_count.php", function() {
setTimeout(refresh1, 1);
});
};
refresh1();
</script>
<button type="button" class="btn btn-danger btn-md" data-toggle='modal' data-target='#cancel'>Cancel</a>
<button type="button" class="btn btn-warning btn-md" data-toggle='modal' data-target='#hold'>Hold</button>
<button type="button" class="btn btn-success btn-md pull-right" data-toggle='modal' data-target='#payment' >Payment</button>
<br /><br />
<center>Thank you and have a Nice day!</center>
</p>
</div>
</div>
</div>
<!-- End of Page SALES.HTML -->
<!-- SALESTABLE.PHP -->
<?php require_once("../includes/db_connection.php"); ?>
<table class="table table-hover" id='salestableid'>
<thead>
<tr class= "default" style="background-color: #e18728">
<th><font color = "white">Action</font></th>
<th width="200"><font color = "white">Product</th>
<th><font color = "white">Quantity</th>
<th width="75"><font color = "white">Price</th>
<th width ="90"><font color = "white">Total</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$user_query = mysqli_query($connection, "SELECT saleslog.itemid, saleslog.qty, saleslog.date, saleslog.total, saleslog.id, items.`name`, items.description, items.retailprice FROM saleslog LEFT JOIN items ON items.id = saleslog.itemid")or die(mysqli_error());
while($row = mysqli_fetch_array($user_query)){
$id = $row['id'];
?>
<tr>
<td></td>
<td><?php echo $row['name']; ?></td>
<td>      <?php echo $row['qty']; ?></td>
<td> <?php echo number_format($row['retailprice'],2); ?></td>
<?php
$total = $row['qty'] * $row['retailprice'];
?>
<td><?php echo number_format($total,2); ?></td>
</tr>
<?php } ?>
</tr>
</tbody>
</table>
<!-- End of Page SALESTABLE.PHP -->
<!--SALES_COUNT.PHP -->
<?php require_once("../includes/db_connection.php"); ?>
<?php
$count_client= mysqli_query($connection, "SELECT * FROM saleslog");
$count = mysqli_num_rows($count_client);
?>
<?php
$totalsales= mysqli_query($connection, "SELECT SUM(total) AS totalpritems FROM saleslog");
while($totalperitems = mysqli_fetch_array($totalsales)){
$total_sales = $totalperitems['totalpritems'];
}
?>
<table class='table' style="border-style: dotted" id="sales_countid">
<tr class='' style="border-style: dotted">
<td width="50" >Total Items: </td>
<td width="50"align="right"><?php echo $count; ?></td>
<td width="50" >Total: </td>
<td width="50"align="right" >P <?php echo number_format($total_sales,2); ?></td>
</tr>
<tr class='' style="border-style: dotted">
<td width="50">Discount: </td>
<td width="50"align="right">0.00 </td>
<td width="50">Tax: </td>
<td width="50"align="right">0.00 </td>
</tr>
<tr style="border-style: dotted">
<td width>Total Payable: </td>
<td align="right">P <?php echo number_format($total_sales,2); ?></td>
<td></td>
<td></td>
</tr>
</table>
<!--End of Page SALES_COUNT.PHP -->
In your refresh code for table1, you refresh table instead.
table1.load(...) instead of table.load(...) in the code below.
var table1 = $("#sales_countid");
var refresh1 = function() {
table.load("sales_count.php", function() {
setTimeout(refresh1, 1);
});
};
I have a check-box for each user and the value of the check-box is the user id
What is the best suitable JavaScript Which disables the delete a.button until have clicked the correct corresponding check-box
If button is disabled I would like to be able to use bootstraps disabled="disabled"
<div class="table-responsive" id="user">
<table class="table table-bordered table-hover">
<thead>
<tr>
<td></td>
<td><b><a><?php echo $column_username;?></a></b></td>
<td><b><a><?php echo $column_status;?></a></b></td>
<td><b><a><?php echo $column_date_added;?></a></b></td>
<td class="text-right"><b><?php echo $column_action;?></b></td>
</tr>
</thead>
<tbody>
<?php if ($users) { ?>
<?php foreach ($users as $user) { ?>
<tr>
<td class="text-center">
<input type="checkbox" name="selected" value="<?php echo $user['user_id']; ?>" />
</td>
<td class="text-left"><?php echo $user['username']; ?></td>
<td class="text-left"><?php echo $user['status']; ?></td>
<td class="text-left"><?php echo $user['date_added']; ?></td>
<td class="text-right">
<i class="fa fa-pencil"></i> Edit User
<i class="fa fa-pencil"></i> Delete User
</td>
</tr>
<?php } ?>
<?php } ?>
</tbody>
</table>
First, all the delete buttons should be disabled when you first load the HTML. You can add the disabled='disabled' attribute to each delete button in your PHP code.
Then in JavaScript you look for a change event on the checkbox:
$("input[type='checkbox']").change(function() {
var thisRow = $(this).parents("tr");
if(this.checked) {
thisRow.find("a.btn-danger").attr("disabled", false);
} else {
thisRow.find("a.btn-danger").attr("disabled", "disabled");
}
});
I want to sum table rows dynamically as the user inputs values. Since the number of rows can vary I want to use a php loop to accomplish this. I'm having problems getting the loop to work correctly. If, for example, there are two individual rows to sum only the bottom row will be summed.
<script type="text/javascript">
var sumScoreF = function(a) {
var rowtotal = 0;
n = new Array();
for (i = 1; i <= 3; i++) {
if (parseInt(document.getElementById(a + i).value) > 0) {
n[i] = parseInt(document.getElementById(a + i).value);
rowtotal += n[i];
}
}
document.getElementById(a + 'total').value = rowtotal;
};
</script>
The following is an example of a single row sum. The total column continues to be summed as values are entered. This is a nice effect but not necessary.
<table>
<tr>
<td>col1</td>
<td>col2</td>
<td>col3</td>
<td>total</td>
</tr>
<tr>
<td>
<input name="p1g1" id="p1g1" type="text" value="" onChange="sumScoreF('p1g')" />
</td>
<td>
<input name="p1g2" id="p1g2" type="text" value="" onChange="sumScoreF('p1g')" />
</td>
<td>
<input name="p1g3" id="p1g3" type="text" value="" onChange="sumScoreF('p1g')" />
</td>
<td>
<input name="p1gtotal" id="p1gtotal" type="text" value="" />
</td>
</tr>
</table>
Here is my attempt at looping additional rows using php and javascript. The first row will not sum but the second row will. I think the problem is with the javascript variable "m" in that it goes directly to the last row but I can't figure out how to fix it.
<table>
<tr>
<td>col1</td>
<td>col2</td>
<td>col3</td>
<td>total</td>
</tr>
<?php for($j=1;$j<=3;$j++) { ?>
<script type="text/javascript">
var k = <?php echo json_encode($j); ?>;
var m = 'p'+k+'g';
</script>
<tr>
<td>
<input name="<?php echo 'p'.$j.'g1'; ?>" id="<?php echo 'p'.$j.'g1'; ?>" type="text" onChange="sumScoreF('m')" />
</td>
<td>
<input name="<?php echo 'p'.$j.'g2'; ?>" id="<?php echo 'p'.$j.'g2'; ?>" type="text" onChange="sumScoreF('m')" />
</td>
<td>
<input name="<?php echo 'p'.$j.'g3'; ?>" id="<?php echo 'p'.$j.'g3'; ?>" type="text" onChange="sumScoreF('m')" />
</td>
<td>
<input name="<?php echo 'p'.$j.'gtotal'; ?>" id="<?php echo 'p'.$j.'gtotal'; ?>" type="text" />
</td>
</tr>
<?php } ?>
Any help is greatly appreciated. Thanks.
Used PHP to echo the loop variable within the onChange javascript function call:
<table>
<tr>
<td>col1</td>
<td>col2</td>
<td>col3</td>
<td>total</td>
</tr>
<?php for($j=1;$j<=3;$j++) { ?>
<tr>
<td>
<input name="<?php echo 'p'.$j.'g1'; ?>" id="<?php echo 'p'.$j.'g1'; ?>" type="text" onChange="sumScoreF('<?php echo 'p'.$j.'g'; ?>')" />
</td>
<td>
<input name="<?php echo 'p'.$j.'g2'; ?>" id="<?php echo 'p'.$j.'g2'; ?>" type="text" onChange="sumScoreF('<?php echo 'p'.$j.'g'; ?>')" />
</td>
<td>
<input name="<?php echo 'p'.$j.'g3'; ?>" id="<?php echo 'p'.$j.'g3'; ?>" type="text" onChange="sumScoreF('<?php echo 'p'.$j.'g'; ?>')" />
</td>
<td>
<input name="<?php echo 'p'.$j.'gtotal'; ?>" id="<?php echo 'p'.$j.'gtotal'; ?>" type="text" />
</td>
</tr>
<?php } ?>