Example #1
0
 /**
  * Render datatable js
  *
  * @param \FrenchFrogs\Table\Table\Table $table
  * @return string
  */
 public function datatable(Table\Table $table)
 {
     $options = [];
     // Is remote loading
     if ($table->isRemote()) {
         $table->addClass('datatable-remote');
         $table->save();
         $options += ['ajax' => ['url' => route('datatable', ['token' => $table->getToken()])], 'processing' => true, 'serverSide' => true, 'pageLength' => $table->getItemsPerPage(), 'deferLoading' => $table->getItemsTotal()];
     }
     //param par default
     $options += ['searching' => true];
     //Param dom datatable
     $dom = '';
     //header dom
     $dom .= '<"dataTables_wrapper no-footer"<"row"<"col-md-6 col-sm-12"lB>';
     // is a search is set
     if ($table->hasSearch()) {
         $dom .= '<"col-md-6 col-sm-12"f>';
     }
     $dom .= '>';
     //dom table
     $dom .= '<"table-scrollable"t>';
     //footer dom
     if ($table->hasFooter()) {
         $dom .= '<"row"<"col-md-5 col-sm-12"i><"col-md-7 col-sm-12"p>>';
     }
     //Set dom
     $options += ['dom' => $dom];
     // columns mamangement
     $columns = [];
     $order = [];
     $index = 0;
     $search = '';
     foreach ($table->getColumns() as $c) {
         $data = [];
         /**@var Column\Column $c */
         $class = $c->getClasses();
         if (!empty($class)) {
             $data['className'] = trim($class);
         }
         // width
         if ($c->hasWidth()) {
             $data['width'] = $c->getWidth();
         }
         // other column attribute
         $data['orderable'] = $c->hasOrder();
         $data['searchable'] = $c->hasStrainer();
         $data['name'] = $c->getName();
         if ($data['searchable'] && !is_null($value = $c->getStrainer()->getValue())) {
             $value = is_bool($value) ? intval($value) : $value;
             $search[] = $value;
         } else {
             $search[] = null;
         }
         // set order for main table
         if ($c->hasOrderDirection()) {
             $order[] = [$index, $c->getOrderDirection()];
         }
         // set columns to null if column parameters is empty
         $columns[] = empty($data) ? null : $data;
         $index++;
     }
     if (!empty($columns)) {
         $options += ['columns' => $columns];
     }
     // buttons
     if ($table->hasDatatableButtons()) {
         $options += ['buttons' => $table->getDatatableButtons()];
     }
     // main order foir the table
     $options['order'] = $order;
     js('onload', '#' . $table->getAttribute('id'), 'dtt', $options);
     // init search configuration - seartchcols doesn't work
     foreach ($search as $i => $s) {
         if (is_null($s)) {
             continue;
         }
         js('onload', '#' . $table->getAttribute('id'), 'DataTable().columns(' . $i . ').search', $s);
     }
     return '';
 }