jquery datatable and cakePHP - php

i am trying to implement jquery datatable, in my cakePHP based website, but it just wont load. this website is already half developed, and from the way i see it, the js' is loaded through a file called _head.inc.ctp located inside the views/layouts folder, i have added the datatables library inside the libs folder which is webroot/js/libs and load it inside the _head.inc.ctp file.
suppose i have this:
my controller:
var $helpers = array(
'Form',
'Html',
'Javascript'
);
//my method
function dataTable_example($id=null){
$details = $this->Detail->find("all");
$this->set('details', $details );
}
my view:
<div>
<?php echo $javascript->link('libs/jquery.dataTables.js'); ?>
<script>
$(document).ready(function(){
$('#js-datatable').dataTable();
});
</script>
<h2><?php echo __l('Tickets');?></h2>
<div>
<table id="js-datatable">
<tr>
<th>some heading 1</th>
<th>some heading 1</th>
<th>some heading 1</th>
</tr>
<?php
if (!empty($details)){
foreach ($details as $detail):
?>
<tr>
<td><?php echo $detail['Detail']['id'];?></td>
<td><?php echo $detail['Detail']['created'];?></td>
<td><?php echo $detail['Detail']['ticket_detail'];?></td>
</tr>
<?php
endforeach;
}else{
?>
<tr>
<td>No Data Found</td>
</tr>
<?php }?>
</table>
</div>
</div>
i even hard coded it using the usual call, and checked it using firebug to see if the script is loaded or not, and according to firebug, it is loaded, so i cant see whats making the script fail my table.
did i missed some steps ? please help
thanks

You don't have thead and tbody elements as required by the datatables script

You should use the find function in your controller and pass the array to the view and in the view write it.. don't just leave the table empty

Related

how to add tabletools to existing datatables?

OK guys, I have a page with a dinamic table using jquery datatables
<div class="widget">
<div class="widget-head">
<h5>Usuarios</h5>
</div>
<div class="widget-body">
<div class="row">
<div class="col-md-12">
<div class="table-responsive">
<table cellpadding="0" cellspacing="0" border="0" id="data-table" width="100%">
<thead>
<tr>
<th><?php echo lang('index_fname_th');?></th>
<th><?php echo lang('index_lname_th');?></th>
<th><?php echo lang('index_email_th');?></th>
<th><?php echo lang('index_groups_th');?></th>
<th><?php echo lang('index_code_th');?></th>
<th><?php echo lang('index_status_th');?></th>
<th><?php echo lang('index_action_th');?></th>
<th><?php echo lang('index_net_th');?></th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user):?>
<tr>
<td><?php echo htmlspecialchars($user->first_name,ENT_QUOTES,'UTF-8');?></td>
<td><?php echo htmlspecialchars($user->last_name,ENT_QUOTES,'UTF-8');?></td>
<td><?php echo htmlspecialchars($user->email,ENT_QUOTES,'UTF-8');?></td>
<td>
<?php foreach ($user->groups as $group):?>
<?php echo anchor("auth/edit_group/".$group->id, htmlspecialchars($group->name,ENT_QUOTES,'UTF-8')) ;?><br />
<?php endforeach?>
</td>
<td><?php echo htmlspecialchars($user->codigo,ENT_QUOTES,'UTF-8');?></td>
<td><?php echo ($user->active) ? anchor("auth/deactivate/".$user->id, lang('index_active_link')) : anchor("auth/activate/". $user->id, lang('index_inactive_link'));?></td>
<td><?php echo anchor("auth/edit_user/".$user->id, 'Edit') ;?></td>
<td><?php echo htmlspecialchars($user->red,ENT_QUOTES,'UTF-8');?></td>
</tr>
<?php endforeach;?>
</tbody>
<tfoot>
<tr>
<th><?php echo lang('index_fname_th');?></th>
<th><?php echo lang('index_lname_th');?></th>
<th><?php echo lang('index_email_th');?></th>
<th><?php echo lang('index_groups_th');?></th>
<th><?php echo lang('index_code_th');?></th>
<th><?php echo lang('index_status_th');?></th>
<th><?php echo lang('index_action_th');?></th>
<th><?php echo lang('index_net_th');?></th>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
<div class="widget-foot">
</div>
</div>
edit:
this is my js script:
/* Data Table */
/* ********** */
$(document).ready(function() {
$('#data-table').dataTable({
"sPaginationType": "full_numbers",
"dom": 'T<"clear">lfrtip'
});
});
/* ****************************** */
and my css is the big file you download from datatables web page.
I have spent almost a week trying to add tabletools to my code i just cant understand the documentation, can anyone point me in the right direction please.
Thanks everyone!
first import the data tabletools package
after that unzipped the folder where the your project folder
after that include the source file
like this
<script src="http://localhost:88/project_name/DataTables-1.10.7/media/js/jquery.js"></script>
<script src="http://localhost:88/project_name/DataTables-1.10.7/media/js/jquery.dataTables.js"></script>
<script src="http://localhost:88/project_name/DataTables-1.10.7/extensions/TableTools/js/dataTables.tableTools.min.js"></script>
<link href="http://localhost:88/project_name/DataTables-1.10.7/media/css/jquery.dataTables.css"rel="stylesheet">
<link href="http://localhost:88/project_name/DataTables-1.10.7/extensions/TableTools/css/dataTables.tableTools.css"rel="stylesheet">
and after that
include the jquery function
<script>
$(document).ready(function() {
$('#index1').DataTable( {
dom: 'T<"clear">lfrtip',
tableTools: {
"sSwfPath": "http://localhost:88/project_name/DataTables1.10.7/extensions/TableTools/swf/copy_csv_xls.swf"
}
} );
} );
</script>
here index1 is table id
Ok.
Table tools are a PLUGIN to datatables, a number of steps are required to get this to work.
I cannot see your header, so I do not know what you have included (js,css)
First you need to have all the scripts/css for the standard data tables.
In addition to that you need to go https://www.datatables.net/extensions/tabletools/plug-ins and download/save the javascript and add that to your header to be loaded, you also need to download, save and load the table tools css sheet in your header as well - again I cannot see that you have done so.
Go to https://www.datatables.net/release-datatables/extensions/TableTools/examples/simple.html, save all the HTML/JS/CSS locally and create a file to test that locally. Once that is working with external files start including the files (js/css) on your server - if its still working you know you have all the required libraries.
Then try your code - if it does not work put a SIMPLE table in their - you could even use the one from the table tools site.
If its still not working what does the debugging console tell you (Firefox/Chrome)?
Add tabletools swf file to your datatable options, and add the datatables js and css files to your code
$('#status_table').DataTable({
"tableTools": {
"sSwfPath": "packages/datatables-tabletools/swf/copy_csv_xls_pdf.swf"
}
});

