2017-09-21 3 views
0

J'ai 2 table et les joindre au côté serveur DataTables, mais ses montrent les lignes en double en vue, ce que je me trompedatatables filtre Afficher les lignes en double

Modèle:

var $table = 'user_provider'; 
    var $column_order = array(null,'u.nm_uprov','u.email','g.nm_gprov'); //set column field database for datatable orderable 
    var $column_search = array('u.nm_uprov','u.email','g.nm_gprov'); 
    var $order = array('u.id_uprov' => 'desc'); // default order 

    function __construct(){ 
     parent::__construct(); 
     $this->load->database(); 

    } 

    private function _get_datatables_query() 
    { 

     $this->db->from($this->table); 
     $this->db->select('*'); 
     $this->db->from('user_provider as u'); 
     $this->db->join('grup_provider as g','u.id_gprov = g.id_gprov'); 

     $i = 0; 

     foreach ($this->column_search as $item) // loop column 
     { 
      if($_POST['search']['value']) // if datatable send POST for search 
      { 

       if($i===0) // first loop 
       { 
        $this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND. 
        $this->db->like($item, $_POST['search']['value']); 
       } 
       else 
       { 
        $this->db->or_like($item, $_POST['search']['value']); 
       } 

       if(count($this->column_search) - 1 == $i) //last loop 
        $this->db->group_end(); //close bracket 
      } 
      $i++; 
     } 

     if(isset($_POST['order'])) // here order processing 
     { 
      $this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']); 
     } 
     else if(isset($this->order)) 
     { 
      $order = $this->order; 
      $this->db->order_by(key($order), $order[key($order)]); 
     } 
    } 

Controller:

public function ajax_list() 
    { 
     $list = $this->M_user_provider->get_datatables(); 
     $data = array(); 
     $no = $_POST['start']; 
     foreach ($list as $uprov) { 
      $no++; 
      $row = array(); 
      $row[] = $no; 
      $row[] = $uprov->nm_uprov; 
      $row[] = $uprov->email; 
      $row[] = $uprov->nm_gprov; 

      //add html for action 
      $row[] = '<a class="btn btn-info btn-fill btn-sm" href="javascript:void(0)" title="Edit" onclick="edit_pic('."'". $uprov->id_uprov."'".')"><i class="glyphicon glyphicon-pencil"></i> Edit</a> 
        <a class="btn btn-danger btn-fill btn-sm" href="javascript:void(0)" title="Hapus" onclick="delete_pic('."'".$uprov->id_uprov."'".')"><i class="glyphicon glyphicon-trash"></i> Hapus</a>'; 

      $data[] = $row; 
     } 

     $output = array(
         "draw" => $_POST['draw'], 
         "recordsTotal" => $this->M_user_provider->count_all(), 
         "recordsFiltered" => $this->M_user_provider->count_filtered(), 
         "data" => $data, 
       ); 
     //output to json format 
     echo json_encode($output); 
    } 

Tableau:

  • fournisseur utilisateur table

  • Groupe Fournisseur table

et dans le bas du tableau montrent ce "Afficher 1 à 4 de 4 entrées (filtrées de 2 entrées au total)", Je ne sais pas ce qui ne va pas parce que tout fonctionne bien sauf la table qui montre les lignes en double.

+0

S'il vous plaît poster votre structure de table. –

+0

oui, j'ai ajouté la structure de la table, s'il vous plaît jeter un oeil –

Répondre

0

Vous ajoutez deux fois la table:

$this->db->from($this->table); 
$this->db->select('*'); 
$this->db->from('user_provider as u'); 

Essayez de supprimer d'abord "de" clause, parce que dans le second, vous ajoutez un alias

+0

ohh vous avez raison, ça va maintenant, merci –