codeigniter - Datatables do not show -
i'm newbie jquery , ajax, found example combination using codeigniter , datatables. did try same way example datatables didn't show.
controller
class user extends ci_controller { private $content; private $data; function __construct(){ parent::__construct(); $this->load->model('usermodel'); $this->init(); } function init(){ if( $this->session->userdata('username') == '' ) redirect('auth'); $this->data['username'] = $this->session->userdata('username'); } function index(){ //$this->content = $this->load->view('userlistview', null, true); $this->content = 'users'; $this->data['content'] = $this->content; //$this->data['users'] = json_encode($this->usermodel->getdatatable()); $this->load->view('home', $this->data); } function data() { //print_r($this->usermodel->getdatatable()); echo json_encode($this->usermodel->getdatatable()); exit; }
model
class usermodel extends ci_model { function getlistall(){ $strsql = "select * mst_user"; $qdata = $this->db->query($strsql); return $qdata; } function getsingle($user_id){ $strsql = "select * mst_user id = '" . $user_id . "'"; $qdata = $this->db->query($strsql); return $qdata; } function getdatatable() { $stable = "mst_user"; $acolumns = array( 'id', 'username', 'fullname', 'email'); $sindexcolumn = "id"; /* * paging */ $slimit = ""; if ( isset( $_post['idisplaystart'] ) && $_post['idisplaylength'] != '-1' ){ $slimit = "limit ".addslashes( $_post['idisplaystart'] ).", " . addslashes( $_post['idisplaylength'] ); } /* * ordering */ $sorder = ""; if ( isset( $_post['isortcol_0'] ) ) { $sorder = "order "; ( $i=0 ; $i<intval( $_post['isortingcols'] ) ; $i++ ) { if ( $_post[ 'bsortable_'.intval($_post['isortcol_'.$i]) ] == "true" ) { $sorder .= "`".$acolumns[ intval( $_post['isortcol_'.$i] ) ]."` ". addslashes( $_post['ssortdir_'.$i] ) .", "; } } $sorder = substr_replace( $sorder, "", -2 ); if ( $sorder == "order by" ) { $sorder = ""; } } /* * filtering * note not match built-in datatables filtering * word word on field. it's possible here, concerned efficiency * on large tables, , mysql's regex functionality limited */ $swhere = ""; if ( isset($_post['ssearch']) && $_post['ssearch'] != "" ) { $swhere = "where ("; ( $i=0 ; $i<count($acolumns) ; $i++ ) { $swhere .= "`".$acolumns[$i]."` '%".addslashes( $_post['ssearch'] )."%' or "; } $swhere = substr_replace( $swhere, "", -3 ); $swhere .= ')'; } //echo $swhere;exit; /* individual column filtering */ ( $i=0 ; $i<count($acolumns) ; $i++ ) { if ( isset($_post['bsearchable_'.$i]) && $_post['bsearchable_'.$i] == "true" && $_post['ssearch_'.$i] != '' ) { if ( $swhere == "" ) { $swhere = "where "; } else { $swhere .= " , "; } $swhere .= "`".$acolumns[$i]."` '%".addslashes($_post['ssearch_'.$i])."%' "; } } /* * sql queries * data display */ $squery = "select sql_calc_found_rows `".str_replace(" , ", " ", implode("`, `", $acolumns))."` $stable $swhere $sorder $slimit"; //$rresult = mysql_query( $squery, $gasql['link'] ) or die(mysql_error()); $stmt = $this->db->query($squery); //$stmt->setfetchmode(zend_db::fetch_both); $rresult = $stmt->result_array(); //return $rresult; //echo $squery;exit; /* data set length after filtering */ $squery = " select found_rows() fr"; //$rresultfiltertotal = mysql_query( $squery, $gasql['link'] ) or die(mysql_error()); $stmt = $this->db->query($squery); //$stmt->setfetchmode(zend_db::fetch_both); //$aresultfiltertotal = mysql_fetch_array($rresultfiltertotal); $aresultfiltertotal = $stmt->result(); //return $aresultfiltertotal; //$aresultfiltertotal = $stmt->fetchall(); $ifilteredtotal = $aresultfiltertotal[0]->fr; //echo $ifilteredtotal;exit; //print_r($aresultfiltertotal);exit; /* total data set length */ $squery = "select count(`".$sindexcolumn."`) total $stable"; //$rresulttotal = mysql_query( $squery, $gasql['link'] ) or die(mysql_error()); //echo $squery;exit; $stmt = $this->db->query($squery); //$stmt->setfetchmode(zend_db::fetch_both); //$aresulttotal = mysql_fetch_array($rresulttotal); $aresulttotal = $stmt->result(); $itotal = $aresulttotal[0]->total; //echo $itotal;exit; /* * output */ /* if(isset($_post['secho'])) $secho = intval($_post['secho']); else $secho = '';*/ $output = array( "secho" => intval($_post['secho']), "itotalrecords" => $itotal, "itotaldisplayrecords" => $ifilteredtotal, "aadata" => array() ); foreach ($rresult $arow) { $row = array(); ( $i=0 ; $i<count($acolumns) ; $i++ ) { if ( $acolumns[$i] == "version" ) { /* special output formatting 'version' column */ $row[] = ($arow[ $acolumns[$i] ]=="0") ? '-' : $arow[ $acolumns[$i] ]; } else if ( $acolumns[$i] != ' ' ) { /* general output */ $row[] = $arow[ $acolumns[$i] ]; } } $output['aadata'][] = $row; } return $output; } }
view
<script type="text/javascript"> $(document).ready(function() { $(".button").button(); $('#table_list').datatable({ "bjqueryui": true, "bserverside": true, "sservermethod": "post", "spaginationtype": "full_numbers", "sajaxsource": "<?php echo base_url();?>index.php/user/data" }); }); </script> <?php //echo $users;?> <form class="form_element" action=""> <div id="content_header"><h2>user - list</h2></div> <div style="float:right;padding-bottom:5px"> <input type="button" value="create new record" onclick="window.location='user/form/add'" class="button"> </div> <hr> <table id="table_list"> <thead> <tr> <td>id</td> <td width="200">user name</td> <td>full name</td> <td>email</td> </tr> </thead> <tbody> </tbody> </table> </form>
home view
<html> <head> <title>admin mos template</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta http-equiv="copyright" content=""> <meta name="description" content="admin mos template"> <meta name="keywords" content="admin page"> <meta name="author" content=""> <meta name="language" content="bahasa indonesia"> <link rel="shortcut icon" href="stylesheet/img/devil-icon.png"> <!--pemanggilan gambar favicon--> <link rel="stylesheet" type="text/css" href="<?php echo base_url();?>include/mos_admin/mos-css/mos-style.css"> <!--pemanggilan file css--> <link type="text/css" href="<?php echo base_url(); ?>include/css/jquery.datatables.css" rel="stylesheet" /> <link type="text/css" href="<?php echo base_url(); ?>include/css/smoothness/jquery-ui-1.8.19.custom.css" rel="stylesheet" /> <script type="text/javascript" src="<?php echo base_url(); ?>include/js/jquery-1.7.2.min.js"></script> <script type="text/javascript" src="<?php echo base_url(); ?>include/js/jquery.datatables.min.js"></script> </head> <body> <?php //print_r($users); //echo $strformtype; ?> <div id="header"> <div class="inheader"> <div class="mosadmin"> <?php $this->load->view('headerview');?> </div> <div class="clear"></div> </div> </div> <div id="wrapper"> <div id="leftbar"> <?php $this->load->view('leftbarview'); ?> </div> <div id="rightcontent"> <?php if($content == 'changepass'){ $this->load->view('changepassview'); } else if($content == 'form'){ $this->load->view('userformview'); } else if($content == 'users'){ $this->load->view('userlistview'); } else{ $this->load->view('rightcontentview'); } ?> </div> <div class="clear"></div> <div id="footer"> <?php $this->load->view('footerview'); ?> </div> </div> </body> </html>
are chance confusing 'data' object in $this->data data method have there (function data() etc)? using ajax , need use js code well, jquery:
$(document).ready(function() { $('#element_trigger_ajax').click(function(){ var my_url = 'your url';//www.example.com/user/data params = {elem_id:$('#some_param').val()}; $.ajax({ async:false, type:'post', url: my_url, data:params, success: function(resp){ # load response data method in user controller within element container div $('div#element_container_that_will_change').html(resp); } }); }); });
Comments
Post a Comment