My table is here http://jsfiddle.net/wqk7Lauz/
All I need is to have all values from td:nth-of-type(3) multiplied with 3.8 on page load.
HTML
$('td:nth-of-type(3)').text(parseFloat($('td:nth-of-type(3)').text()) * 3.8)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-bordered mydatatable table-hover display responsive nowrap">
<tbody>
<tr>
</tr>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Preço por 1000</th>
<th>Quantidade mínima</th>
<th>Quantidade máxima</th>
<th>Descrição</th>
</tr>
<tr>
<td>9</td>
<td>Product </td>
<td>
R$1.86
</td>
<td>100</td>
<td>10000</td>
<td>S1 </td>
</tr>
<tr>
<td>10</td>
<td>Product </td>
<td>
R$3.98
</td>
<td>1000</td>
<td>100000</td>
<td>S1</td>
</tr>
</tbody>
</table>
You're very close, you just need to use the callback version of text so you're only handling the text of each individual element, and remove the R$ at the beginning:
$('td:nth-of-type(3)').text(function() {
return parseFloat($(this).text().replace("R$", "")) * 3.8;
});
Example:
$('td:nth-of-type(3)').text(function() {
return parseFloat($(this).text().replace("R$", "")) * 3.8;
});
<table class="table table-bordered mydatatable table-hover display responsive nowrap">
<tbody>
<tr>
</tr>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Preço por 1000</th>
<th>Quantidade mínima</th>
<th>Quantidade máxima</th>
<th>Descrição</th>
</tr>
<tr>
<td>9</td>
<td>Product </td>
<td>
R$1.86
</td>
<td>100</td>
<td>10000</td>
<td>S1 </td>
</tr>
<tr>
<td>10</td>
<td>Product </td>
<td>
R$3.98
</td>
<td>1000</td>
<td>100000</td>
<td>S1</td>
</tr>
</tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
If you want to add back the R$, and perhaps limit the result to a specific number of places, you can do that with string concatenation and toFixed:
$('td:nth-of-type(3)').text(function() {
return "R$" + (parseFloat($(this).text().replace("R$", "")) * 3.8).toFixed(2);
});
Example:
$('td:nth-of-type(3)').text(function() {
return "R$" + (parseFloat($(this).text().replace("R$", "")) * 3.8).toFixed(2);
});
<table class="table table-bordered mydatatable table-hover display responsive nowrap">
<tbody>
<tr>
</tr>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Preço por 1000</th>
<th>Quantidade mínima</th>
<th>Quantidade máxima</th>
<th>Descrição</th>
</tr>
<tr>
<td>9</td>
<td>Product </td>
<td>
R$1.86
</td>
<td>100</td>
<td>10000</td>
<td>S1 </td>
</tr>
<tr>
<td>10</td>
<td>Product </td>
<td>
R$3.98
</td>
<td>1000</td>
<td>100000</td>
<td>S1</td>
</tr>
</tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Hi change your script function with this
$(".table tr td:nth-child(3)").each(function(){ //Loop
//alert($(this).text())
var thrdtd = $(this).text(); //Getting value of td
onlyno = thrdtd.replace('R$', ''); // Removing R$
//akert(thrdtd);
$(this).text('R$ '+ parseFloat(onlyno * 3.8).toFixed(2)) // Adding R$ and also multiplying at the same time and update that result to td again
});
In this first i am getting value of every third td value through loop
$(".table tr td:nth-child(3)").each(function(){
and then removing character from it before multiplying and then after multiplying i am updating the same td value
You can use the below code:
$('td:nth-of-type(3)').text("R$" + (parseFloat($(this).text().replace("R$", "")) * 3.8).toFixed(2));
Related
I want only td (Test 1) change class(yellow).
How to make only td (Test 1) change color (not entire row).
$('input[name="cek"]').on('change', function() {
$(this).closest('tr').toggleClass('yellow', $(this).is(':checked'));
});
$(document).ready(function() {
$('input:checked[name="cek"]').closest('tr').addClass('yellow');
});
.yellow {
background-color: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table">
<thead>
<tr>
<th nowrap>TGL</th>
<th>CEK</th>
<th>TITEL</th>
<th>VAL</th>
</tr>
</thead>
<tbody>
<tr>
<td>01.12.19</td>
<td nowrap><input type='checkbox' name='cek' /></td>
<td nowrap>Test 1</td>
<td nowrap>1</td>
</tr>
</tbody>
</table>
You can try the below code.
$('input[name="cek"]').on('change', function() {$(this).closest('td').next('td').toggleClass('yellow',$(this).is(':checked'));});
.yellow{
background-color:yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table" >
<thead>
<tr>
<th nowrap>TGL</th>
<th>CEK</th>
<th>TITEL</th>
<th>VAL</th>
</tr>
</thead>
<tbody>
<tr>
<td>01.12.19</td>
<td nowrap><input type='checkbox' name='cek' /></td>
<td nowrap>Test 1</td>
<td nowrap>1</td>
</tr>
</tbody>
</table>
You can do something below
$('input[name="cek"]').on('change', function() {
// $(this).closest('td').toggleClass('yellow',$(this).is(':checked'));
$(this).parents('td').siblings("td:eq(1)").toggleClass('yellow',$(this).is(':checked'));
});
$(document).ready(function() {
$('input:checked[name="cek"]').closest('tr').addClass('yellow');
});
.yellow{
background-color:yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table" >
<thead>
<tr>
<th nowrap>TGL</th>
<th>CEK</th>
<th>TITEL</th>
<th>VAL</th>
</tr>
</thead>
<tbody>
<tr>
<td>01.12.19</td>
<td nowrap><input type='checkbox' name='cek' /></td>
<td nowrap>Test 1</td>
<td nowrap>1</td>
</tr>
</tbody>
</table>
Use parent() with next().
$('input[name="cek"]').on('change', function() {
$(this).parent().next('td').toggleClass('yellow', $(this).is(':checked'));
});
.yellow {
background-color: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table">
<thead>
<tr>
<th nowrap>TGL</th>
<th>CEK</th>
<th>TITEL</th>
<th>VAL</th>
</tr>
</thead>
<tbody>
<tr>
<td>01.12.19</td>
<td nowrap><input type='checkbox' name='cek' /></td>
<td nowrap>Test 1</td>
<td nowrap>1</td>
</tr>
</tbody>
</table>
first question here, my problem is this.
I've got 22 more or less values to print stored inside a table in a DB. Problem is some of this values gotta have HTML tags in it, mainly tags <s> .
<?php
$query = "SELECT * FROM teams";
$result = mysqli_query($db, $query);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "
<div class='col-md-12'>
<div class='card strpied-tabled-with-hover'>
<div class='card-header '>
<h4 class='card-title'>".$row['name']." ".$row['surname']."</h4>
<p class='card-category'>".$row['team']."</p>
</div>
<div class='card-body table-full-width table-responsive'>
<table class='table table-hover table-striped'>
<thead>
<th>#</th>
<th>Player</th>
<th>Player Bonus</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td style='color:red; font-weight:bold'>".$row['c1']."</td>
<td>".$row['c1b']."</td>
</tr>
<tr>
<td>2</td>
<td>".$row['c2']."</td>
<td>".$row['c2b']."</td>
</tr>
<tr>
<td>3</td>
<td>".$row['c3']."</td>
<td>".$row['c3b']."</td>
</tr>
<tr>
<td>4</td>
<td>".$row['c4']."</td>
<td>".$row['c4b']."</td>
</tr>
<tr>
<td>5</td>
<td>".$row['c5']."</td>
<td>".$row['c5b']."</td>
</tr>
<tr>
<td>6</td>
<td>".$row['c6']."</td>
<td>".$row['c6b']."</td>
</tr>
<tr>
<td>7</td>
<td>".$row['c7']."</td>
<td>".$row['c7b']."</td>
</tr>
<tr>
<td>8</td>
<td>".$row['c8']."</td>
<td>".$row['c8b']."</td>
</tr>
<tr>
<td>9</td>
<td>".$row['c9']."</td>
<td>".$row['c9b']."</td>
</tr>
<tr>
<td>10</td>
<td>".$row['c10']."</td>
<td>".$row['c10b']."</td>
</tr>
<tr>
<td>11</td>
<td>".$row['c11']."</td>
<td>".$row['c11b']."</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>";} ?>
TL;DR: $row[c1] and so on have HTML tags stored as values like <s>stringvalue</s> but it just echo stringvalue. What can I do? Thanks and sorry for my inexperience.
EDIT: I mispelled the title in an horrific way.
My html table goes like below, In the footer of my table i want to add the "Total" and sum of all the values at the footer of particular "quarters"
"quarter1" "quarter2" "quarter3" "quarter4"
250000 115000
175000 600000 275000
300000 150000 750000
650000 450000
850000 290000 145000
my view code is like below
How to overcome my issue, please suggest me. Any helps are appreciated,
Thank you
<table class="table table-bordered" id="referListTable">
<thead>
<tr>
<th>SI.No</th>
<th>Jan-Mar</th>
<th>Apr-Jun</th>
<th>Jul-Sep</th>
<th>Oct-Dec</th>
</tr>
</thead>
<tbody>
<?php
$i=1;
foreach($dashboardDetails as $llist){
?>
<tr>
<td><?php echo $i; ?></td>
<td align="right">$llist['previous']; ?></td>
<td align="right">$llist['quarter1'];?></td>
<td align="right">$llist['quarter2'];?></td>
<td align="right">$llist['quarter3'];?></td>
<td align="right">$llist['quarter4'];?></td>
</tr>
<?php
$i++;
}
?>
</tbody>
<tfoot>
<tr>
<th>Total</th>
<td id="total" ></td>
<td id="total" ></td>
<td id="total" ></td>
<td id="total" ></td>
<td id="total" ></td>
</tr>
</tfoot>
</table>
Try something like this
var sums = []
$("table tbody tr").each(function() {
$(this).find("td").each(function(i, x) {
if ($(x).html().length) {
var tdValue = parseInt($(x).html());
tdValue = (tdValue + parseInt(sums[i] == undefined ? 0 : sums[i]))
sums[i] = tdValue
}
})
})
$.each(sums,function(i,x) {
$("tfoot tr").append("<td>"+x+"</td>")
})
Note this is created before we had your html code in the question, but it should be able to give you an idea how to make it work with your own html code
var sums = []
$("table tbody tr").each(function() {
$(this).find("td").each(function(i, x) {
if ($(x).html().length) {
var tdValue = parseInt($(x).html());
tdValue = (tdValue + parseInt(sums[i] == undefined ? 0 : sums[i]))
sums[i] = tdValue
}
})
})
$.each(sums,function(i,x) {
$("tfoot tr").append("<td>"+x+"</td>")
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<thead>
<tr>
<td>quarter1</td>
<td>quarter2</td>
<td>quarter3</td>
<td>quarter4</td>
</tr>
</thead>
<tbody>
<tr>
<td>250000</td>
<td>115000</td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td>175000</td>
<td>600000</td>
<td>275000</td>
</tr>
<tr>
<td>300000</td>
<td>150000</td>
<td>750000</td>
<td></td>
</tr>
<tr>
<td>650000</td>
<td></td>
<td></td>
<td>450000</td>
</tr>
<tr>
<td>850000</td>
<td>290000</td>
<td>145000</td>
<td></td>
</tr>
</tbody>
<tfoot>
<tr>
</tr>
</tfoot>
</table>
How can I make each row clickable without repeating
This one is an example that shows the problem, parameter could be the code:
<table>
<thead>
<tr>
<th>Code</th>
<th>User</th>
...
</tr>
</thead>
<tbody>
<tr>
<td> 123 </td>
<td> User A </td>
...
</tr>
<tr>
<td> 456 </td>
<td> User B </td>
...
</tr>
</tbody>
Thanks
Excuse me for my English, I hope that you understand the problem.
There are a few different ways to achieve this. Here are a couple using plain javascript and one using jQuery.
Plain JS
With plain javascript with just use the onclick parameter. Pretty straight forward.
<table>
<thead>
<tr>
<th>Code</th>
<th>User</th>
...
</tr>
</thead>
<tbody>
<tr onclick="window.location='page/parameter1';">
<td> 123 </td>
<td> User A </td>
...
</tr>
<tr onclick="window.location='page/parameter2';">
<td> 456 </td>
<td> User B </td>
...
</tr>
</tbody>
</table>
jQuery
With jQuery you add a class so you can use that as the selector. There is also a data-href parameter that will hold the URL you want the user to go to when they click the row.
<table>
<thead>
<tr>
<th>Code</th>
<th>User</th>
...
</tr>
</thead>
<tbody>
<tr class="clickable" data-href="page/parameter1">
<td> 123 </td>
<td> User A </td>
...
</tr>
<tr class="clickable" data-href="page/parameter2">
<td> 456 </td>
<td> User B </td>
...
</tr>
</tbody>
</table>
<script>
jQuery(document).ready(function($) {
$("tr.clickable").click(function() {
window.location = $(this).data("href");
});
});
</script>
Your code should look like :
<table>
<thead>
<tr>
<th>Code</th>
<th>User</th>
...
</tr>
</thead>
<tbody>
<tr>
<td> 123 </td>
<td> User A </td>
...
</tr>
<tr>
<td> 456 </td>
<td> User B </td>
...
</tr>
</tbody>
Added end tag </a>
Because of all products have different price of its different package, I have separated its table for each products.
<table width="70%" border="0" cellspacing="0" cellpadding="0">
<tr c>
<td colspan="4"><div align="center">Product One</div></td>
</tr>
<tr>
<td>Id</td>
<td>package</td>
<td>price</td>
<td>image</td>
</tr>
<tr>
<td>1</td>
<td>2kg</td>
<td>$10</td>
<td>p2.jpg</td>
</tr>
<tr>
<td>2</td>
<td>4kg</td>
<td>$20</td>
<td>p4.jpg</td>
</tr>
<tr>
<td>3</td>
<td>6kg</td>
<td>$30</td>
<td>p6.jpg</td>
</tr>
<tr>
<td>4</td>
<td>8kg</td>
<td>$40</td>
<td>p8.jpg</td>
</tr>
</table></br></br>
<table width="70%" border="0" cellspacing="0" cellpadding="0">
<tr c>
<td colspan="4"><div align="center">Product Two</div></td>
</tr>
<tr>
<td>Id</td>
<td>package</td>
<td>price</td>
<td>image</td>
</tr>
<tr>
<td>1</td>
<td>2kg</td>
<td>$12</td>
<td>p2.jpg</td>
</tr>
<tr>
<td>2</td>
<td>4kg</td>
<td>$14</td>
<td>p4.jpg</td>
</tr>
<tr>
<td>3</td>
<td>6kg</td>
<td>$16</td>
<td>p6.jpg</td>
</tr>
<tr>
<td>4</td>
<td>8kg</td>
<td>$18</td>
<td>p8.jpg</td>
</tr>
</table>
Now what I want is to create a new table to collect the all products name: product one, product two, product three and product four.
<table width="70%" border="0" cellspacing="0" cellpadding="0">
<tr c>
<td colspan="4"><div align="center">Product One</div></td>
</tr>
<tr>
<td>Id</td>
<td>Name</td>
</tr>
<tr>
<td>1</td>
<td>Product One</td>
</tr>
<tr>
<td>2</td>
<td>Product Two</td>
</tr>
<tr>
<td>3</td>
<td>Product 3</td>
</tr>
<tr>
<td>4</td>
<td>Product Four</td>
</tr>
</table></br></br>
My question now is how to get all information of all product by connecting the name of products.
I am going to create a drop-down selection for its package. when they select the package, the price will be changed.
i know this is in coldfusion but i am sure there must be something similar in php
<cfquery name="gtmyinfo" datasource="mydb">
SELECT *
FROM mytable
</cfquery>
<table>
<tr>
<td colspan="4"><div align="center">Product ID</div></td>
<td colspan="4"><div align="center">Product Name</div></td>
<td colspan="4"><div align="center">Product Image</div></td>
</tr>
<tr>
<cfoutput query="gtmyinfo">
<td colspan="4"><div align="center">#ProductID#</div></td>
<td colspan="4"><div align="center">#ProductName#</div></td>
<td colspan="4"><div align="center">#ProductImage#</div></td>
</tr>
</cfoutput>
the table will keep looping as long with as many rows of data their are in the query.
you can limit which data is called with the "where" tag in the query or with an if/else tag in the table.