Example #1
0
    <div class="table-responsive">
        <table id="grid-data-api" class="table table-striped table-condensed" >
            <thead>
                <tr>
                    <?php 
foreach ($fields as $field) {
    ?>
                        <th data-column-id="<?php 
    echo $field;
    ?>
" <?php 
    echo $elements->primary_key() == $field ? 'data-identifier="true"' : '';
    ?>
 >
                            <?php 
    if (isset($captions[$field]) and !exec::is_callable($captions[$field])) {
        ?>
                                <?php 
        echo Text::ucfirst($captions[$field]['model'] . ' ' . $captions[$field]['caption']);
        ?>
                            <?php 
    } else {
        ?>
                                <?php 
        echo Text::ucfirst($field);
        ?>
                            <?php 
    }
    ?>
                        </th>
                    <?php 
 /**
  * gets the info used from index
  * @return string json
  */
 public function action_ajax()
 {
     $elements = ORM::Factory($this->_orm_model);
     //search searchPhrase: from an array specified in the controller. If none search does not appear. do in bootdrig action
     if (Core::post('searchPhrase') !== NULL and count($this->_search_fields) > 0) {
         foreach ($this->_search_fields as $field) {
             $elements->or_where($field, 'LIKE', '%' . Core::post('searchPhrase') . '%');
         }
     }
     //extra filters
     foreach ($this->_filter_post as $field => $value) {
         //forced null value
         if ($value === NULL) {
             $elements->where($field, 'IS', NULL);
         } elseif ($value === 'NOT NULL') {
             $elements->where($field, 'IS NOT', NULL);
         } elseif (is_array($value)) {
             $elements->where($field, 'BETWEEN', $value);
         } elseif (isset($this->_fields_caption[$field])) {
             if (is_numeric($value)) {
                 $elements->where($field, '=', $value);
             } else {
                 $data = $this->_fields_caption[$field];
                 $search = ORM::Factory($data['model']);
                 $search = $search->where($data['caption'], 'LIKE', '%' . $value . '%')->find_all();
                 if (count($search) > 0) {
                     $result = array();
                     foreach ($search as $res) {
                         $result[] = $res->{$field};
                     }
                     $elements->where($field, 'IN', $result);
                 } else {
                     //not found normal search then ;)
                     $elements->where($field, 'LIKE', '%' . $value . '%');
                 }
             }
         } elseif (is_numeric($value)) {
             $elements->where($field, '=', $value);
         } else {
             $elements->where($field, 'LIKE', '%' . $value . '%');
         }
     }
     //dr($elements);
     //sort by sort[group_name]:asc
     if (Core::post('sort') !== NULL) {
         $sort = Core::post('sort');
         $field = key($sort);
         $dir = current($sort);
         if (in_array($field, array_keys($elements->table_columns()))) {
             $elements->order_by($field, $dir);
         }
     } else {
         $elements->order_by($elements->primary_key(), 'DESC');
     }
     $pagination = Pagination::factory(array('view' => 'oc-panel/crud/pagination', 'current_page' => array('page' => Core::post('current', 1)), 'total_items' => $elements->count_all(), 'items_per_page' => Core::post('rowCount', 10)))->route_params(array('controller' => $this->request->controller(), 'action' => $this->request->action()));
     $elements = $elements->limit($pagination->items_per_page)->offset($pagination->offset)->find_all();
     $rows = array();
     foreach ($elements as $element) {
         foreach ($this->_index_fields as $field) {
             //captions for fields....
             if (isset($this->_fields_caption[$field])) {
                 //callable function
                 if (exec::is_callable($function = $this->_fields_caption[$field])) {
                     $result[$field] = call_user_func($function, $element->{$field});
                 } else {
                     $model = $this->_fields_caption[$field]['model'];
                     $caption = $this->_fields_caption[$field]['caption'];
                     $result[$field] = $element->{$model}->{$caption};
                 }
             } else {
                 $result[$field] = Text::limit_chars(strip_tags($element->{$field}));
             }
         }
         $rows[] = $result;
         $result = array();
     }
     //the json returned
     $result = array('current' => $pagination->current_page, 'rowCount' => $pagination->items_per_page, 'rows' => $rows, 'total' => $pagination->total_items);
     $this->auto_render = FALSE;
     //$this->response->headers('Content-type','application/javascript');//why the heck doesnt work with this???
     $this->template = View::factory('js');
     $this->template->content = json_encode($result);
 }