EllisLab text mark
Advanced Search
23 of 30
23
   
Ignited DataTables
Posted: 31 May 2012 07:28 AM   [ # 331 ]   [ Rating: 0 ]
Joined: 2012-05-31
8 posts

I want to add a column by using add_column() function only under certain circumstances.

For example in a table with data, only if a user is the creator of the table’s row data to be able to edit it by showing a column like “Edit”, otherwise the column will be hidden or display nothing.


For now i am using this

$this->datatables->select(SELECT VALUES);
            
$this->datatables->from(TABLE);
            
$this->datatables->where(CONDITION);
            
$this->datatables->add_column('edit''<a class="edit" href="$1">Edit</a>''id');
            return 
$this->datatables->generate(); 

And i would like the add_column to be added and shown only on certain rows based on a check that i will be doing if the user is the creator.

Is it possible somehow or do i have to edit the core of the datatables library?

 
Posted: 31 May 2012 08:49 AM   [ # 332 ]   [ Rating: 0 ]
Joined: 2012-05-31
8 posts
Sport_Billy - 31 May 2012 07:28 AM

I want to add a column by using add_column() function only under certain circumstances.

For example in a table with data, only if a user is the creator of the table’s row data to be able to edit it by showing a column like “Edit”, otherwise the column will be hidden or display nothing.


For now i am using this

$this->datatables->select(SELECT VALUES);
            
$this->datatables->from(TABLE);
            
$this->datatables->where(CONDITION);
            
$this->datatables->add_column('edit''<a class="edit" href="$1">Edit</a>''id');
            return 
$this->datatables->generate(); 

And i would like the add_column to be added and shown only on certain rows based on a check that i will be doing if the user is the creator.

Is it possible somehow or do i have to edit the core of the datatables library?

OK i found a way, even if in my opinion is not the best one.

I am taking the output of the above function,
decoding using json_decode,
checking through other function for each desired json object ‘id’ if current user is its creator and if true then changing its ‘edit’ column value to the desired output,
then re-json_encode() and display the output.

There should be an easier way though.

 
Posted: 31 May 2012 02:39 PM   [ # 333 ]   [ Rating: 0 ]
Joined: 2011-05-03
11 posts
laxdoctor - 30 May 2012 04:25 PM

DiLer - I’m having the same exact problem. When I directly access my controller that produces the json I see the correct data output. I was also able to get it to work when I set bServerSide to false.

EDIT 2: I made two changes to get this to work.

First, set add “sServerMethod”: “POST” so your script looks something like:

$(document).ready(function() {
 
$('#test_table').dataTable{
  
"bProcessing"true,
  
"bServerSide"true,
  
"sServerMethod""POST",
  
"sAjaxSource""/index.php/tables/listener"
 
);
); 

Second, change Line 295 of the datatables library

From:

if($sSearch != ''

To:

if($sSearch != && $sSearch != ''

Oh man, thanks a lot!
At first it didn’t work, but I could figure out why. I enabled csrf protection in the config file. There is an issue with ajax and this option when set to TRUE.

$config['csrf_protection'TRUE

The moment I disabled it, it worked. BUT I don’t want to disable it. So I googled for an hour or so and here is what I found:

http://unruhdesigns.com/blog/2012/03/a-simple-solution-to-codeigniter-csrf-protection-and-ajax
This pretty much explains the problem. I did not test the solution posted there, but it should work, too.

My solution was something I found here:
http://ellislab.com/forums/viewthread/214772/

data<?php echo $this->security->get_csrf_token_name(); ?>=<?php echo $this->security->get_csrf_hash(); ?> 

This combined with some other stuff I found gave me this: (and it works!)

$(document).ready(function() {
    
$('#example').dataTable{
      
"bProcessing"true,
      
"bServerSide"true,
      
"sAjaxSource""<?php echo base_url(); ?>home/getdatabyajax",
      
"fnServerData": function(sSourceaoDatafnCallback){
      aoData
.push"name""<?php echo $this->security->get_csrf_token_name(); ?>""value""<?php echo $this->security->get_csrf_hash() ?>" );
      $.
ajax({
 
'dataType''json',
 
'type''POST',
 
'url'sSource,
 
'data'aoData,
 
'success'fnCallback
      }
);
    
}  
  } 
);
); 

This works even with crsf_protection enabled.

So in the end I just changed the code as showed above and modified the datatables library as you mentionend.
I guess “sServerMethod”: “POST” and ajax(‘type’: ‘POST’) is pretty much the same?! That’s why I didn’t need it?!

Thanks again laxdoctor, you made my day (or year)

/edit
Ok the search does not work :/
Probably because of the modification of the datatables library…does anyone have a solution?

/edit2
Oh man I hope this is the right solution…
I found out that the sql queries are escaped wrong. Take a look at this:

SELECT `id`, `username`, `create_dateFROM (`accounts`) WHERE (id LIKE '%'somestring'%' OR username LIKE '%'somestring'%' OR create_date LIKE '%'somestring'%' ORDER BY `idasc LIMIT 10 

This is what datatables does. But as you can see ‘somestring’ (the word I searched for) shouldn’t have the ‘’ around it. The query should be ...LIKE ‘%somestring%’ .... and not ...LIKE ‘%‘somestring’%’...

With that in mind I changed the datatables library back to normal. So line 295 is

if($sSearch != ''

again.
But I changed line 290 (I tested a lot of stuff, so maybe it’s a line above or under that, I’m not 100% sure).

The line is:

$sSearch $this->ci->db->escape($this->ci->input->post('sSearch')); 

I changed it too

$sSearch $this->ci->input->post('sSearch'); 

Now the string isn’t in quotes anymore and everything works fine. I hope this helps other people out there!

 
Posted: 31 May 2012 03:59 PM   [ # 334 ]   [ Rating: 0 ]
Avatar
Joined: 2011-03-26
111 posts
Sport_Billy - 31 May 2012 08:49 AM
Sport_Billy - 31 May 2012 07:28 AM

I want to add a column by using add_column() function only under certain circumstances.

For example in a table with data, only if a user is the creator of the table’s row data to be able to edit it by showing a column like “Edit”, otherwise the column will be hidden or display nothing.


For now i am using this

$this->datatables->select(SELECT VALUES);
            
$this->datatables->from(TABLE);
            
$this->datatables->where(CONDITION);
            
$this->datatables->add_column('edit''<a class="edit" href="$1">Edit</a>''id');
            return 
$this->datatables->generate(); 

And i would like the add_column to be added and shown only on certain rows based on a check that i will be doing if the user is the creator.

Is it possible somehow or do i have to edit the core of the datatables library?

OK i found a way, even if in my opinion is not the best one.

I am taking the output of the above function,
decoding using json_decode,
checking through other function for each desired json object ‘id’ if current user is its creator and if true then changing its ‘edit’ column value to the desired output,
then re-json_encode() and display the output.

There should be an easier way though.

 

/// load helper here ..
            
$this->datatables->select(SELECT VALUES);
            
$this->datatables->from(TABLE);
            
$this->datatables->where(CONDITION);
            
$this->datatables->add_column('edit''$1''check_this(id)');
            return 
$this->datatables->generate(); 

make a helper function like this ..

function check_this($id)
{
  
if $id bla bla..
  return  
'<a class="edit" href="$id">Edit</a>';
  else
  return 
'';
 Signature 

Ignited Datatables
Nested Sets

 
Posted: 01 June 2012 03:21 AM   [ # 335 ]   [ Rating: 0 ]
Joined: 2012-05-31
8 posts

Thank you very much ηυмвєяσηє !!!
The helper did exactly what i wanted grin

 
Posted: 13 June 2012 04:36 AM   [ # 336 ]   [ Rating: 0 ]
Joined: 2012-05-31
8 posts

I am using an ajax source to load my data by setting “sAjaxSource” parameter and “bServerSide”: FALSE.
Data loads fine.

Now i am trying to set the parameter “bServerSide”: TRUE but it does not load and just shows “Loading data from server”.

What is the problem?
Should i set any other parameters?

In my controller i just ECHO the result of my Model:
Model:

return $this->datatables->generate(); 

Controller:

echo $this->MODEL_NAME->FUNCTION(); 

And here is my view’s js code:

$('#TABLE').dataTable(
{
 
"bProcessing"false,
 
"bDestroy"true,
 
"bServerSide"true,
 
"bStateSave"true,
 
"sAjaxSource"CI.base_url "index.php/CONTROLLER/FUNCTION",
 
"bDeferRender"true,
 
"sDom"'<"H"Rlfr>t<"F"ip>',
 
"aaSorting"[[ 8"asc" ]],
 
"aoColumnDefs"
  { 
"sClass""defaultCountdown""aTargets"[ 11 ] },
  
"sClass""price""aTargets"[ 8 ] },
  
"bSortable"false"aTargets"[ 0 ] },
  
"bVisible"false"aTargets"[ 12 ] },
  
"mDataProp""0""aTargets"[ 1 ] },
  
"mDataProp""1""aTargets"[ 2 ] },
  
"mDataProp""2""aTargets"[ 3 ] },
  
"mDataProp""3""aTargets"[ 4 ] },
  
"mDataProp""4""aTargets"[ 5 ] },
  
"mDataProp""5""aTargets"[ 6 ] },
  
"mDataProp""6""aTargets"[ 7 ] },
  
"mDataProp""7""aTargets"[ 8 ] },
  
"mDataProp""8""aTargets"[ 9 ] },
  
"mDataProp""9""aTargets"[ 10 ] },
  
