I need a help with the following code. There are 2 styles: activatedRow and disactivatedRow. When a user clicks on a button (located inside the TD of a table), then either activatedRow or disactivatedRow is applied to a row. Selection of a style is based on current style. For instance, if a row is activated, then the button will disactivate it, and vice versa.
So, I have a problem with AJAX success. How to write IF statement to check current style of a row?
P.S. Also it would be interesting to know how to change TD IMG (button) for disactivated and activated rows.
CSS
.activatedRow td {
background-color:#FFFFFF !important;
color: #000000 !important;
}
.disactivatedRow td {
background-color:#DFE1ED !important;
color: #9896A8 !important;
}
Function "disactivateRow()"
<script type="text/javascript">
function disactivateRow(flightNum,obj){
$.ajax({
type: "POST",
url: "callpage.php?page=tables/disactivate.php",
data: "flightNum=" + flightNum,
success: function(){
if ($(obj).hasClass("activatedRow")) {
$(obj).removeClass("activatedRow").addClass("disactivatedRow");
} else
$(obj).removeClass("disactivatedRow").addClass("activatedRow");
}
});
}
</script>
Button inside the table
<table id="newspaper-b" border="0" cellspacing="2" cellpadding="2" width = "100%">
<thead>
<tr>
<th scope="col">Flt Num</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<?php foreach ($result1 as $row):
$flightNum=$row['flightNum'];
?>
<tr id="<?php echo $flightNum; ?>" class="edit_tr">
<td><?php echo $flightNum;?></td>
<td id="<?php echo $flightNum; ?>">
<div onclick='disactivateRow("<?php echo $flightNum; ?>", this)'>
<img src='images/disactivate.png' alt='Disactivate' />
</div>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
jQuery DataTable activation
$(document).ready(function(){
$('#newspaper-b').dataTable({
"sPaginationType":"full_numbers",
"bJQueryUI":true,
'fnRowCallback': function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
if (aData[0] == "0") {
nRow.className = "disactivatedRow";
} else
nRow.className = "activatedRow";
return nRow;
}
});
ANSWER:
It works now:
$(".disact_but" ).click(function() {
var ID=$(this).attr('id');
$.ajax({
type: "POST",
url: "callpage.php?page=tables/disactivate.php",
data: "flightNum=" + ID,
success: function(){
$("#"+ID).toggleClass("disactivatedRow", 100);
}
});
return false;
});
If obj is the TD to apply the style as your code indicates you could use toggleClass
Add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the switch argument.
$(obj).closest('tr').toggleClass('disactivatedRow');
$(obj).closest('tr').toggleClass('activatedRow');
EDIT
According new information you added, obj is a row's child. closest method should give you the appropiate tr and then we can apply toggleClass
Related
I have three php scripts. lectures.php , lectures-ajax.php and lectures-play.php
In lectures.php, data get retrieved from lectures-ajax.php via jquery ajax after selecting option from dropdown.
In retrieved data via ajax contains a link. I want to open that link (i.e. Play Now button link) in colorbox. But it is not working.
code in lectures.php :
head section :
<link rel="stylesheet" href="source-floatbox/colorbox.css" />
<script src="js/jquery.min.js"></script>
<script src="source-floatbox/jquery.colorbox.js"></script>
<script>
var $ar = jQuery.noConflict();
$ar(document).ready(function(){
$ar('#loader').hide();
$ar("#sthaan_table").on('change', function() {
var sthaan_table_value=$ar("#sthaan_table").val();
var dataString = 'sthaan_table_value='+ sthaan_table_value;
if ($ar('#sthaan_table').is(':empty')){
$ar("#sthaan_details").hide();
$ar('.sthaan_details_empty').show();
$ar("#sthaan_details'").empty();
}else{
$ar.ajax({
beforeSend: function() {
$ar('#loader').show();
$ar("#sthaan_details").hide();
$ar('.sthaan_details_empty').hide();
},
complete: function(){
$ar('#loader').hide();
},
type: "POST",
url: "lectures-ajax.php",
data: dataString,
cache: false,
success: function(html){
$ar("#sthaan_details").html(html);
$ar("#sthaan_details").show();
$ar('.sthaan_details_empty').hide();
}
});
}
});
});
</script>
In body section :
<select name='sthaan' id='sthaan_table' class="form-control" >
<option value="">Select Sthaan</option>
<option value="cumulative">Cumulative</option>
<option value="sutra">Sutra Sthaan</option>
<option value="shaareer">Shaareer Sthaan</option>
<option value="nidaan">Nidaan Sthaan</option>
<option value="chikitsa">Chikitsa Sthaan</option>
<option value="kalpa">Kalpa Sthaan</option>
<option value="uttar">Uttar Sthaan</option>
</select>
<div id="loader" class="loader"><img src="images/red1.gif" style="width:100px; height:100px;"></div>
<div class="sthaan_details" id="sthaan_details"></div>
<div class="status alert alert-danger sthaan_details_empty" style="display:none;">Select Sthaan First</div>
code in lectures-ajax.php :
<?php
include("db.php");
if (isset($_POST["sthaan_table_value"])){
$sthaan_selected = $database->filter($_POST["sthaan_table_value"]);
?>
<div style="background-color:#26b864; color:#FFFFFF; font-size:15px; padding:5px; font-weight:600;">Audio Lectures - <b><?php echo $sthaan_selected ;?></b></div><br>
<?php
$q1 = "SELECT * FROM $audiotable where sthaan='$sthaan_selected' and showhide='0' order by upload_date ASC";
$counts = 1;
$len = $database->num_rows($q1);
if($len !=0){
$results = $database->get_results($q1);
?>
<table class="audiorec ff">
<thead>
<tr>
<th scope="col" style="width:5%;">Sr.No.</th>
<th scope="col" style="width:45%;">Lecture Details</th>
<th scope="col" style="color:#FFFFFF; width:16%; text-align:center;">Play Now</th>
</tr>
</thead>
<tbody>
<?php
foreach ($results as $data){
?>
<tr>
<td scope="row" data-label="Sr.No."><?php echo $counts;?></td>
<td data-label="Lecture Details"><?php echo ucwords(stripslashes($data['file_subject']));?></td>
<td data-label="Play Now">
Play
<script>
$(document).on("click", ".colorbox-my", function(){
$('.colorbox-my').colorbox({
href: $(this).data('url'),
iframe: true,
innerWidth: '95%',
innerHeight:'70%',
opacity : 0,
fixed:true,
escKey: false,
overlayClose: false,
});
});
</script>
</td>
</tr>
<?php $counts++;} ?>
</tbody>
</table>
<?php
}
}
?>
If displaying all results on lectures.php by default (without using sorting / filter option with dropdown and without using lectures-ajax.php, jquery - ajax) colorbox working.
Your kind help appreciated...
cut $(document).on("click", ".colorbox-my", function(){
$('.colorbox-my').colorbox({
href: $(this).data('url'),
iframe: true,
innerWidth: '95%',
innerHeight:'70%',
opacity : 0,
fixed:true,
escKey: false,
overlayClose: false,
}); code from lectures-ajax.php and use it in lectures.php script section it will work
I need to set class on table on spesific tr on ajax proses. my html table like below
<table class="table table-striped table-borderless table-hover" id="tablePray">
<thead>
<tr>
<th style="width:20%;">Nama / Name</th>
<th style="width:45%;">Keterangan / Description</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
foreach ($prays as $row)
{
?>
<tr id="prayRow<?php echo $row->id;?> ">
<td class="user-avatar"> <img src="<?php echo base_url();?>assets/admin/img/avatar.gif" alt="Avatar"><?php echo $row->name;?></td>
<td><?php echo $row->prayNeed;?></td>
<td class="text-right"> Healed</td>
</tr>
<?php
}
?>
and my jquery like this :
$('#changeStatusFrm').submit(function(e) {
e.preventDefault();
$id=$('#idPray').val();
$token=$('#token').val();
data = new FormData();
data.append("idPray",$id);
data.append("<?php echo $this->security->get_csrf_token_name();?>", $token );
$.ajax({
data: data,
type: "POST",
url: '<?php
echo base_url('Pray/ChangeStatus');
?>'
,
cache: false,
contentType: false,
processData: false,
success: function(url) {
var result=url.split('|');
$('#token').val(result[0]);
alert('Pray status have been change');
$("#mod-danger").modal("hide");
$("#tablePray tr#prayRow"+$id).addClass('table-success');
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
});
I want to change the spesific tr if row link get click.
Can anybody help me?? thx
If you are using datatable then you can use something like this :
$('#tablePray').dataTable( {
"columnDefs": [
{ className: "my_class", "targets": [ 0 ] }
]
} );
reference link :- https://datatables.net/reference/option/columns.className
This example here demonstrates adding and removing classes on a row from a click event.
Set like this
var val = "#prayRow"+$id;
$(val).addClass('table-success');
Make sure #prayRow$id is already defined in table
FYI: move alert('Pray status have been change'); to end of the line
Here is a screen shot of the problem
The original file has some hard coded data and I am suppose to replace this data with the one I have in my JSON file, I tried that with this code, made some progress but every time I click on the other e mail header the same data gets placed in the tag of my html file (index). I need to find a way to stop data from duplicating. Can somebody help me?! I am new in AJAX/JSON
$(document).ready(function() {
$.getJSON("email_list.js",
{format: "json"}).done(function(data){
console.log(data);
for (key in data) {
emailID = "#" + data[key].id;
$(".email-header")
.eq(key).attr("id", data[key].id);
$(emailID).find('td')
.eq(0).attr("id", "sender" + data[key].id)
.html(data[key].sender);
$(emailID).find('td')
.eq(1).attr("id", "subject" + data[key].subject)
.html(data[key].subject);
$(emailID).find('td')
.eq(2).attr("id", "datetime" + data[key].datetime)
.html(data[key].datetime);
}
});
// show/hide emails when click on headers
$("tr.email-header").click(function(){
id = "#body " + $(this).attr("id");
//creates the name for each file
fileID = $(this).attr("id") + ".js";
//console.log(id);
$.ajax({
url: fileID,
dataType: "JSON",
success: function(data) {
console.log(data);
//I need to replace the contents in the HTML file with
//my JASON file contents.
fElement = $("tr.email-body").find("p");
fElement.eq(0).html(data.recipient);
fElement.eq(1).html(data.body);
fElement.eq(2).html(data.recipient);
fElement.eq(3).html(data.body);
fElement.eq(4).html(data.recipient);
fElement.eq(5).html(data.body);
}}).fail(function() {
console.log(id + " - HAS AN ERROR!");
});
$(this).next().eq(0).toggle();
});
// hide email on click
$("tr.email-body").click(function(){
$(this).hide();
});
});
Here's an updated snippet that is pulling ajax data from your github repo. Just trace through to see how we are accessing the elements in the js file.
Some additional notes:
Try to use classes for repetitive elements like sender subject and datetime rather than ids. Then it is even easier to access them. I didn't change that.
You have it set to reload the email every time it's header is clicked. Might want to ensure to only load once.
You should clear out the existing html right away so the placeholder text isn't shown for a split second before the real content gets loaded.
$(document).ready(function() {
$.getJSON("https://raw.githubusercontent.com/checonunez74/AJAX-JSONproject/master/email_list.js", {
format: "JSON"
}).done(function(data) {
for (key in data) {
emailID = "#" + data[key].id;
$(".email-header")
.eq(key).attr("id", data[key].id);
f_el = $(emailID).find('td');
f_el.eq(0).attr("id", "sender")
.html(data[key].sender);
f_el.eq(1).attr("id", "subject")
.html(data[key].subject);
f_el.eq(2).attr("id", "datetime")
.html(data[key].datetime);
}
});
// show/hide emails when click on headers
$("tr.email-header").click(function() {
let id = $(this).attr("id"),
emailBody = $(this).next(".email-body"),
emailRecipient = emailBody.find('p').first(),
emailContent = emailBody.find('p').last();
//make the AJAX call here
$.ajax({
url: 'https://raw.githubusercontent.com/checonunez74/AJAX-JSONproject/master/' + id + '.js',
dataType: "JSON",
success: function(data) {
emailRecipient.text('To: ' + data.recipient);
emailContent.text(data.body);
}
}).fail(function() {
console.log(id + " - HAS AN ERROR!");
});
emailBody.toggle();
});
// hide email on click
$("tr.email-body").click(function() {
$(this).hide();
});
});
.email-body {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="http://code.jquery.com/jquery.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="email.js"></script>
<link rel="stylesheet" href="email.css">
</head>
<body>
<h1>Email Inbox</h1>
<table class="table table-hover" id="inbox">
<thead>
<tr class="active">
<th>Sender</th>
<th>Subject</th>
<th>Received</th>
</tr>
</thead>
<tbody>
<tr class="warning email-header">
<td>Bob</td>
<td>Re: Your brains</td>
<td>01/03/2014 9:56pm</td>
</tr>
<tr class="email-body">
<td colspan="3">
<p>To: Tom#tom.tom</p>
<p>Heya Tom, it's Bob, from the office down the hall...</p>
</td>
</tr>
<tr class="warning email-header">
<td>Your only friend</td>
<td>I've been shockingly nice</td>
<td>04/07/2011 12:34pm</td>
</tr>
<tr class="email-body">
<td colspan="3">
<p>To: want#you.gone</p>
<p>That's what I'm counting on.</p>
</td>
</tr>
<tr class="warning email-header">
<td>Mr. Fancy Pants</td>
<td>Chances are...</td>
<td>10/21/2005 4:16am</td>
</tr>
<tr class="email-body">
<td colspan="3">
<p>To: dancing#the.parade</p>
<p>You thought you had some fancy pants and now you know it's true.</p>
</td>
</tr>
</tbody>
</table>
</body>
</html>
I am a beginner and I am trying to get the cost from MYSQL database to work on every row. I have created two columns of cost to show what I have attempted. So far, I can get the cost, but I can only get it to work on last row using class = "<?php echo $line_item; ?>" I also tried using "class = results" which displays the right cost but it displays on every row. I know this looks like a mess but any help is much appreciated
<?php
include '****';
$order_num = $_REQUEST['order_n'];
$result = mysql_query("SELECT * from items where order_n=$order_num"); ?>
<script src="javascripts/jquery.js" type="text/javascript"></script>
<script src="javascripts/jquery.dataTables.js" type="text/javascript"></script>
<table id='datatables' class='display' cellspacing='1' table width = '1200' border = '5'' div >
<thead>
<tr>
<th><div align='left'><span class='style7'>Line</span></div></th>
<th><div align='left'><span class='style7'>Order #</span></div></th>
<th><div align='left'><span class='style7'>Choose Product</span></div></th>
<th><div align='left'><span class='style7'>cost</span></div></th>
<th><div align='left'><span class='style7'>cost</span></div></th>
</tr>
</thead>
<?php
while($row = mysql_fetch_assoc($result)){
$line_item=$row['line_item']; ?>
<td><span class="text"><?=$row['line_item']?></td>
<td><span class="text"><?=$row['order_n']?></td>
<td>
<form action="">
<select name="Description" id="<?php echo $line_item; ?>" c
class = "descrip_1"
<option value="">Select a Item:</option>
<option value="2x8-08 SYP #2">2x8-08 SYP #2</option>
<option value="3/4 Galvanized Nut">3/4 Galvanized Nut</option>
</select>
</form>
</td>
<td width=60px><label class="results"></label></td>
<td width=60px><label class="<?php echo $line_item; ?>"></label></td>
</tr><?php
} ?>
</tbody>
</table>
</div>
</body>
</html>
<script type="text/javascript">
$(function() {
function_1();
$('.descrip_1').on('change', function() {
function_2(this);
});
});
function function_2(elem) {
var $container = $(elem).parent().parent();
var descrip_2= $container.find('.descrip_1').val();
$.ajax({
type: 'POST',
data: ({p : descrip_2}),
url: 'cost.php',
success: function(data) {
// Using "results will get cost on every row,
// but the cost is the same on each row.
// Should be different depending on line number
$('.results').html(data);
//Using php echo $line_item will work only one line.
$('.<?php echo $line_item; ?>').html(data); }
});
}
function function_1(){
}
</script>
PHP cost.php file to get cost
<?php
include("*****");
$name=$_POST['p'];
$sql = mysql_query("SELECT * FROM types WHERE Description = '".$name."'");
$row = mysql_fetch_array($sql);
echo "<table border='0' cellspacing='0'>
<th><font color='black'>".$row['Cost']."</th>
</table>"; ?>
You don't need to use a variable class name.
Just use <td width=60px><label class="line_item"></label></td> and then
$container.find(".line_item").html(data);
in your jquery success.
Let me know if this not what you want, your question is a little confusing (do you want all rows to update or just the changed row? This should only change the changed row).
Edit
Okay, use $(elem).closest("tr").find(".line_item").html(data);
This is a solution that is loosely coupled with the corresponding html. I wrongly assumed the $container variable was, well, a container.
Josh DM answered my question and it works now. Here is what I did
html
<td width=60px><label class="line_item"></label></td>
javascript
<script type="text/javascript">
$(function() {
function_1();
$('.descrip_1').on('change', function() {
function_2(this);
});
});
function function_2(elem) {
// This will give the tr of the Element Which was changed
var $container = $(elem).parent().parent();
var descrip_2= $container.find('.descrip_1').val();
$.ajax({
type: 'POST',
data: ({p : descrip_2}),
url: 'listercust.php',
success: function(data) {
$(elem).closest("tr").find(".line_item").html(data);
}
});
}
function function_1(){
}
</script>
Am just trying to show all the list in the pagination. am using datatable plugin's pagination on my file, pagination is working fine, in the list i have an image that have a function delete on click. in my pagination 1'st five records is shown . and the other five records shown on click next, delete function working properly on 1'st five record when i click on next than delete function stop it's working .
my code:-
<script>
var conf = jQuery.noConflict();
conf(document).ready(function(){
conf(".delete").on( "click", function() {
alert('adfasdfasd');
//alert(conf(this).attr("id"));
custid = conf(this).attr("id");
var r=confirm("Are you sure");
if (r==true)
{
var url = "<?php echo Mage::getBaseUrl();?>turnkey/index/deleteuser/";
conf.ajax({
type:"POST",
url:url,
data: { 'custid':custid},
success: function(msz){
alert(msz);
if(msz=="deleted") {
conf("#hidepro"+custid).hide("slow");
}
else {
//conf("#hidepro"+proid).hide();
alert("product cant be deleted");
}
//console.log("chal hun");
}
});
}
else
{
return false ;
}
});
});
</script>
and the pagination code is :-
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$('#example').dataTable();
} );
</script>
<div id="container">
<div id="demo">
<table cellpadding="0" cellspacing="0" border="0" class="display" id="example" width="100%">
<thead>
<tr>
<th>Factory</th>
<th></th>
<th>Contact</th>
<th>URL</th>
<th>Remove</th>
</tr>
</thead>
<tbody>
<?php
foreach( $custemail as $custemail1 ) {
$customer = Mage::getModel("customer/customer");
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($custemail1); //load customer by email id ?>
<tr class="odd gradeX" style=" border-bottom: 1px solid #FF0000;" id = "hidepro<?php echo $customer['entity_id'] ?>">
<td class="bodyText style4" ><a style=" color: #CC3300;float: left;margin-top: 42px !important;text-decoration: underline;" href="<?php echo Mage::getBaseUrl();?>turnkey/index/listuser?id=<?php echo $customer->getEntity_id();?>"><?php echo $customer->getFactory();?></a> </td>
<td class="bodyText style4">
<a href="<?php echo Mage::getBaseUrl();?>turnkey/index/listuser?id=<?php echo $customer->getEntity_id();?>">
<img style=" width:100px;height:100px;margin-bottom:5px ;" src = "http://lab.ghrix.com/turn-key-mart/media/<?php echo $customer->getUserImage();?>"></a>
</td>
<td class="bodyText style4" style="padding-top: 10px;">
<?php echo $customer->getFirstname();?><?php //echo $customer->getUser_address();?><br><?php echo $customer->getmobile();?><br><?php echo $customer->getEmail();?></td>
<td class="bodyText style4" style="float: left;margin-top: 42px !important;" >
<a target="_blank" style="color:#005580;" href="<?php echo $customer->getWebsite();?>"><?php echo $customer->getWebsite();?></a></td>
<td class="bodyText style4"><div style= "cursor:pointer;" class = "delete" id = "<?php echo $customer['entity_id'] ?>"><img width="60px" src="<?php echo $this->getSkinUrl('images/trash.jpg'); ?>"</div></td>
</tr>
<?php }?>
</tbody>
</table>
</div>
</div>
please suggest where mistake is happen.
Without a live example, I can't be sure, but try this:
replace
conf(".delete").on("click", function() ...
with:
conf(document).on("click", ".delete", function() ...
The reason is that conf(".delete") only attaches to elements available at the time the function is run. It might be that your dataTable plugin runs first, removes the extra elements, then the delete binder is run and only works on the first 5. The second method binds to the document, and checks each click to see if it matches the .delete selector. This used to be known as jQuery.live()