How to auto format code file *.ctp in PDT Helios?

I am using Eclipse for PHP (PDT) Helios, how to auto format CakePHP Template file? For example: when I follow this tutorial , when I edit file index.ctp like this:
<!-- File: /app/View/Posts/index.ctp -->
<h1>Blog posts</h1>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Created</th>
</tr>
<!-- Here is where we loop through our $posts array, printing out post info -->
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php echo $this->Html->link($post['Post']['title'],
array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
</td>
<td><?php echo $post['Post']['created']; ?></td>
</tr>
<?php endforeach; ?>
<?php unset($post); ?>
</table>
I don't undestand how Eclipse autoformat above snippet code. Please help me, thank you!
In Aptana Studio (is based on Eclipse) try this:
Window / Preferences / General / Editors / File Associations
If You haven't .ctp file, You must add it, after add to this extension Assiociated files to PHP
I do this like my follow screenshot:

how to post the data with out click on checkbox in cakephp?

I have form where the select boxes are there and select all option, i want to post all the data without click on check boxes, i dont want to view this page, mean if user go to signup page the data will already selected by default how can i do that wothout any view?
this is my view code
<?php v_start($this);?>
<?php
#if(element exits of controller-action)
echo $this->element('validations/users/signup');
?>
<script>
function toggleChecked(status)
{
jQuery(".new-checkbox input").each( function() {
jQuery(this).attr("checked",status);
}
)
}
</script>
<h1><span style="color:CornflowerBlue"><?php echo __l('Step1');?></span></h1>
<?php
$form = $this->FormManager;
echo $form->create('User',array_merge($form_options,array('default'=>true)));
?>
<table width = "100%">
<tbody>
<tr>
<td>
<?php echo $form->input('',array('id'=>'mark-all','onclick'=>'toggleChecked(this.checked)','type'=>'checkbox','label'=>'Select All Products' ));?>
<?php echo $form->input('product_id',
array(
'multiple'=>'checkbox',
'options'=>$products,
'div'=>false,
'class'=>'new-checkbox'
)
);
?>
</td>
</tr>
<tr>
<td>
<?php
echo $this->FormManager->end(LBL_BTN_NEXT);
?>
</td>
</tr>
</tbody>
</table>
i dont want this page, the user will select the data by default.
this is my controller code
$products = $this
->User
->ProductsUser
->Product
->find('list',array(
'fields' => 'product_id,name',
'conditions'=>array(
'Product.is_visible'=>true
)
)
);
$this->set('products',$products);
if($this->request->is('post'))
{
$this->Session->write('signUp.signUpProduct',$this->request->data['User']);
$this->redirect(array('action'=>'signup_product'));
}
}
how can i do that, Thanks in advance.
The attr() doesn't take true or false as an argument.
jQuery(this).attr("checked","checked"); // Correct usage of attr()
Although I think attr is deprecated for the favorable prop() method.
jQuery(this).prop("checked",status);
Which indeed does take a true/fales argument. :)
Hope this helps.

