EllisLab text mark
Advanced Search
26 of 28
26
   
Ignited DataTables
Posted: 20 October 2012 08:24 AM   [ Ignore ]   [ # 381 ]   [ Rating: 0 ]
Avatar
Joined: 2012-10-02
71 posts

I have this current code.

$this->load->library('datatables');
  
$this->datatables
    
->select("username")
    ->
select("CONCAT(salutation, '. ', first_name, ' ', middle_initial, '. ', last_name) as full_name"FALSE)
    ->
select("office, position, email, privilege, datetime_registered")
    ->
where("username !="$this->session->userdata('username'))
    ->
from("users")
    ->
add_column('view''<a href="">AA</a>');
  echo 
$this->datatables->generate(); 

I want to add a column at the end for ‘Actions’
add_coumn is not working

 
Posted: 20 October 2012 02:42 PM   [ Ignore ]   [ # 382 ]   [ Rating: 0 ]
Avatar
Joined: 2012-07-19
32 posts
ninjayan - 20 October 2012 08:24 AM

I have this current code.

$this->load->library('datatables');
  
$this->datatables
    
->select("username")
    ->
select("CONCAT(salutation, '. ', first_name, ' ', middle_initial, '. ', last_name) as full_name"FALSE)
    ->
select("office, position, email, privilege, datetime_registered")
    ->
where("username !="$this->session->userdata('username'))
    ->
from("users")
    ->
add_column('view''<a href="">AA</a>');
  echo 
$this->datatables->generate(); 

I want to add a column at the end for ‘Actions’
add_coumn is not working

Ah, I see what you mean now. Sorry, I misunderstood your first post.. My mistake. smile

Just to confirm, is this code in your controller or your model? Is the data getting through if you try without the add_column() method in the chain?

If I remember correctly, when I tried this I only got it to work after using print() at the end of the $this->datatables() chain. Here’s the relevant code from a recent project I worked on:

// MY_Controller
function get_list() {
 $this
->load->model('my_model');
 
$this->my_model->get_list();
}

// MY_Model
public function get_list() {
 $this
->datatables->select('
  Table.Col1,
  Table.Col2,
  Table.Col3
 '
);
 
$this->datatables->from('Table');
 
 print(
$this->datatables->generate());

In this case, I set the AJAX source in my DT initialization to a url generated by:

echo(base_url('my_controller/get_list')); 

Not sure if there’s a better way to do this, but this worked for me. Just a thought, but maybe try use print() instead of echo() on your generate() line.

 
Posted: 20 October 2012 10:38 PM   [ Ignore ]   [ # 383 ]   [ Rating: 0 ]
Avatar
Joined: 2012-10-02
71 posts

Thanks for replying.
My code is only at the controller. I haven’t tried yet to put it on my model.

 
Posted: 20 October 2012 11:04 PM   [ Ignore ]   [ # 384 ]   [ Rating: 0 ]
Avatar
Joined: 2012-10-02
71 posts

Ok, I think I have to show you the codes.

controller

public function get_active_users() {
  $this
->load->library('datatables');
  
$this->datatables
    
->select("username")
    ->
select("CONCAT(salutation, '. ', first_name, ' ', middle_initial, '. ', last_name) as full_name"FALSE)
    ->
select("office, position, email, privilege, datetime_registered")
    ->
where("username !="$this->session->userdata('username'))
    ->
from("users")
    ->
add_column("view""<a>AA</a>");
  echo 
$this->datatables->generate();
 

view:

[removed]
 
$(document).ready(function() {
  
$('#users_table').dataTable({
   
//"sScrollX": "100%",
   //"sScrollXInner": "100%",
   
"sPaginationType""full_numbers",
   
"bProcessing"true,
   
"bServerSide"true,
   
"bJQueryUI"true,
   
"bDeferRender"true,
   
"sAjaxSource""<?php echo base_url(); ?>/users/get_active_users",
   
"sServerMethod""POST"
  
});
 
});
 
[removed]

<thead>
      <
tr>
       <
th class="width70 text-center">Username</th>
       <
th class="text-center">Full Name</th>
       <
th class="text-center">Office</th>
       <
th class="text-center">Position</th>
       <
th class="text-center">Email</th>
       <
th class="width60 text-center">Privilege</th>
       <
th class="width160 text-center">Date Registered</th>
      </
tr>
     </
thead>
     <
tbody>
      <
tr>
       <
td colspan="7" class="dataTables_empty">Loading data from server</td>
      </
tr>
     </
tbody

I want to add a column Action after the ‘Date Registered’. Content of it will be icon disable user, promote/remove as admin.

 
Posted: 21 October 2012 05:40 AM   [ Ignore ]   [ # 385 ]   [ Rating: 0 ]
Avatar
Joined: 2012-07-19
32 posts
ninjayan - 20 October 2012 11:04 PM

I want to add a column Action after the ‘Date Registered’. Content of it will be icon disable user, promote/remove as admin.

I suspect the biggest issue here is the fact that the number of columns between your data source (in your controller) and the number of columns in your end table (in your view), do not match. You’re trying to put data for 8 columns into a 7 column table. Out of curiosity, have you tried adding an extra column to your HTML table in your view? Sometimes the simplest solution works out best in the end.

Unfortunately though, DataTables itself doesn’t support dynamically adding columns, this question has been asked many times on the DT forum in the past. Digging through these questions again this morning, I found a link to a tutorial that looks like it does exactly what you’re asking for [link].

Otherwise, have you considered using something like the drill-down rows example, placing the ‘user specific functions’ in the row that shows up below the clicked row? This way, the data is added dynamically to the row before it’s displayed, and the row is destroyed after it’s hidden. It’s very easy to use and very efficient as well.

This is giving me ideas for another project I’m working on now.. Time to go play. (:

 
Posted: 21 October 2012 06:17 AM   [ Ignore ]   [ # 386 ]   [ Rating: 0 ]
Avatar
Joined: 2012-10-02
71 posts

Thank you. I will try it.

 
Posted: 21 October 2012 10:06 PM   [ Ignore ]   [ # 387 ]   [ Rating: 0 ]
Avatar
Joined: 2012-10-02
71 posts

Can I directly echo the username in the add_column? I want to put the username inside anchor tag

$this->load->library('datatables');
  
$this->datatables
    
->select("username")
    ->
select("CONCAT(salutation, '. ', first_name, ' ', middle_initial, '. ', last_name) as full_name"FALSE)
    ->
select("office, position, email, privilege")
    ->
select("DATE_FORMAT(datetime_registered, '%b %d, %Y - %h-%i %p') as datetime_registered"FALSE)
    ->
where("username !="$this->session->userdata('username'))
    ->
from("users")
    ->
add_column("view""<a>A</a>"); //HERE
  
echo $this->datatables->generate(); 

The first link is the same from what I’m looking for but I’m using ignited datatables.
Sorry.

 
Posted: 22 October 2012 01:10 AM   [ Ignore ]   [ # 388 ]   [ Rating: 0 ]
Avatar
Joined: 2012-10-02
71 posts
cryogenix - 08 March 2011 03:39 AM
Pegasus275 - 02 March 2011 02:00 PM

How to use it with action column for example edit/delete etc.

<tr>
<td> id from db</td>
<td> name from db </td>
<td> Edit
</tr>

you can use petroz’s client side solution to create additional columns:

petroz - 02 March 2011 09:48 PM

I use a callback on the Datatables.net configuration to generate the table on the fly. Add `fnDrawCallback` to attach any javascript function/method to the generation process.

Here is one for adding columns.

Datatables.prototype.addDataColumn = function()
{
  $(”#datatable tr:gt(0)”).append(”<td>View Edit Delete</td>”);
}

or you can also modify the library where I commented:

/*
  add additional columns here
  like adding a Delete Row control for example:

  $aaData[$row_key][] = '<a href="#">Delete Button</a>';
*/ 

Can’t find this commented lines.

 
Posted: 22 October 2012 03:18 AM   [ Ignore ]   [ # 389 ]   [ Rating: 0 ]
Avatar
Joined: 2012-10-02
71 posts

Okay. so I guess this will be my last question for this.

->add_column("Action",
        
"<form id='modify_form' action='modify_user/$1'>
        <button class='modify-btn' title='Modify'>
        <img src='../assets/images/edit.png'/>
        </button>
        </form>"
,
        
"username"); 

Form submit goes to ‘modify_user’ function. Now I want to store the username to a variable and I will do compare. How will I do that?

 
Posted: 22 October 2012 05:46 AM   [ Ignore ]   [ # 390 ]   [ Rating: 0 ]
Avatar
Joined: 2012-10-02
71 posts

Okay so I just answered my question. smile
Thanks again to those who gave inputs.

 
Posted: 23 October 2012 02:21 AM   [ Ignore ]   [ # 391 ]   [ Rating: 0 ]
Avatar
Joined: 2011-05-06
19 posts

I got this error, why is this?

A Database Error Occurred

Error Number
1096

No tables used

SELECT 
LIMIT 100

Filename
C:\server\www\bigpmc\system\database\DB_driver.php

Line Number
330 
 
Posted: 23 October 2012 03:17 AM   [ Ignore ]   [ # 392 ]   [ Rating: 0 ]
Avatar
Joined: 2012-10-02
71 posts

check your database configuration. As said on the error, ‘no tables used’

 
Posted: 23 October 2012 03:26 AM   [ Ignore ]   [ # 393 ]   [ Rating: 0 ]
Avatar
Joined: 2011-05-06
19 posts

other tables work fine, problem with this function, I am new to use Datatables library.

function getBrand(){
   
  
   $table 
"actor";
   
$columns = array("actor_id""fullname""last_update");
   
$index "actor_id";
   
$this->load->library("Datatables");
   echo 
$this->datatables->generate($table$columns$index);
   
  

   
 
Posted: 23 October 2012 03:37 AM   [ Ignore ]   [ # 394 ]   [ Rating: 0 ]
Avatar
Joined: 2012-10-02
71 posts

I will just share you my code

$this->load->library('datatables');
  
$this->datatables
->select("office, position, email, privilege")
->
from("users")
echo 
$this->datatables->generate(); 
 
Posted: 23 October 2012 03:48 AM   [ Ignore ]   [ # 395 ]   [ Rating: 0 ]
Avatar
Joined: 2011-05-06
19 posts

It’s work , thanks…..

 
26 of 28
26