I want to use the datatables jquery plugin for my project. I'm doing the project on cakePHP. I can't seem to get datatables running. I'm not quite sure if my code is right. Also I have added the css file and the two JS files in my directory under Webroot/css and Webroot/js.
Can someone help me please?
My code is as follows:
<html>
<head>
<!-- DataTables CSS -->
<link rel="stylesheet" type="text/css" href="/DataTables-1.10.0/css/jquery.dataTables.css">
<!-- jQuery -->
<script type="text/javascript" charset="utf8" src="/DataTables-1.10.0/js/jquery.js"></script>
<!-- DataTables -->
<script type="text/javascript" charset="utf8" src="/DataTables-1.10.0/js/jquery.dataTables.js"></script>
$(document).ready( function () {
$('#callTable').DataTable();
} )
</head>
<?php
$usertype=$this->SESSION->read('User.usertype');
if($usertype=="admin")
echo $this->element('setTopNavigation');
else
echo $this->element('setTopNavigationStaff');
//var_dump($calls);
?>
<div class="callsIndex">
<h2><?php echo __('Call Details'); ?> </h2>
<div class="bottomButtonnew"><?php echo $this->Html->link(__('Add Calls'), array('action' => 'add')); ?></div>
<table id="callTable" cellpadding="0" cellspacing="0">
<tr>
<th><?php echo $this->Paginator->sort('Call Date'); ?></th>
<th><?php echo $this->Paginator->sort('Call Time'); ?></th>
<th><?php echo $this->Paginator->sort('Comments'); ?></th>
<th><?php echo $this->Paginator->sort('Next Call Date'); ?></th>
<th><?php echo $this->Paginator->sort('Customer Name'); ?></th>
<th><?php echo $this->Paginator->sort('Company Name'); ?></th>
<th><?php echo $this->Paginator->sort('Employee Name'); ?></th>
<th class="actions"><?php echo __(''); ?></th>
</tr>
<?php foreach ($calls as $call): ?>
<?php
$class ='';
if($call['Call']['next_call_date']==date('Y-m-d')){
$class = ' class="call_for_today"';
}
?>
<tr<?php echo $class; ?>>
<td><?php echo date("d-m-Y", strtotime($call['Call']['call_date'])); ?> </td>
<td><?php echo h($call['Call']['call_time']); ?> </td>
<td><?php echo h($call['Call']['comments']); ?> </td>
<td><?php echo date("d-m-Y", strtotime($call['Call']['next_call_date'])); ?> </td>
<td>
<?php echo $this->Html->link($call['Customers']['customer_name'], array('controller' => 'customers', 'action' => 'view', $call['Customers']['id'])); ?>
</td>
<td>
<?php echo $this->Html->link($call['Companies']['company_name'], array('controller' => 'companies', 'action' => 'view', $call['Companies']['id'])); ?>
</td>
<td>
<?php echo $this->Html->link($call['Employees']['employee_name'], array('controller' => 'employees', 'action' => 'view', $call['Employees']['id'])); ?>
</td>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('action' => 'view', $call['Call']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<p>
<?php
echo $this->Paginator->counter(array(
'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total')
));
?> </p>
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</div>
<br>
</div>
There are two problems with your code.
1) Add document.ready inside script tag and put semicolon (;) at the end of document.ready
<script>
$(document).ready( function () {
$('#callTable').DataTable();
} );
</script>
2) And datatable work with table structure which has header enclosed in <thead></thead> tag and row enclosed inside <tbody></tbody> like below
:
<table id="callTable" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th><?php echo $this->Paginator->sort('Call Date'); ?></th>
<th><?php echo $this->Paginator->sort('Call Time'); ?></th>
<th><?php echo $this->Paginator->sort('Comments'); ?></th>
<th><?php echo $this->Paginator->sort('Next Call Date'); ?></th>
<th><?php echo $this->Paginator->sort('Customer Name'); ?></th>
<th><?php echo $this->Paginator->sort('Company Name'); ?></th>
<th><?php echo $this->Paginator->sort('Employee Name'); ?></th>
<th class="actions"><?php echo __(''); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($calls as $call): ?>
<?php
$class ='';
if($call['Call']['next_call_date']==date('Y-m-d')){
$class = ' class="call_for_today"';
}
?>
<tr<?php echo $class; ?>>
<td><?php echo date("d-m-Y", strtotime($call['Call']['call_date'])); ?> </td>
<td><?php echo h($call['Call']['call_time']); ?> </td>
<td><?php echo h($call['Call']['comments']); ?> </td>
<td><?php echo date("d-m-Y", strtotime($call['Call']['next_call_date'])); ?> </td>
<td>
<?php echo $this->Html->link($call['Customers']['customer_name'], array('controller' => 'customers', 'action' => 'view', $call['Customers']['id'])); ?>
</td>
<td>
<?php echo $this->Html->link($call['Companies']['company_name'], array('controller' => 'companies', 'action' => 'view', $call['Companies']['id'])); ?>
</td>
<td>
<?php echo $this->Html->link($call['Employees']['employee_name'], array('controller' => 'employees', 'action' => 'view', $call['Employees']['id'])); ?>
</td>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('action' => 'view', $call['Call']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Your $(document).ready handler needs to be placed inside a <script> tag
Related
I want to add value to td in jquery. I have this code but not working.
Return all values for single td. I need that input [name="text-194] contain further values not all.
<table id="mytable" style="width: 100%;" border="1">
<tbody>
<?php
$counter = 1;
foreach ($result_list as $key => $row) { ?>
<tr>
<td class="jakas"><p><?php echo $counter; ?></p></td>
<td class="numer" value="<?php echo $row->numer; ?>"><p><?php echo $row->numer; ?></p></td>
<td class="nazwa" value="<?php echo $row->nazwa; ?>"><p><?php echo $row->nazwa; ?></p></td>
<td class="input" value="<?php htmlspecialchars($row->nazwa); ?>">
<?php echo do_shortcode('[contact-form-7 id="73" title="plik"]'); ?></td>
<?php echo '<input type="text" name="idtest" value="' . htmlspecialchars($row->nazwa) . '">'; ?>
</tr>
<?php $counter++; ?>
<script>
(function ($) {
$(document).ready(function () {
var value = '<?=htmlspecialchars($row->nazwa)?>';
console.log(value);
$('input[name="text-194"]').val($('input[name="text-194"').val() + value);
});
})(jQuery);
</script>
<?php
}
?>
I am trying to have mysql data displayed on a bootstrap modal and for some reason it is only pulling the ID of the first link that is clicked and not the ID for the row specified on the link clicked to display said modal. (hope that makes sense).
The link to be clicked:
<a data-toggle="modal" data-target="#leadModal" href="?id=<?php echo $id; ?>"><?php echo $f; echo ' '; echo $l; ?></a>
Here is the code that surrounds the link:
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>Primary Phone</th>
<th>Date Created</th>
<th>Arrival Date</th>
<th>Departure Date</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>E-Mail Address</th>
<th>Primary Phone</th>
<th>Date Created</th>
<th>Arrival Date</th>
<th>Departure Date</th>
</tr>
</tfoot>
<tbody>
<?php
$agent = $userRow['userName'];
$query = "SELECT * FROM leads WHERE status='Follow-Up' && agent='$agent'";
$result = mysql_query($query) or die(mysql_error());
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
$f = $line['first'];
$l = $line['last'];
$e = $line['email'];
$p1 = $line['PrimePhone1'];
$p2 = $line['PrimePhone2'];
$p3 = $line['PrimePhone3'];
$a = $line['arrival'];
$d = $line['departure'];
$ag = $line['agent'];
$id = $line['id'];
$dc = $line['datecreated'];
$es = $line['emailsent'];
?>
<tr>
<td><a data-toggle="modal" data-target="#leadModal" href="?id=<?php echo $id; ?>"><?php echo $f; echo ' '; echo $l; ?></a></td>
<td><?php echo $e; ?></td>
<td><?php echo $p1; echo '-'; echo $p2; echo'-'; echo $p3; ?></td>
<td><?php echo $dc; ?></td>
<td><?php echo $a; ?></td>
<td><?php echo $d; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
And here is what I have for code to display the data in the modal:
<!-- Lead Modal-->
<div class="modal fade" id="leadModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header"><?php
$id = $_GET["id"];
$query = "SELECT * FROM leads WHERE id=$id";
$result = mysql_query($query);
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
<h5 class="modal-title" id="exampleModalLabel"><?php echo $line['first']; ?> <?php echo $line['last']; ?>'s Existing Lead</h5><?php } ?>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body"><?php
$id = $_GET["id"];
$query = "SELECT * FROM leads WHERE id=$id";
$result = mysql_query($query) or die(mysql_error());
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr align="center">
<td>
<table width="750" border="2" align="center" cellpadding="1" cellspacing="0" class="table">
<tr>
<td>Original Agent:</td>
<td><?php echo $line['agent']; ?></td>
</tr>
<?php if ($line['upagent'] == ''){ } else { ?> <tr>
<td>Last Updated By:</td>
<td><?php echo $line['upagent']; ?></td>
</tr><?php } ?>
<tr>
<td width="186">First Name:</td>
<td width="552">
<?php echo $line['first']; ?></td>
</tr>
<?php if ($line['last'] == ''){ } else { ?> <tr>
<td>Last Name:</td>
<td><?php echo $line['last']; ?></td>
</tr><?php } ?>
<?php if ($line['email'] == ''){ } else { ?><tr>
<td>E-Mail Address:</td>
<td><?php echo $line['email']; ?></td>
</tr><?php } ?>
<tr>
<td>Primary Phone:</td>
<td><?php echo $line['PrimePhone1']; echo '-'; echo $line['PrimePhone2']; echo '-'; echo $line['PrimePhone3']; ?></td>
</tr>
<?php if ($line['AltPhone1'] == '' || $line['AltPhone2'] == '' || $line['AltPhone3'] == '') { } else { ?>
<tr>
<td>Alternate Phone:</td>
<td><?php echo $line['AltPhone1']; echo '-'; echo $line['AltPhone2']; echo '-'; echo $line['AltPhone3']; ?></td>
</tr>
<?php } ?>
<tr>
<td>Lead Created:</td>
<td><?php echo $line['datecreated']; ?></td>
</tr>
<?php if($line['emailsent'] == 1){ ?> <tr>
<td>Follow-Up Email Sent:</td>
<td><?php echo $line['followupsent']; ?></td>
</tr>
<?php } ?>
<tr>
<td>Arrival Date:</td>
<td><?php echo $line['arrival']; ?></td>
</tr>
<tr>
<td>Departure Date:</td>
<td><?php echo $line['departure']; ?></td>
</tr>
<?php if ($line['adults'] == ''){ } else { ?>
<tr>
<td>Adults:</td>
<td><?php echo $line['adults']; ?></td>
</tr><?php } ?>
<?php if ($line['child'] == ''){ } else { ?>
<tr>
<td>Children:</td>
<td><?php echo $line['child']; ?></td>
</tr>
<?php } ?>
<?php if ($line['area'] == ''){ } else { ?>
<tr>
<td>Requested Area:</td>
<td><?php echo $line['area']; ?></td>
</tr>
<?php } ?>
<?php if ($line['budget'] == ''){ } else { ?>
<tr>
<td>Budget:</td>
<td><?php echo $line['budget']; ?></td>
</tr>
<?php } ?>
<?php if ($line['suggested'] == ''){ } else { ?>
<tr>
<td>Suggested Properties:</td>
<td><?php echo $line['suggested']; ?></td>
</tr>
<?php } ?>
<?php if ($line['discount'] == ''){ } else { ?>
<tr>
<td>Discount Offered:</td>
<td><?php echo $line['discount']; ?></td>
</tr>
<?php } ?>
<?php if ($line['promo'] == ''){ } else { ?>
<tr>
<td>Promo Code:</td>
<td><?php echo $line['promo']; ?></td>
</tr>
<?php } ?>
<tr>
<td>Lead Status:</td>
<td><?php echo $line['status']; ?></td>
</tr>
<?php if ($line['notes'] == ''){ } else { ?>
<tr>
<td height="85">Additional Notes:</td>
<td><?php echo $line['notes']; ?></td>
</tr>
<?php } ?>
<?php if ($line['resnum'] == ''){ } else { ?>
<tr>
<td>Reservation Number:</td>
<td><?php echo $line['resnum']; ?></td>
</tr><?php } } ?>
</table>
</td>
</tr>
</table>
</div>
<div class="modal-footer">
<button class="btn btn-secondary" type="button" data-dismiss="modal">Close</button>
<a class="btn btn-primary" href="#">Button</a>
</div>
</div>
</div>
</div>
Any help is greatly appreciated.
Thanks,
Scott
What you're asking cannot be done in the current design. Your answer will be in looping your db results to create several modals (messy) or using AJAX to retrieve the modal data based on the link ID. A good example is in the answer here: Using Jquery Ajax to retrieve data from Mysql
Here is my code:
<section class="panel">
<header class="panel-heading">
<a data-toggle="modal" data-target="#modal_edit" onclick="modal_edit('<?php echo base_url() ?>leave/save')" class="btn btn-default pull-right" href=""><i class="fa fa-calendar-minus-o"></i> Add Leave</a>
<!-- <a class="btn btn-default pull-right" href="<?php echo base_url() ?>leave/apply_leave"><i class="fa fa-calendar-minus-o"></i> Appsssly Leave</a> -->
<h2 class="panel-title">Employee Leave History</h2>
</header>
<div class="panel-body">
<table class="table table-bordered table-striped mb-none" id="datatable-tabletools" data-swf-path="assets/vendor/jquery-datatables/extras/TableTools/swf/copy_csv_xls_pdf.swf">
<thead>
<tr>
<th>#</th>
<th>Employee Name</th>
<th>Leave Type </th>
<th>Subject</th>
<th class="hidden-phone">From date - To date</th>
<th class="hidden-phone">Reason</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php $count = 1 ?>
<?php foreach ($leave as $leave): ?>
<tr class="gradeX">
<td><?php echo $count++ ?></td>
<td id="name"><?php echo $leave->first_name . ' ' . $leave->last_name ?>
<input type="button" id="employee_id" value="<?php echo $leave->employee_id ?>"/>
</td>
<td><?php echo $leave->name ?></td>
<td><?php echo $leave->subject ?></td>
<td><?php echo $leave->from_date . ' - ' . $leave->to_date ?></td>
<td><?php echo $leave->reason ?></td>
<td>
<select id="status" data-plugin-selectTwo class="form-control populate placeholder" >
<option value="Pending">Pending</option>
<option value="Granted">Granted</option>
<option value="Rejected">Rejected</option>
</select>
</td>
</tr>
<?php endforeach; ?>
<!-- //onchange="update_status('<?php echo base_url().'status/leave/'.$employee_id ?>')" onchange="myFunction();" -->
</tbody>
</table>
</div>
<div>
</div>
</section>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$("#status").ready(function () {
$(this).change(function () {
//var y =$('#status').val();
//alert($('#status option:selected').val() );
//alert($("#status").select());
// var x = $('#employee_id').val();
// alert(this.x)
console.log(1);
$.ajax({
url :'<?php echo base_url() ?>leave/status',
data : {
status : $(this).val(),
employee_id : $('#employee_id').val(),
// name : $('#name').val(),
},
dataType: 'json',
success : function(response) {
alert(response);
}
});
});
});
</script>
I have foreach loop and there is a drop-down list with values. If i have 4 loops then I want to update leaves for only 1-st loop, means id = 1, using ajax it works perfectly.
Now I want to update the leave for id = 2 but the problem is, jquery is not getting the id = 2 value, it selects the first id value means id =1 .
now I would like to update the id = 2 to update their leave as well.
You can't use same id multiple times.
First change in your html
<?php foreach ($leave as $leave): ?>
<tr class="gradeX">
<td><?php echo $count++ ?></td>
<td id="name"><?php echo $leave->first_name . ' ' . $leave->last_name ?>
<input type="button" class="employee_id" value="<?php echo $leave->employee_id ?>"/>
</td>
<td><?php echo $leave->name ?></td>
<td><?php echo $leave->subject ?></td>
<td><?php echo $leave->from_date . ' - ' . $leave->to_date ?></td>
<td><?php echo $leave->reason ?></td>
<td>
<select id="<?php echo $leave->employee_id ?>" class="status" data-plugin-selectTwo class="form-control populate placeholder" >
<option value="Pending">Pending</option>
<option value="Granted">Granted</option>
<option value="Rejected">Rejected</option>
</select>
</td>
</tr>
<?php endforeach; ?>
In Javascript:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$(".status").change(function () {
var employee_id = $(this).prop('id');
var status = $(this).val();
$.ajax({
url :'<?php echo base_url() ?>leave/status',
data : {'status' : status, 'employee_id' : employee_id},
dataType: 'json',
success : function(response) {
alert(response);
}
});
});
});
</script>
I hope this will solve your problem.
I have a table that shows some data from database. I want if there isn't data in some <td> , the column from next row, to be displayed in its place. This is what I mean:
If I have this kind of table:
When there is no data for example for $data['created_at'], it should be displayed $data['due_date'] in its place.
How could I do that?
<table>
<tr>
<td colspan="3">
<?php if(!empty($data['created_at'])){ ?>
<strong><?php echo "Created Date:";?>:</strong> <?php echo $data['created_at']; ?>
<?php } ?>
</td>
<td colspan="3">
<?php if(!empty($data['username'])){ ?>
<strong><?php echo "Username:";?>:</strong> <?php echo $data['username']; ?>
<?php } ?>
</td>
</tr>
<tr>
<td colspan="3">
<?php if(!empty($data['due_date'])){ ?>
<strong><?php echo "Date delivery:";?>:</strong> <?php echo $data['due_date']; ?>
<?php } ?>
</td>
<td colspan="3">
<?php if(!empty($data['copmany'])){ ?>
<strong><?php echo "Company:";?>:</strong> <?php echo $data['copmany']; ?>
<?php } ?>
</td>
</tr>
</table>
Are you looking to just omit a row if the result is empty? Simply change the place of your if statement to encompass the td
<table>
<tr>
<?php if(!empty($data['created_at'])){ ?>
<td colspan="3">
<strong><?php echo "Created Date:";?>:</strong> <?php echo $data['created_at']; ?>
</td>
<?php } ?>
<?php if(!empty($data['username'])){ ?>
<td colspan="3">
<strong><?php echo "Username:";?>:</strong> <?php echo $data['username']; ?>
</td>
<?php } ?>
</tr>
<tr>
<?php if(!empty($data['due_date'])){ ?>
<td colspan="3">
<strong><?php echo "Date delivery:";?>:</strong> <?php echo $data['due_date']; ?>
</td>
<?php } ?>
<?php if(!empty($data['copmany'])){ ?>
<td colspan="3">
<strong><?php echo "Company:";?>:</strong> <?php echo $data['copmany']; ?>
</td>
<?php } ?>
</tr>
</table>
How to uppercase each j1 "click" then the f1 text change to uppercase, j2 "click" then the f2 text change to uppercase, et seq..
<table>
<form>
<?
$i=0;
foreach ($im as $row):
$i++;
?>
<tr>
<td><? echo $i;?></td>
<td><? echo $row['Something'] ?></td>
<td><input type="checkbox" id="j<? echo $i ?>" onclick="if(this.checked){myFunction()}"></td>
<td><input type="text" id="f<? echo $i ?>" value="Test"></td>
</tr>
<script>
function myFunction() {
document.getElementById("f"+<? echo $i ?>).value = document.getElementById("f"+<? echo $i ?>).value.toUpperCase();
}
</script>
<? endforeach; ?>
</form>
</table>
Pass the value of $i to the function and use it to identify the element. Try with -
<td><input type="checkbox" id="j<? echo $i ?>" onclick="if(this.checked){myFunction('<? echo $i ?>')}"></td>
And define the function -
<script>
function myFunction(i) {
document.getElementById("f"+i).value = document.getElementById("f"+i).value.toUpperCase();
}
</script>
You dont have to put the function inside the loop.
for another option, you don't need to declare "id" attribute in input text element, you can use "name" attribute http://jsfiddle.net/j4080ytq/
<form>
<table>
<?
$i=0;
foreach ($im as $row):
$i++;
?>
<tr>
<td><? echo $i;?></td>
<td><? echo $row['Something'] ?></td>
<td><input type="checkbox" onclick="if(this.checked){f<? echo $i; ?>.value = f<? echo $i; ?>.value.toUpperCase();}"></td>
<td><input type="text" name="f<? echo $i ?>" value="Test"></td>
</tr>
<? endforeach; ?>
</table>
</form>