html link tr broken not on place

I've tried making my hole tr tag to be clickable so I made this code
<?php foreach ($data['forums'] as $forum): ?>
<?php #var_dump($forum); ?>
<tr class="fix head">
<th class="fix ltext"><strong><?php echo $forum['name'] ?></strong></th>
<th class="fix rtext"><strong>Trending</strong></th>
<th class="fix ltext"><strong>Latest Post</strong></th>
</tr>
<?php foreach ($forum['children'] as $child): ?>
<?php #var_dump($child); ?>
<tr class="fix">
<a href="#">
<td class="fix ltext cl">
<strong><?php echo $child['name']; ?></strong>
<p><?php echo $child['description_html']; ?></p>
</td>
<td class="fix rtext cr">1423</td>
</a>
<td class="fix ltext cr cl">
tanya jawab sesuatu by <a class="u" href="#">=awdwad</a>
</td>
</tr>
<?php endforeach ?>
<?php endforeach ?>
on the view
The problem is
The link should be just above the highlighted part and below it. How could it be above far away, below my body tag?
Does ayone have any experience that could possibly generate this error?
Well, you've put the a tag where it's not allowed, so any browser's answer is legitimate. You should put an a into each td. Perhaps, you may alternatively handle click event for tr element, but that would require javascript.
The problem is that that is an invalid place for an <a>. No DTD will allow what you have tried to do.
Your approach is all wrong. You have to use Javascript to make a whole <tr> clickable. AFAIK, there is no way to do this in any HTML variant alone.
Try something like this:
<table>
<tr id="my_clickable_tr">
<td>Stuff</td>
<td>Stuff</td>
<td>Stuff</td>
</tr>
<!-- More table stuff -->
</table>
<script type="text/javascript">
document.getElementById('my_clickable_tr').onclick = function () {
window.location.href = 'http://wherever.you.want/to/send/the.clicker';
};
</script>
Anchor tags (a) are not allowed as children of table row (tr) tags, see the documentation here. Only table header (th) and table data (td) tags are allowed.

How to create a custom template system in PHP

I want to use a custom template system in my php application,
What I want is I want to keep away my php codes from design, I would like to use a tpl file for designs and a php file for php codes
I dont want to use any ready maid scripts. Can any one point out some links link or useful info how to build a php templating system to achieve this
Thank you
The way I do it is to create a template file(.tpl if you wish) and insert markers which will be replaced with str_replace in PHP. The code will look something like this:
For template.tpl file
<body>
<b>Something: </b> <!-- marker -->
</body>
For the PHP
$template = file_get_contents('template.tpl');
$some_data = 'Some Text'; //could be anything as long as the data is in a variable
$template = str_replace('<!-- marker -->', $some_data, $template);
echo $template;
That's it in a nutshell but it can get a lot more complex. The marker can be anything as long as it's unique.
I want to keep away my php codes from design, I would like to use a tpl file for designs
...and mix your tpl codes with "design"!
what's the difference then? :)
PHP itself is efficient templating system.
And nowadays most developers agreed that dividing your PHP code to business logic part and display logic part is most preferable way.
It can be very limited subset of PHP of course. You will need an output operator (<?=$var?>) one, a condition <? if(): ?>...<? endif ?>, a loop <? foreach(): ?>...<? endforeach ?> and include.
An example of such a template:
<table>
<? foreach ($data as $row): ?>
<tr>
<td><b><?=$row['name'] ?></td>
<td><?=$row['date'] ?></td>
</tr>
<tr>
<td colspan=2><?=$row['body'] ?></td>
</tr>
<? if ($row['answer']): ?>
<tr>
<td colspan=2 valign="top">
<table>
<tr>
<td valign="top"><b>Answer: </b></td>
<td><?=$row['answer'] ?></td>
</tr>
</table>
</td>
</tr>
<? endif ?>
<? if($admin): ?>
<tr>
<td colspan=2>
<? if($row['del']): ?>
show
<? else: ?>
hide
<? endif ?>
edit
</td>
</tr>
<? endif ?>
<? endforeach ?>
</table>

Categories