Welcome Guest, Not a member yet? Register   Sign In
Ignited DataTables

[eluser]Unknown[/eluser]
Hi, Thanks for the library.
I just want to know, this library is only work for mysql database, am i correct?

[eluser]cryogenix[/eluser]
yes they will only work for mysql.

[eluser]Pegasus275[/eluser]
Code:
// user listener
    public function user_listener()
    {
      $table = 'users';
      $columns = array('id', 'username','email');
      $index = 'id';
      
$joins['groups']['columns'] = array('description');
      $joins['groups']['fk'] = 'users.group_id = groups.id';
      $joins['meta']['columns'] = array('first_name','last_name','id');
      $joins['meta']['fk'] = 'users.id = meta.user_id';
      $custom_columns['delete'] = array('test$1', array('users.id'));          
      $options['custom_columns'] = $custom_columns;    
      $options['joins'] = $joins;
      $this->load->library('Datatables');
      $data['result'] = $this->datatables->generate($table, $columns, $index,$options);
      echo $data['result'];
      //$this->load->view('welcome_message', $data);
    }


and view

   $('#test').dataTable
    ({
      'bServerSide'    : true,
      'bAutoWidth'     : false,
      'sPaginationType': 'full_numbers',
      'sAjaxSource'    : 'http://localhost/wepu/admin.php/user/user_listener',
      "aoColumns": [
              { "sName": "users.id"},
              { "sName": "users.username"},
              { "sName": "delete", "bSortable": false }
        ],
        'fnServerData': function(sSource, aoData, fnCallback)
      {
        $.ajax
        ({
          'dataType': 'json',
          'type'    : 'POST',
          'url'     : sSource,
          'data'    : aoData,
          'success' : fnCallback
        });
        datatables_show_search($(this).attr('id'));
      }
    });

{ "sName": "delete", "bSortable": false }


it is not working
i use jquery 1.6 and latest datatables
when i include custom column i get error: requested unknown parameter from the datasource ...

[eluser]Pegasus275[/eluser]
and my json

{"sEcho":0,"iTotalRecords":"3","iTotalDisplayRecords":"3","aaData":[["1","administrator","admin@admin.com","Administrator","Admin","istrator","jeda1"],["2","marijang","marijan","Administrator","marijan","Greguric","jeda2"],["3","lidijad","test","Administrator","lidija","dragojevi?","jeda3"]],"sColumns":"users.id,users.username,users.email,groups.description,meta.first_name,meta.last_name,meta.id,delete"}

[eluser]Pegasus275[/eluser]
{ "sName": "edit", "bSortable": false }

[eluser]ηυмвєяσηє[/eluser]
I haven't tested it with Jquery 1.6 yet. But as i can see,you have 8 columns in your defination. and i see only 3 in your JS. ;

Code:
"aoColumns": [
              { "sName": "users.id"},
              { "sName": "users.username"},
              { "sName": "delete", "bSortable": false },
              { "sName": "users.email"  },
              { "sName": "groups.description" },
              { "sName": "meta.first_name" },
              { "sName": "meta.last_name" },
              { "sName": "meta.id"}
        ],
or you can try
Code:
"aoColumns": [
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              { "bSortable": false }
        ],

Btw, i think u added custom columns later, because i can't see the custom column's value in your json.

Regards,
Yusuf

[eluser]Pegasus275[/eluser]
Ok
This is solution

the new function (old replace on line 313)
Code:
/**
    * Builds a JSON encoded string data
    *
    * @param string $columns
    * @param string $iTotal
    * @param string $iFilteredTotal
    * @param string $rResult
    * @return string
    */
    protected function produce_output($columns, $iTotal, $iFilteredTotal, $rResult, $custom_columns = '')
    {
      $aaData = array();
      $sColumnOrder = '';

      foreach($rResult->result_array() as $row_key => $row_val)
      {
        foreach($row_val as $col_key => $col_val)
        {
          if($row_val[$col_key] == 'version')
            $aaData[$row_key][$col_key] = ($aaData[$row_key][$col_key] == 0)? '-' : $col_val;
          else
            $aaData[$row_key][$col_key] = $col_val;
        }

        if(isset($custom_columns) && is_array($custom_columns))
        {
          foreach($custom_columns as $cus_col_key => $cus_col_val)
          {
            if(isset($cus_col_val[1]) && is_array($cus_col_val[1]))
            {
              foreach($cus_col_val[1] as $cus_colr_key => $cus_colr_val)
                $cus_col_val[0] = str_ireplace('$' . ($cus_colr_key + 1), $aaData[$row_key][$cus_colr_val], $cus_col_val[0]);

              $aaData[$row_key][$cus_col_key] = $cus_col_val[0];
            }
            else
              $aaData[$row_key][$cus_col_key] = $cus_col_val[0];
          }
        }
      }

      foreach($columns as $col_key => $col_val)
        $sColumnOrder .= $col_val . ',';

      if(isset($custom_columns) && is_array($custom_columns))
        foreach($custom_columns as $cus_col_key => $cus_col_val)
          $sColumnOrder .= $cus_col_key . ',';

      $sColumnOrder = substr_replace($sColumnOrder, '', -1);

      $sOutput = array
      (
        'sEcho'                => intval($this->ci->input->post('sEcho')),
        'iTotalRecords'        => $iTotal,
        'iTotalDisplayRecords' => $iFilteredTotal,
        'aaData'               => $aaData,
        'sColumns'             => $sColumnOrder
      );
     // echo "<div>".json_encode($aaData)."</div>";
      return json_encode($sOutput);
    }

