The code I have posted below shows a child row when you click on the value inside of a row, in this example with colindex of 2.
I was wondering what way I could make the same thing happen but when you click anywhere on the row. I am not sure on how to do this. The way I am creating my table is by using tablesorter and a php query. The display of the table works fine, I am just having issues finding documentation on displaying the child row when a row is clicked not the value.
My PHP:
<?php
foreach($report_tabs as $report) {
$tag=$report["tag"];
$title=$report["title"];
$hcols=$report["hdrcols"];
$cols=$report["datacols"];
$db_query=$report["dbquery"];
echo "<div id='my_test_table_tab' class='tab-pane active'>";
echo "<table id='test_table' class='table table-hover tablesorter'>";
echo "<thead>";
echo "<tr>";
// removed the hard coded column headers and took the ones from our query
global $hcols;
foreach($hcols as $column_header) {
echo "<th>$column_header</th>";
}
echo "</tr>";
echo "</thead>";
echo "<tbody>";
//Use queried data to create each row of the table
$rowcount=0;
//Creating checker health table & filling with data
if ( isset($db_query)) {
while($row = mysqli_fetch_array($db_query)) {
$rowcount++;
// removed the hard coded column set and made it driven off of the array below
echo "<tr>";
$colindex = 0;
foreach( $cols as $column_name ) {
$val = $row[$column_name];
if ($colindex == 2) {
echo "<td style='text-align: left; width: 1pt;'><a href='#' class='toggle' onClick='drawChart(\"$val\");'>$val</a></td>";
$tempval = $val;
} else {
echo "<td style='width:100pt; text-align='right'>$val</td>";
}
$colindex++;
}
echo "</tr>";
echo "<tr class='tablesorter-childRow'>";
echo "<td colspan='3'>";
echo "<div id='$tempval' style='height: 400px;'></div>";
echo "<div id='fail$tempval' style='height: 400px;'></div>";
echo "</td>";
echo "</tr>";
}
}
echo "</tbody>";
echo "</table>";
echo "<h4>$rowcount rows retrieved</h4>";
echo "</div>";
}
?>
My TableSorter function to open based on value click:
<script>
// Table Sorter Options
$(document).ready(function(){
// Turns all tables with the 'tablesorter' class into tablesorter tables with the given widgets
$(".tablesorter").tablesorter({
// stickyHeaders - Keeps headers always visible when scrolling down
// filter - Adds filter boxes to each column
cssChildRow : "tablesorter-childRow",
widgets: ['stickyHeaders','filter'],
widgetOptions: {
stickyHeaders_offset : 50,
filter_placeholder : {search : ''},
filter_saveFilters: true,
pager_output: '{startRow} - {endRow} / {filteredRows} ({totalRows})',
pager_removeRows: false,
filter_childRows : true,
filter_cssFilter : 'tablesorter-filter',
filter_startsWith : false,
filter_ignoreCase : true
},
});
// Clear buttons
add_clear_buttons();
var table = $( '#my_test_table_tab' );
// hide child rows
table.find( '.tablesorter-childRow td' ).addClass( 'hidden' );
// Toggle child row content (td), not hiding the row since we are using rowspan
table.delegate( '.toggle', 'click' ,function() {
// use "nextUntil" to toggle multiple child rows
// toggle table cells instead of the row
$( this )
.closest( 'tr' )
.nextUntil( 'tr.tablesorter-hasChildRow' )
.find( 'td' )
.toggleClass( 'hidden' );
return false;
});
// Toggle filter_childRows option
$( 'button.toggle-combined' ).click( function() {
var widget_options = table[0].config.widgetOptions,
options = !widget_options.filter_childRows;
widget_options.filter_childRows = options;
$( '.state1' ).html( options.toString() );
// update filter; include false parameter to force a new search
table.trigger( 'search', false );
return false;
});
});
</script>
All you'd need to do is change the delegate function (why are you using such an old version of jQuery?), to point to the row instead of the link.
table.delegate( '.tablesorter-hasChildRow', 'click' ,function() {
I set up this demo, which is using a newer version of jQuery - the delegate function has been deprecated, so use on instead:
$table.on('click', '.tablesorter-hasChildRow', function() {
Related
I have a new question about a project I had been working on. I was designing a grid with different colored cells. it has a hidden div which shows when a cell is clicked, however I realized that only one cell(the last one of it's type) will show. i.e. if I have 2 objects with the column "objaffinity" as 0 ("enemy") it will show both red cells on the grid, however only the last one will actually work.
how can I make it so that it will show the proper information for each cell?
here's my code:
mapgen.php:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="cellinfo.js"></script>
<script src="cmenu.js"></script>
<?php
require("sql.php");
$sql = <<<SQL
SELECT *
FROM `maps`
WHERE `objpresent` = 1
SQL;
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
} // ran the query
//$xobj = array();
//$yobj = array();
$otype = array();
$oname = array();
$xyobj = array();
while($row = $result->fetch_assoc()){
$xyobj[$row['x']][$row['y']] = true;
$otype[$row['id']]=$row['objaffinity'];
$oname[$row['id']]=$row['object'];
}
// get the rows
$cellid=1;
//find whether the row is obstructed
for ($y = 0; $y < 20; $y++) {
echo '<tr>';
for ($x = 0; $x < 25; $x++) {
echo "<td>";
//Detect what type of object it is
if (isset($xyobj[$x][$y])) {
if($otype[$cellid] == 2)
{
echo "<a href='#'> <div class='foe'> </div><div class='foepopup'>";
echo $oname[$cellid];
echo "</div></a>";
}
elseif($otype[$cellid] == 1)
{
echo "<a href='#'><div class='friend'></div><div class='friendpopup'>";
echo $oname[$cellid];
echo "</div></a>";
}
else
{
echo "<a href='#'> <div class='neutral'></div><div class='neutralpopup'>";
echo $oname[$cellid];
echo "</div></a>";
}
$cellid++;
}
echo '</td>';
}
echo '</tr>';
}
?>
Cellinfo.js:
$(document).ready(function(){
//initially hide all popups
$(".foepopup").hide();
$(".neutralpopup").hide();
$(".friendpopup").hide();
//foebutton selected
$(".foe").on("click",function(e){
$(".friendpopup").hide();
$(".neutralpopup").hide();
$(".foepopup").show();
});
//close foe when selected
$(".foepopup").on("click",function(e){
$(".foepopup").hide();
});
//neutral button pressed
$(".neutral").on("click",function(e){
$(".foepopup").hide();
$(".friendpopup").hide();
$(".neutralpopup").show();
});
//close neutral
$(".neutralpopup").on("click",function(e){
$(".neutralpopup").hide();
});
//friend button pressed
$(".friend").on("click",function(e){
$(".foepopup").hide();
$(".neutralpopup").hide();
$(".friendpopup").show();
});
//close friend
$(".friendpopup").on("click",function(e){
$(".friendpopup").hide();
});
});
In your functions you use selectors, so for the script it does not matter which div was clicked.
Let me show you some examples:
$(".foepopup").on("click",function(e){
$(".foepopup").hide();
});
It should be something like this rather:
$(".foepopup").on("click",function(e){
$(this).hide();
});
And another example:
$(".neutral").on("click",function(e){
$(".foepopup").hide();
$(".friendpopup").hide();
$(".neutralpopup").show();
});
Rewrite it like this:
$(".neutral").on("click",function(e){
var td_tag = $(this).parent().parent();
td_tag.children(".foepopup").hide();
td_tag.children(".friendpopup").hide();
td_tag.children(".neutralpopup").show();
});
Rewrite other code on your own. this is the element on which click was triggered. td_tag will contain parent cell of a div clicked. After that, children method will allow you to find needed elements already inside specific cell.
Good luck!
I'm making a mail icon which could send emails of different contents stored in arrays.
the problem is, the contents are isolated using a loop:
foreach ($table as $data) {
echo '<a id="email-content" href="mailto:?body='.$data->content.'"><i class="icon-envelope" ></i></a>';
echo "<div class='search-tmp-div' style='display:none;'><pre class='search'>$data->content</pre></div>";
}
I was wondering if there is anyone I can use the function without the envelope icon printing out a bunch of times. I want it to print only once?
$count = 0;
foreach ($table as $data) {
echo "<a id="email-content" href="mailto:?body='.$data->content.'">";
if($count == 0){ echo '<i class="icon-envelope"></i>';}
echo "</a><div class='search-tmp-div' style='display:none;'><pre class='search'>$data->content</pre></div>";
$count++;
}
This just adds a counter initially has a value of 0, thus printing <i class...></i> on the first loop (where $count == 0).
Yes there are many, an example of an alternative way would be:
create the icon button
create an html table
in each row, you put a column with a checkbox or radio box (for selection)
upon clicking on the icon button, you get the selected row(s) and send the requested email(s)
Edit:
An example of what you need would be:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#btn").click(function () {
selector = "content-"+$("input[type=radio]:checked").val();
location.href = "mailto:?body="+$("#"+selector).text();
});
});
</script>
<?php
$table = array( array( "id" => 1, "content"=>"content 1") , array( "id" => 2, "content"=>"content 2"), array( "id" => 3, "content"=>"content 3"), array( "id" => 4, "content"=>"content 4") );
echo '<img id="btn" src="http://cdn1.iconfinder.com/data/icons/Primo_Icons/PNG/128x128/email_send.png" width="40px" style="cursor:pointer;" />';
foreach ($table as $data) {
echo "<div class='search-tmp-div' style='display:block;'>";
echo "<input type=\"radio\" name=\"content\" value=\"{$data['id']}\">";
echo "<pre class='search' id=\"content-{$data['id']}\">{$data['content']}</pre></div>";
}
hi guys I'm trying go create tool tip for every HTML table cell do you guys have any idea how can i get td attribute id here
thanks a lot
jquery code sample
$(function () {
$(".test").hover(
function () {
var toolTipHtml = $("#ToolTipDiv").clone();
$(this).append(toolTipHtml);
toolTipHtml.show("slow");
},
function () {
var toolTipHtml = $(this).find(".tooltip");
toolTipHtml.hide();
toolTipHtml.remove();
}
);
});
echo "<table>";
while($row = mysql_fetch_array($result))
{
$id = $row['id_out_org'];
echo "<tr>";
echo "<td>" .$row['KG']."</td>";
echo "<td class='test'>" .$row['B']."</td>";
echo "<td >" .$row['B']."</td>";
echo "<td class='test'>";
echo "<div id = 'ToolTipDiv' class='tooltip' style='background-color: White; display: none; width: 20%;'>";
echo "Total: $ "; echo $totalp = $total + $fuel;
echo "</div>";
"</td>";
echo "</tr>";
}
echo "</table>";
Use a catch-all selector and then iterate over them:
$("td[id$=ToolTipDiv_]").each(function() {
$(this).attr('title', 'some tool tip');
});
Better you use this open source plug in
Tooltip
It might fulfill your need.
See examples here
$('#table td.test').tooltip();
To get the HTML and ID from every table cell:
$('td').each(function() {
var toolTipHtml = $(this).html(); // now you have the contents of the cell
id = $(this).attr('id'); // now you have the id
});
since you are using a Hoover you may use something like this
$('.tooltip').hover(function() {
alert(this.id);
// or to get the id of the row
var toolTipHtml = $("#"+this.id).clone();
});
Basically by clicking the "comment" link the last result of the query should show and by clicking again it should be hidden. I have tried Rocket's code as well but I get an error message in the bottom of the browser and when I click "comments" it just takes me to the top of the page. I would apprieciate some advice on this
$i = 1; // ID Counter
while($row = mysql_fetch_array($result))
{
echo "<h1>$row[title]</h1>";
echo "<p class ='second'>$row[blog_content]</p> ";
echo "<p class='meta'>Posted by .... • $row[date] • Comments<div id='something$i' style='display: none;'>$row[comment]</div>";
$i++; // Increment counter
}
This is a loop, echoing the same thing over and over, thus making all the divs have the same ID, something2.
IDs need to be unique, you gonna have to make unique IDs for each div.
Something like: <div id='something$i' style='display: none;'> (remembering to increment $i).
Also, you're gonna to escape the quotes in your onclick attribute.
<a href='#' onclick=\"toggle_visibility('something$i');\">
The code should look something like this:
$i = 1; // ID Counter
while($row = mysql_fetch_array($result))
{
echo "<h1>$row[title]</h1>";
echo "<p class ='second'>$row[blog_content]</p> ";
echo "<p class='meta'>Posted by .... • $row[date] • Comments<div id='something$i' style='display: none;'>$row[comment]</div>";
$i++; // Increment counter
}
Escape the quotes :
$blah = "onclick='toggle_visibility(\"something2\");'>Comments</a>"
There is an easier way to hiding / showing the next sibling ....
try this
<div style="display:none">some hidden content</div>
function toggle(el,ev) {
ev.preventDefault(); // prevent the link from being followed
el = next(el); // get the next element
if (el.style.display == "none") { // toggle the display
el.style.display = "block";
} else {
el.style.display = "none";
}
}
/*
Credit to John Resig for this function
taken from Pro JavaScript techniques
*/
function next(elem) {
do {
elem = elem.nextSibling;
} while (elem && elem.nodeType != 1);
return elem;
}
Working example
You can throw in a counter into your code as the while loop is executing to dynamically generate unique id's for each comment div. Or, you can pull a unique field out of the query result for the id's, as long as you hook up to it appropriately later if it ends up being used and remain consistent in the rest of the code.
either
$count = count($result);
...
while (...){
$count--;
echo '... id="something'. $count .'" ...'
}
or...
while (...){
echo '... id="something'. $row['ID'] .'" ...'
}
a PHP table populates its data from mysql, When a user clicks .button a drop down table row menu appears where user has ability to add item to cart, the name of the item in cart should be the mysql entry $sound['downloadlink'] located in the table row above .mp3buy, It is adding the actual string "$sound['downloadlink']" instead of what the mysql entry for download link should be.
PHP Table
<?php
while($sound=mysql_fetch_assoc($records)){
echo "<tr class='adder'>";
echo "<td width='40' class='player'> <a href='beats/".$sound['downloadlink']."' class='sm2_button'>Play/</a></td>";
echo '<td width="250" class="name">'.$sound['name'].' <span class="red date">'.$sound['date'].'</span></td>';
echo "<td width='88' class='bpm'>".$sound['bpm']." B.P.M.</td>";
echo "<td width='72' class='length'>".$sound['length']."</td>";
echo "<td width='275' class='keywords'>".$sound['keywords']."</td>";
echo "<td width='96' class='buy'><img class='button' src='99cents.png'/></td>";
echo "</tr>";
}
?>
Jquery:
$('#mytable').on('click', ".button", function () {
var thisRow = $(this).parents('tr.adder');
var hasNextRow = thisRow.next('tr.added').length;
if (hasNextRow) {
thisRow.next('tr.added').remove();
} else {
$(this).parents('tr.adder').after('<tr class="added"><td height="100" colspan="6" ><img class="mp3buy" data-product-id=$sound["downloadlink"] src="mp31.png"/></td></tr>');
}
});
$('#mytable').on('click', ".mp3buy", function () {
var flag = $(this).data('flag');
simpleCart.add({
name : $(this).attr("data-product-id"),
price : .99,
quantity : (flag ? -1 : 1)
});
$(this).attr("src", flag ? "mp31.png" : "mp32.png");
$(this).data('flag', !flag);
});
Firstly, your PHP in your jQuery block is not getting parsed as you need to write <?= $sound["downloadlink"] ?> (PHP 5.4 syntax).
However, that's not your solution as you want the correct link for the product, not one fixed value. You could change your button so that it knows the link, as follows:
<img class='button' src='99cents.png' data-link='".$sound["downloadlink"]."'/>
Then you need to make quite a few jQuery changes:
$('.button').on('click', function () {
var thisRow = $(this).parents('tr.adder');
var hasNextRow = thisRow.next('tr.added').length;
if (hasNextRow) {
thisRow.next('tr.added').remove();
} else {
$(this).parents('tr.adder').after(
'<tr class="added"><td height="100" colspan="6" ><img class="mp3buy" data-product-id="'
+ $(this).data('link')
+ '" src="mp31.png"/></td></tr>'
);
}
});
$('#mytable').on('click', ".mp3buy", function () {
var flag = $(this).data('flag');
simpleCart.add({
name : $(this).data("product-id"),
price : .99,
quantity : (flag ? -1 : 1)
});
$(this).attr("src", flag ? "mp31.png" : "mp32.png");
$(this).data('flag', !flag);
});
That's roughly right but I might have missed something. The idea is that you keep your data in your HTML then use your script to manipulate the data. Don't put your data in your script. Also, it doesn't really make sense to transfer the value of the data-downloadlink attribute over to a data-product-id attribute. It would be better to refer to the original value.