<?php defined("BASEPATH") or exit("No direct script access allowed");
class Kullanici extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('Kullanici_model');
}
public function index(){
$this->load->view("liste");
}
public function DataTable(){
$post = $this->input->post();
$response = $this->Kullanici_model->DataTable($post);
echo(json_encode($response,true));
}
}
<?php defined("BASEPATH") or exit("No direct script access allowed");
class Kullanici_model extends CI_Model{
function DataTable($post){
$this->db->from('Kullanici');
if(! empty($post["search"]["value"])){
foreach ($post["columns"] as $column){
$this->db->or_like($column["data"],$post["search"]["value"]);
}
}
$ttl = $this->db->count_all_results();
$column = $post['order'][0]['column'];
$order_field = $post["columns"][$column]["data"];
$order_val = $post["order"][0]["dir"];
$this->db->select('*');
$this->db->from('Kullanici');
if(! empty($post["search"]["value"])){
foreach ($post["columns"] as $column){
$this->db->or_like($column["data"],$post["search"]["value"]);
}
}
$this->db->order_by("$order_field",$order_val);
if($this->input->post('length') > 0){
$this->db->limit($this->input->post('length'),$this->input->post('start'));
}
$rss = $this->db->get()->result_array();
$response["data"] = $rss;
$response["recordsTotal"] = $ttl;
$response["recordsFiltered"] = $ttl;
$response["query"] = $this->db->last_query();
return $response;
}
}
<?php defined("BASEPATH") or exit("No direct script access allowed");?>
<!doctype html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Codeingiter 3 ile Datatables Kullanımı</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="css/datatables.min.css">
<link rel="stylesheet" href="css/dataTables.bootstrap4.min.css">
<link rel="stylesheet" href="css/responsive.dataTables.min.css">
</head>
<body>
<table id="kullanici" class="table table-striped table-hover responsive nowrap" cellspacing="0" width="100%">
<thead class="bg-aksiyon text-white">
<tr>
<th scope="col">Adi Soyadı</th>
<th scope="col">Telefon</th>
<th scope="col">E Posta</th>
<th scope="col" class="text-right pr-30">İŞLEM</th>
</tr>
</thead>
<tbody></tbody>
<tfoot class="bg-aksiyon text-white">
<tr>
<th scope="col">Adi Soyadı</th>
<th scope="col">Telefon</th>
<th scope="col">E Posta</th>
<th scope="col" class="text-right pr-30">İŞLEM</th>
</tr>
</tfoot>
</table>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script src="js/datatables.min.js"></script>
<script src="js/dataTables.buttons.min.js"></script>
<script src="js/dataTables.responsive.min.js"></script>
<script src="js/buttons.colVis.min.js"></script>
<script src="js/buttons.html5.min.js"></script>
<script src="js/buttons.print.min.js"></script>
<script src="js/buttons.selected.js"></script>
<script src="js/jszip.min.js"></script>
<script src="js/pdfmake.min.js"></script>
<script src="js/vfs_fonts.js"></script>
<script src="datatables_script.js"></script>
</body>
</html>
$(document).ready(function () {
table = $('#kullanici').DataTable( {
iDisplayLength : 10,
pageLegenth : 10,
responsive : true,
lengthMenu : [[10,50,100,250,500, -1], ["10 Ad.","50 Ad.","100 Ad.","250 Ad.","500 Ad.", "Hepsi"]],
language : { url : 'js/Turkish.json' },
"processing" : true,
"serverSide" : true,
"ajax" : {
url : "Kullanici/DataTable",
type : 'post',
},
columns : [
{
data : 'adi',
},
{
data : 'telefon',
},
{
data : 'eposta',
},
{
data: null,
orderable: false,
class : 'text-right',
render:function (data, type, row) {
html = `<a href="Kullanici/Duzenle/${data.id}" class='btn btn-success btn-sm mr-10' >Düzenle</a>`;
return html;
}
},
],
order : [
[0,'desc']
],
columsDefs : [
{
targets : ['nosort'],
orderable : false
},
{
targets: 0,
className: 'noVis'
}
],
dom: 'Bflrtip',
buttons: [
{
extend: 'collection',
text: "İndir",
buttons: [
{
extend: 'excelHtml5',
text: "Excel",
exportOptions: {
columns: ':visible:not(:last-child)'
}
},
{
extend: 'csvHtml5',
text: "CSV",
exportOptions: {
columns: ':visible:not(:last-child)'
}
},
{
extend: 'pdfHtml5',
text: "PDF",
exportOptions: {
columns: ':visible:not(:last-child)'
}
},
{
extend: 'print',
text: "Yazdır",
exportOptions: {
columns: ':visible:not(:last-child)'
},
}
],
},
{
extend: 'colvis',
text: "Sütun Gizle/Göster",
className : 'btn btn-secondary',
columns: [':visible:not(:last-child)']
},
{
text: '<i class="icon-spinner9 mr-1"></i> Yeniden Yükle',
className : 'btn btn-dark',
action: function ( e, dt, node, config ) {
dt.ajax.reload(null, false);
}
},
{
text: '<i class="icon-user-plus mr-1"></i> Yeni Kullanıcı',
className : 'btn btn-success',
action: function ( e, dt, node, config ) {
location.href = 'Kullanici/Ekle';
}
}
],
"initComplete": function(settings, json) {
}
});
})