2017-07-25 12 views
0

Ich habe diesen Link verwiesen, um meine Codeigniter-Datatable Ajax-basierte Datentabelle zu machen.Codeigniter Ajax Datentabelle mit Fehler

http://mbahcoding.com/tutorial/php/codeigniter/codeigniter-simple-server-side-datatable-example.html

Aber es ist Fehler zeigt und nicht die Daten in Datentabelle zeigt.

Nachricht: Undefined index: Länge

Nachricht: Undefined index: Undefined index:

Nachricht beginnen zeichnen

Bitte jemand mir helfen, diese Fehler zu entfernen.

Mein JQuery

$(document).ready(function(){ 
     //Ajax Datatable 
     //datatables 
    var dataTable = $('#dataTables-suburb').DataTable({ 

     processing: true, //Feature control the processing indicator. 
     serverSide: true, //Feature control DataTables' server-side processing mode. 
     order: [], //Initial no order. 

     // Load data for the table's content from an Ajax source 
     ajax: { 
      url: "<?php echo site_url('admin/states/state_table_ajax')?>", 
      type: "POST" 
     }, 

     //Set column definition initialisation properties. 
     columnDefs: [ 
     { 
      targets: [ 0 ], //first column/numbering column 
      orderable: false, //set not orderable 
     }, 
     ], 


    }); 
}); 

Meine Regler Funktion

public function state_table_ajax() 
    { 
     $data['details'] = $this->LoginModel->admin_details($this->session->userdata('admin-username')); 
     foreach($data['details'] as $detail): 
     $country_id = $detail['country_id']; 
    endforeach; 
     $states = $this->StatesModel->get_states_table($country_id); 

     $no = $_POST['start']; 

     foreach($states as $state): 
      $no++; 
      $row = array(); 
      $row[] = $no; 
      $row[] = $state['id']; 
      $row[] = $state['state_name']; 
      //$row[] = '<button type="button" name="update" id="'.$state['id'].'" class="btn btn-warning btn-xs">Update</button>'; 
      //$row[] = '<button type="button" name="delete" id="'.$state['id'].'" class="btn btn-danger btn-xs">Delete</button>'; 
      $state_data[] = $row; 
     endforeach; 

     $output = array(
       "draw" => $_POST['draw'], 
       "recordsTotal" => $this->StatesModel->get_states_count($country_id), 
       "recordsFiltered" => $this->StatesModel->get_states_count_filtered($country_id), 
       "data" => $state_data, 
     ); 
     //echo "<pre>"; 
     //print_r($output); 
     //output to json format 
     echo json_encode($output); 
    } 

My Model Code

public function get_states($country_id) 
    { 
     $order_column = array('id', 'state_name'); 
     $this->db->select('id, state_name') 
          ->where('country_id', $country_id, FALSE) 
          ->from('tbl_states'); 

     //For Search value Datatable 
     if(isset($_POST['search']['value'])) 
     { 
      $this->db->like('state_name', $_POST['search']['value']); 
     } 
     //For Order Datatable 
     if(isset($_POST['order'])) 
     { 
      $this->db->order_by($this->order_column[$_POST['order']['0']['column']], $_POST['order']['0']['dir']); 
     } 
     else 
     { 
      $this->db->order_by('id', 'DESC'); 
     } 

     //$result = $query->result_array(); 
     //return $result; 
    } 
    //Get Datatable 
    public function get_states_table($country_id) 
    { 
     $this->get_states($country_id); 
     if($_POST["length"] != -1) 
     { 
      $this->db->limit($_POST["length"], $_POST["start"]); 
      $query = $this->db->where('country_id', $country_id, FALSE) 
           ->get(); 
      return $query->result_array(); 
     } 
    } 
    function get_states_count_filtered($country_id) 
    { 
     $this->get_states($country_id); 
     $query = $this->db->where('country_id', $country_id, FALSE) 
          ->get(); 
     return $query->num_rows(); 
    } 
    public function get_states_count($country_id) 
    { 
     $this->db->where('country_id', $country_id, FALSE) 
       ->from('tbl_states'); 


     return $this->db->count_all_results(); 
    } 

Vielen Dank im Voraus.

+0

Zeigen Sie Ihren HTML- und Jquery-Code –

+0

Jetzt zeigt dieser Becoz ich habe den Ajax in einer anderen Funktion aufgerufen und in einem anderen angezeigt. DataTables-Warnung: Tabellen-ID = DataTables-Vorort - Ajax-Fehler. Weitere Informationen zu diesem Fehler finden Sie unter http://datatables.net/tn/7 –

+0

Sie haben den Link bereitgestellt, der zeigt, um welchen Fehler es sich handelt und wie wir es lösen können. –

Antwort

0

In Ihrem Controller

von

$no = $_POST['start']; 

Um

if(isset($_POST['start']) && isset($_POST['draw'])) 
{ 
    $no = $_POST['start']; 
}else{ 
    // need start and draw 
    die(); // some error 
} 

und in Ihrem Modell

Von

if($_POST["length"] != -1) 

Um

if(isset($_POST["length"]) && $_POST["length"] != -1) 
0

Ändern Sie Ihr Model Code wie folgt aus:

public function get_states($country_id) 
    { 
     $order_column = array('id', 'state_name'); 

     $this->db->select('id, state_name'); 
     $this->db->from('tbl_states'); 
     $this->db->where('country_id', $country_id); 
     //For Search value Datatable 
     if(isset($_POST['search']['value'])) 
     { 
      $this->db->like('state_name', $_POST['search']['value']); 
     } 
     //For Order Datatable 
     if(isset($_POST['order'])) 
     { 
      $this->db->order_by($this->order_column[$_POST['order']['0']['column']], $_POST['order']['0']['dir']); 
     } 
     else 
     { 
      $this->db->order_by('id', 'DESC'); 
     } 
     $query = $this->db->get(); 
     $result = $query->result_array(); 
     return $result; 
    } 
    //Get Datatable 
    public function get_states_table($country_id,$length,$start) 
    { 
     $this->get_states($country_id); 
     if($length != -1) 
     { 
      $this->db->limit($length, $start); 
      $query = $this->db->where('country_id', $country_id, FALSE) 
           ->get(); 
      return $query->result_array(); 
     } 
    } 
    function get_states_count_filtered($country_id) 
    { 
     $this->get_states($country_id); 
     $query = $this->db->where('country_id', $country_id, FALSE) 
          ->get(); 
     return $query->num_rows(); 
    } 
    public function get_states_count($country_id) 
    { 
     $this->db->where('country_id', $country_id, FALSE) 
       ->from('tbl_states'); 


     return $this->db->count_all_results(); 
    } 

Und in der Steuerung Aufruf dieser Funktion:

get_states_table() 

wie folgt aus:

wo

$length = $_POST['length']; 
$start = $_POST['start']; 
get_states_table($country_id,$length,$start)