"mDataProp""10""aTargets"[ 11 ] },
  
"mDataProp""11""aTargets"[ 0 ] }
 ]
}
); 
 
Posted: 13 June 2012 10:51 AM   [ # 337 ]   [ Rating: 0 ]
Joined: 2011-03-24
22 posts

hi,

actually you should not echo anything in the controller but instead do something like this

$data['result'$this->your_model->your_function();
$this->load->view('raw_ajax'$data); 

and then creat a view (raw_ajax.php in my example) with just

<?php echo $result?> 

it should work a little better smile

have swing

 
Posted: 14 June 2012 07:10 AM   [ # 338 ]   [ Rating: 0 ]
Joined: 2011-04-19
7 posts

I did rewrite one of my datatables view with mDataProp function for almost all columns using “aoColumns”. At the end I discovered that sorting a columns doesn’t work because Ignited Code is receiving ‘function’ for mDataProp. Is is possible to redefine that ‘function’ type by returning something from one of a few type of calls to mDataProp function?

I don’t like a solution with “aoColumnDefs” because I had to use “aTargets” with column numbers. mDataProp object approach is much better for my complicated and composed columns.

 
Posted: 14 June 2012 11:36 AM   [ # 339 ]   [ Rating: 0 ]
Joined: 2012-05-31
8 posts
virtualgadjo - 13 June 2012 10:51 AM

hi,

actually you should not echo anything in the controller but instead do something like this

$data['result'$this->your_model->your_function();
$this->load->view('raw_ajax'$data); 

and then creat a view (raw_ajax.php in my example) with just

<?php echo $result?> 

it should work a little better smile

have swing

Hey thanks a lot for your reply.
I tryied what you suggested but still nothing displays if i set bServerSide: TRUE
If i set it to FALSE , everything works ok.

 
Posted: 15 June 2012 09:33 AM   [ # 340 ]   [ Rating: 0 ]
Joined: 2011-04-19
7 posts

I converted two “mDataProp” columns from functions to ones as field names. Now Ignited Code makes a query with their names (field1 & field2) as filters:
WHERE (field1 LIKE ‘%’‘%’ OR field2 LIKE ‘%’‘%’ )

As there is no filter values the query returns no records instead of returning all records!?

 
Posted: 18 June 2012 12:15 PM   [ # 341 ]   [ Rating: 0 ]
Joined: 2012-06-17
3 posts

echo prints result on the page as follows:
{"sEcho":0,"iTotalRecords":2,"iTotalDisplayRecords":2,"aaData":[["1","23096720","MIHIR","mihir.parekh@abc.com","1"],["2","12345","Mihir","abc@gmail.com","1"]],"sColumns":"id,username,name,email,login_count"}

The table just displays processing…

My controller

function user_management_edit(){
   
  $this
->load->library('Datatables');
  
$this->datatables->select('id , username , name , email , login_count');
  
$this->datatables->from('user');
  echo 
$this->datatables->generate();
  
//$mytable = $this->datatables->generate();
  //echo ($mytable);
  //$this->load->view('user_management_edit');
  //$data['result'] = $this->datatables->generate();
   
$data['main_content''user_management_edit';
   
$this->load->view('Template'$data);
  
  
  

My View

<section class="grid_12">
    <
div class="box">
      <
div class="title"><span class="icon16_sprite i_spreadsheet"></span>Data table</div>
      <
p><a href="[removed]void(0);">Click to add a new row</a></p>
      <
div class="inside">
<
table cellpadding="0" cellspacing="0" border="0" class="display" id="test">
 <
thead>
  <
tr>
   <
th width="10%">Id</th>
   <
th width="20%">Username</th>
   <
th width="25%">Name</th>
   <
th width="25%">Email</th>
   <
th width="20%">Login Count</th>
  </
tr>
 </
thead>
 <
tbody>
  <
tr>        
   <
td colspan="5" class="dataTables_empty">Loading data from server</td>
   </
tr>
 </
tbody>
</
table>
   </
div>
   
  </
div>
 </
section

My Template initializing script

[removed]
  
$(document).ready(function() {
  
$('#test').dataTable{
   
   
"bProcessing"true,
     
"bServerSide"true,
     
"sServerMethod""POST",
     
"sPaginationType""full_numbers",
     
"bDeferRender"true,
     
"sAjaxSource""<?php echo base_url(); ?>index.php/home/user_management_edit",
     
"aoColumns"
           
[
      { 
'sName' 'id' },
      
'sName' 'username' },
      
'sName' 'name' },
      
'sName' 'email' },
      
'sName' 'login_count' }
           ]
,
     
"fnServerData": function ( sSourceaoDatafnCallback 
    
{
     
$.ajax{
      
"dataType""json",
      
"type""POST",
      
"url"sSource,
      
"data"aoData,
      
"success"fnCallback,
      
);
    

   } 
);
  
);
  
[removed] 

Please help me solve this issue

 

 
Posted: 19 June 2012 05:43 AM   [ # 342 ]   [ Rating: 0 ]
Joined: 2010-09-25
7 posts

concat is not working on select

select("id, CONCAT('first_name', ' ', 'last_name') as name"); 
 
Posted: 19 June 2012 07:42 AM   [ # 343 ]   [ Rating: 0 ]
Joined: 2012-02-15
4 posts

Hi!!
I have a problem with this useful library.
My situation as follows.
Datatables library in application/libraries

the view with the datatable table:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<
html>
<
head>
<
meta http-equiv="content-type" content="text/html; charset=utf-8" />
<
link rel="shortcut icon" type="image/ico" href="http://www.datatables.net/media/images/favicon.ico" />
<
title>DataTables example</title>
<
style type="text/css" title="currentStyle">
@
import "http://datatables.net/release-datatables/media/css/demo_page.css";
@
import "http://datatables.net/release-datatables/media/css/demo_table.css";
</
style>
[removed][removed]
[removed][removed]
[removed]
$(document).ready(function()
  
{
    
$('#example').dataTable
    
({
      
'bServerSide'    true,
      
'sAjaxSource'    '<?=site_url("agents/ajax")?>',
    
});
  
});
[removed]
</head>
<
body id="dt_example">
<
div id="container">
<
h1>Ignited Datatables</h1>
  <
table border="0" cellpadding="4" cellspacing="0" class="display" id="example">
    <
thead>
      <
tr>
        <
th width="10%">Name</th>
        <
th width="10%">Surname</th>
      </
tr>
    </
thead>
    <
tbody>
      <
tr>
        <
td>loading...</td>
      </
tr>
    </
tbody>
  </
table>
</
div>
</
body>
</
html

the function ajax() in controller “agents” that is called in datatable init

function ajax(){
  $this
->datatables
  
->select('name, surname'
    ->
from('agents');
  echo  
$this->datatables->generate() ;
 

The problem is that table rows does not show up.
if I access directly to the function ajax(), the output is:

{"sEcho":0,"iTotalRecords":5,"iTotalDisplayRecords":5,"aaData":[["Piero","Maltesino"],["Piero","Maltese"],["piero","Maltese"],["prova","prova"],["Piero","Maltese"]],"sColumns":"name,surname"

that seems to be correct.

Firebug and chrome tools does not show any error.

If I use native php ignitedDatatable, it works correctly.
Any idea?
Thanks smile

 
Posted: 20 June 2012 04:03 AM   [ # 344 ]   [ Rating: 0 ]
Joined: 2011-03-24
22 posts

hi,

@sport_billy
sorry to be a little late to answer…
may be you could try with ‘bProcessing’ set to true + “sServerMethod”  : “POST”

then, as you’re using ajax source, you may need to add the function

'fnServerData'   : function(sSourceaoDatafnCallback){
 
$.ajax ({
  
'dataType''json',
  
'type'    'POST',
  
'url'     sSource,
  
'data'    aoData,
  
'success' fnCallback
 }
);

last thing, be sure to change this

$sSearch $this->ci->db->escape($this->ci->input->post('sSearch')); 

into this

$sSearch $this->ci->input->post('sSearch'); 

line 290 in the librairy as the former line double escapes data and generates an sql syntax error (ci active record already escapes data)

have swing

 
Posted: 27 June 2012 08:00 AM   [ # 345 ]   [ Rating: 0 ]
Joined: 2012-05-31
8 posts
virtualgadjo - 20 June 2012 04:03 AM

hi,

@sport_billy
sorry to be a little late to answer…
may be you could try with ‘bProcessing’ set to true + “sServerMethod”  : “POST”

then, as you’re using ajax source, you may need to add the function

'fnServerData'   : function(sSourceaoDatafnCallback){
 
$.ajax ({
  
'dataType''json',
  
'type'    'POST',
  
'url'     sSource,
  
'data'    aoData,
  
'success' fnCallback
 }
);

last thing, be sure to change this

$sSearch $this->ci->db->escape($this->ci->input->post('sSearch')); 

into this

$sSearch $this->ci->input->post('sSearch'); 

line 290 in the librairy as the former line double escapes data and generates an sql syntax error (ci active record already escapes data)

have swing

Hello and thanks again very much for your help..i made all of the changes you suggested but still nothing shows up if i set “bServerSide”: TRUE

Have you managed to make it work?

If so, can you please give me a sample of your code that works so i can test it?

 

 
23 of 30
23