controller
Code:
$table = 'users';
      $columns = array('id', 'username','email','created_on');
      $index = 'id';
      $joins['groups']['columns'] = array('description');
      $joins['groups']['fk'] = 'users.group_id = groups.id';
      $joins['meta']['columns'] = array('first_name','last_name');
      $joins['meta']['fk'] = 'users.id = meta.user_id';
      $custom_columns['delete'] = array('jeda$1', array('id')); // this is also change
      $options['custom_columns'] = $custom_columns;    
      $options['joins'] = $joins;
      $this->load->library('Datatables');
      $data['result'] = $this->datatables->generate($table, $columns, $index,$options);
      echo $data['result'];

and now the view
Code:
$('#test').dataTable
    ({
      'bServerSide'    : true,
      'bAutoWidth'     : false,
      'sPaginationType': 'full_numbers',
      'sAjaxSource'    : 'http://localhost/wepu/admin.php/user/user_listener',
      "aoColumns": [
              { "mDataProp": "id" },
            { "mDataProp": "username" },
            { "mDataProp": "username" } and so on
        ],
        'fnServerData': function(sSource, aoData, fnCallback)
      {
        $.ajax
        ({
          'dataType': 'json',
          'type'    : 'POST',
          'url'     : sSource,
          'data'    : aoData,
          'success' : fnCallback
        });
        datatables_show_search($(this).attr('id'));
      }
    });

[eluser]Pegasus275[/eluser]
With this you can select more columns and display only what you need. In custom column you can use columns that you are not want to display.

for example

$joins['meta']['columns'] = array('first_name','last_name','id as meta_id');

$custom_columns['delete'] = array('user_id :$1 group_id: $2', array('id','group_id'));

This is nice library but it be better to write something like

$this->datatable->join...
$this->datatable->custom_filed('edit','<a href="">$1</a>',array('id'));
$this->datatable->where('user.id'=1)->get('users')->json();

something like thatBig Grin

[eluser]ηυмвєяσηє[/eluser]
Still im trying to figure out what was your problem.

if you dont want to show a column that is used in custom column, just set bVisible to false. There is no need to change anything current.


Regards,
Yusuf

[eluser]Tar_Tw45[/eluser]
I'm trying to use Ignited DataTables. Here are my code files ..

Controller : post.php
Code:
class Post Extends CI_Controller{

    function test() {
        $table = 'post';
        $columns = array('title', 'category', 'post_date','add_time');
        $index = 'id';
        $this->load->library('Datatables');
        $data['result'] = $this->datatables->generate($table, $columns, $index);
        if(count($data)){
            $this->load->view('backend/page/post/test', $data);
        }
    }

}

View : test.php (located in view/backend/page/post/)
Code:
[removed]
$(document).ready(function(){
    $('#test_table').dataTable({
        'bServerSide'    : true,
        'bAutoWidth'     : false,
        'sPaginationType': 'full_numbers',
        'sAjaxSource': 'www.mysite.com/post/test',
        'aoColumns' : [
            { 'sName': 'title'},
            { 'sName': 'category'},
            { 'sName': 'post_date'},
            { 'sName': 'add_time' },
        ],
        'fnServerData': function(sSource, aoData, fnCallback){
            $.ajax({
                'dataType': 'json',
                'type': 'POST',
                'url': sSource,
                'data': aoData,
                'success': fnCallback
            });
        },
    });
});
[removed]

<fieldset>
    <legend>
        <b>Search Result</b>
    </legend>
    <table cellpadding="0" cellspacing="0" border="0" class="display"
        id='test_table'>
        <thead>
            <tr>
                <th style="width: 46%">Title</th>
                <th style="width: 18%">Category</th>
                <th style="width: 18%">Post Date</th>
                <th style="width: 18%">Added Date</th>
            </tr>
        </thead>
        <tbody>
        <tr>                                
              <td>loading..</td>  
           </tr>  
        </tbody>
        <tfoot>
            <tr>
                <th>Title</th>
                <th>Category</th>
                <th>Post Date</th>
                <th>Added Date</th>
            </tr>
        </tfoot>
    </table>
</fieldset>

Main Page : main.php
Code:
[removed]
    $('#test_button').click(function(){
        $.post('post/test', function(data){
            $('#target_area').html(data);
        });
    });
[removed]

<div class="navigation_button" id='test_button'>Test DataTable with CL Framework</div>
<div class="content_area" id="target_area"></div>

What I want them to do is
1. Main.php will contains 2 div tags. When I clicked the first one, it will call the post controller.
2. In the post controller, It should get data from post table. Each entry of data will contains 4 column. Then send that data to the view file located in "backend/page/post/"
3. test.php will recieve data from controller and generate the page content to show in the Main.php
4. Once test.php finished data generate. The page content will send back to the callback function in Main.php's JavaScript code. Then, the page content will be put into the seconde div tag of Main.php and generate to DataTable

My problem is every thing seems to work fine but none of the data entry were shown. My MySQL database contains 70xxx post entries so this wasn't because of the database was empy. I copied most of the code in controller and view from the Ignited DataTables wiki. I don't know what did I do wrong. Can you guys help me out? Very thank in advance.




Theme © iAndrew 2016 - Forum software by © MyBB