Quantcast
Viewing all articles
Browse latest Browse all 82110

Datatables and Wordpress Server Side Processing Integration problem solved.

Hi,
Like other wordpress developers, I had also faced the problem of integrating datatable(server side processing) with wordpress. I tried every tips, tricks and multiple ways to solve the problem, but couldn't do it. My ajaxified functions of the wordpress simple dint work. I din't have much time and had to finish a small project on time. So, I decided to skip it and go through the simple way. The way - the plugin developer had suggested and implemented.

Read the complete story here http://nirajkvinit.blogspot.in/2013/01/datatables-and-wordpress-integration.html

Codes are:

My js code for datatable is:
oTable = jQuery('#example').dataTable({  
     "bProcessing": true,
     "bServerSide": true,
     "sAjaxSource": ajaxurl+'?action=fn_my_ajaxified_dataloader_ajax',
     "sServerMethod": "POST",
     "bDeferRender": true,
     "fnServerData": fnDataTablesPipeline,
  "bScrollInfinite": true,
        "bScrollCollapse": true,
        "sScrollY": "200px"
 });

My Wordpress PHP Code:

Define Ajax Actions

add_action('wp_ajax_fn_my_ajaxified_dataloader_ajax', 'fn_my_ajaxified_dataloader_ajax');
add_action('wp_ajax_nopriv_fn_my_ajaxified_dataloader_ajax', 'fn_my_ajaxified_dataloader_ajax');

Now the wordpress Function

function fn_my_ajaxified_dataloader_ajax()
{
 global $wpdb;
 
 $aColumns = array( 'engine', 'browser', 'platform', 'version', 'grade' ); 
 $sIndexColumn = "id"; 
 $sTable = "ajax";
 
 $sLimit = "";
 if ( isset( $_REQUEST['iDisplayStart'] ) && $_REQUEST['iDisplayLength'] != '-1' )
 {
  $sLimit = "LIMIT ".intval( $_REQUEST['iDisplayStart'] ).", ".
   intval( $_REQUEST['iDisplayLength'] );
 }
 
 $sOrder = "";
 if ( isset( $_REQUEST['iSortCol_0'] ) )
 {
  $sOrder = "ORDER BY  ";
  for ( $i=0 ; $i<intval( $_REQUEST['iSortingCols'] ) ; $i++ )
  {
   if ( $_REQUEST[ 'bSortable_'.intval($_REQUEST['iSortCol_'.$i]) ] == "true" )
   {
    $sOrder .= "`".$aColumns[ intval( $_REQUEST['iSortCol_'.$i] ) ]."` ".
     ($_REQUEST['sSortDir_'.$i]==='asc' ? 'asc' : 'desc') .", ";
   }
  }
  
  $sOrder = substr_replace( $sOrder, "", -2 );
  if ( $sOrder == "ORDER BY" )
  {
   $sOrder = "";
  }
 }
 
 $sWhere = "";
 if ( isset($_REQUEST['sSearch']) && $_REQUEST['sSearch'] != "" )
 {
  $sWhere = "WHERE (";
  for ( $i=0 ; $i<count($aColumns) ; $i++ )
  {
   $sWhere .= "`".$aColumns[$i]."` LIKE '%".esc_sql( $_REQUEST['sSearch'] )."%' OR ";
  }
  $sWhere = substr_replace( $sWhere, "", -3 );
  $sWhere .= ')';
 }
  
 for ( $i=0 ; $i<count($aColumns) ; $i++ )
 {
  if ( isset($_REQUEST['bSearchable_'.$i]) && $_REQUEST['bSearchable_'.$i] == "true" && $_REQUEST['sSearch_'.$i] != '' )
  {
   if ( $sWhere == "" )
   {
    $sWhere = "WHERE ";
   }
   else
   {
    $sWhere .= " AND ";
   }
   $sWhere .= "`".$aColumns[$i]."` LIKE '%".esc_sql($_REQUEST['sSearch_'.$i])."%' ";
  }
 }
 
 $sQuery = "
  SELECT SQL_CALC_FOUND_ROWS `".str_replace(" , ", " ", implode("`, `", $aColumns))."`
  FROM   $sTable
  $sWhere
  $sOrder
  $sLimit
  ";
 $rResult = $wpdb->get_results($sQuery, ARRAY_A);
 
 $sQuery = "
  SELECT FOUND_ROWS()
 ";
 $rResultFilterTotal = $wpdb->get_results($sQuery, ARRAY_A); 
 $iFilteredTotal = $aResultFilterTotal[0];
 
 $sQuery = "
  SELECT COUNT(`".$sIndexColumn."`)
  FROM   $sTable
 ";
 $rResultTotal = $wpdb->get_results($sQuery, ARRAY_A); 
 $iTotal = $aResultTotal[0];
 
 $output = array(
  "sEcho" => intval($_REQUEST['sEcho']),
  "iTotalRecords" => $iTotal,
  "iTotalDisplayRecords" => $iFilteredTotal,
  "aaData" => array()
 );
 
 foreach($rResult as $aRow)
 {
  $row = array();
  for ( $i=0 ; $i<count($aColumns) ; $i++ )
  {
   if ( $aColumns[$i] == "version" )
   {    
    $row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
   }
   else if ( $aColumns[$i] != ' ' )
   {    
    $row[] = $aRow[ $aColumns[$i] ];
   }
  }
  $output['aaData'][] = $row;
 }
 
 echo json_encode( $output );
 die(); 
}

This conversion may not be perfect, but atleast it is letting me do my job. Yes, I am using $_REQUEST. It worked the first time, so I dint try $_POST. Why don't you try that and let me know so that I can mention here that $_POST works.

I found another problem with this example of mine which is based on the real work as mentioned above. I am unable to get the pipe-lined data. Well, will fix that again sometime.

Viewing all articles
Browse latest Browse all 82110

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>