Guest User

Untitled

a guest
Jan 18th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.27 KB | None | 0 0
  1. <?php
  2.  
  3. class DatatablesHelper extends AppHelper {
  4.  
  5.     public $out = null;
  6.     public $helpers = array('Html');
  7.  
  8.     public function __construct(View $View, $settings = array()) {
  9.         parent::__construct($View, $settings);
  10.         if (!empty($settings['configFile'])) {
  11.             $this->loadConfig($settings['configFile']);
  12.         }
  13.     }
  14.     public function display ($data){
  15.  
  16.         $this->out = null;
  17.         debug($this->request);
  18.         $this->out .= '<table id="datatable" cellpadding="0" cellspacing="0">';
  19.            $this->out .= ' <thead>';
  20.            $this->out .= '     <tr>';
  21.         foreach ($data['aColumns'] as $key => $value){
  22.             if (is_numeric($key)){
  23.                 $this->out .= '          <th>'.$value.'</th>';
  24.             }else{
  25.                 $this->out .= '          <th>'.$key.'</th>';
  26.             }
  27.  
  28.         }
  29.            $this->out .= '    </tr>';
  30.            $this->out .= '</thead>';
  31.            $this->out .= '<tbody>';
  32.            $this->out .= '</tbody>';
  33.         $this->out .= '</table>';
  34.  
  35.         return ($this->out);
  36.  
  37.     }
  38.  
  39.     // The default settings enable infinate scroll (lazy loading - good for large tables) and server-side sorting/searching
  40.     // You will probably only need to edit sScroll and possibly iDisplayLength and bJqueryUI which you should do from the view
  41.     // NOT here.
  42.  
  43.     public function javaScript ($data, $options=array()){
  44.  
  45.         $this->out = null;
  46.  
  47.         if (!isset ($options['sScrollY']) ){ $options['sScrollY'] = '300px'; }
  48.         if (!isset ($options['bScrollInfinite']) ){ $options['bScrollInfinite'] = true; }
  49.         if (!isset ($options['bProcessing']) ){ $options['bProcessing'] = false; }
  50.         if (!isset ($options['bLengthChange']) ){ $options['bLengthChange'] = false; }
  51.         if (!isset ($options['bScrollCollapse']) ){ $options['bScrollCollapse'] = false; }
  52.         if (!isset ($options['iDisplayLength']) ){ $options['iDisplayLength'] = 50; }
  53.         if (!isset ($options['bServerSide']) ){ $options['bServerSide'] = true; }
  54.         if (!isset ($options['bJQueryUI']) ){ $options['bJQueryUI'] = true; }
  55.  
  56.         if ($options['bScrollInfinite']){$options['bScrollInfinite'] = 'true';}else{$options['bScrollInfinite'] = 'false';}
  57.         if ($options['bProcessing']){$options['bProcessing'] = 'true';}else{$options['bProcessing'] = 'false';}
  58.         if ($options['bLengthChange']){$options['bLengthChange'] = 'true';}else{$options['bLengthChange'] = 'false';}
  59.         if ($options['bScrollCollapse']){$options['bScrollCollapse'] = 'true';}else{$options['bScrollCollapse'] = 'false';}
  60.         if ($options['bServerSide']){$options['bServerSide'] = 'true';}else{$options['bServerSide'] = 'false';}
  61.         if ($options['bJQueryUI']){$options['bJQueryUI'] = 'true';}else{$options['bJQueryUI'] = 'false';}
  62.  
  63.         $this->out .= '<script type="text/javascript" charset="utf-8">';
  64.         $this->out .= ' $(document).ready(function() {';
  65.             $this->out .= '     $("#datatable").dataTable( {';
  66.         $this->out .= '         "sScrollY": "'. $options['sScrollY'] .'",';
  67.            $this->out .= '          "bScrollInfinite": '. $options['bScrollInfinite'] .',';
  68.            $this->out .= '          "bProcessing": '. $options['bProcessing'] .',';
  69.            $this->out .= '          "bLengthChange": '. $options['bLengthChange'] .',';
  70.            $this->out .= '          "bScrollCollapse": '. $options['bScrollCollapse'] .',';
  71.            $this->out .= '          "iDisplayLength": '. $options['iDisplayLength'] .',';
  72.            $this->out .= '          "bServerSide": '. $options['bServerSide'] .',';
  73.            $this->out .= '          "bJQueryUI": '. $options['bJQueryUI'] .',';
  74.            $this->out .= '          "aaSorting": [[0,"asc"]],';
  75.            $this->out .= '          "aoColumns": [';
  76.         foreach ($data['aColumns'] as $key => $value){
  77.             if (is_numeric($key)){
  78.                 $this->out .= '{ "sName": "'.$value.'" },';
  79.             }else{
  80.                 $this->out .= '{ "sName" : "' . $key . '", ';
  81.                 if (isset($value['sortable'])){
  82.                     if ($value['sortable']){
  83.                         $this->out .= '"bSortable": true, ';
  84.                     }else{
  85.                         $this->out .= '"bSortable": false, ';
  86.                     }
  87.                 }
  88.                 if (isset($value['searchable'])){
  89.                     if ($value['searchable']){
  90.                         $this->out .= '"bSearchable": true, ';
  91.                     }else{
  92.                         $this->out .= '"bSearchable": false, ';
  93.                     }
  94.                 }
  95.                 $this->out = substr($this->out,0,-2) . ' },';
  96.             }
  97.         }          
  98.         $this->out .= '         ],';
  99.         $this->out .= '         "sAjaxSource": "' . $this->Html->url($data['sAjaxSource']) . '"';
  100.         $this->out .= '     } );';
  101.             $this->out .= ' } );';
  102.         $this->out .= '</script>';
  103.  
  104.         return ($this->out);
  105.  
  106.     }
  107. }
  108.  
  109. ?>
Advertisement
Add Comment
Please, Sign In to add comment