Welcome Guest, Not a member yet? Register   Sign In
Datatables library
#1

Hello guys,

If you are using Datatables jquery plugin with Codeigniter 3, and having hard time to setup server side processing, here is the library makes things easier for you.

https://github.com/n1crack/Datatables/

Installing: 
via composer: (start using composer if you haven't yet.)

Code:
composer require ozdemir/datatables

set vendor path in "config/config.app":

PHP Code:
$config['composer_autoload'] = 'vendor/autoload.php'

And here is the ajax code.  just 3 lines of code Smile 

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

use 
Ozdemir\Datatables\Datatables;
use 
Ozdemir\Datatables\DB\CodeigniterAdapter;

class 
Welcome extends CI_Controller {

    public function index()
    {
        $this->load->view('welcome_message');
    }

    public function ajax()
    {
        $datatables = new Datatables(new CodeigniterAdapter);
        $datatables->query('Select film_id, title, description from film');
        echo $datatables->generate();
    }



I know there are tons of library out there. But I have learned a lot of things while playing with this. I would be happy if it helps you. 

Regards.
Yusuf
Reply
#2

(This post was last modified: 06-19-2017, 11:44 PM by Corsari.)

Hello Yusuf

your library looks having a great potential and on Github I see you keep it updated

I'm using xampp and I have correctly installed composer for my xampp environment

One kind hint since I'm new to composer and also pretty new with Codeigniter

In your Github you instruct to
just run :
Code:
composer require ozdemir/datatables

this will be run from the DOS prompt,

so my hint/clarification request is regarding-->
where should I locate the current dir before to run the composer command?

in the main codeigniter' directory
Code:
D:\>cd d:\xampp\htdocs\cidttest

or inside the application subdirectory
Code:
D:\>cd d:\xampp\htdocs\cidttest\application

Thank you for clarifying

Also can you kindly tell me which components and in which directories, composer will add to the project?
(I know, I can check it by myself after having run it, but a brief description is welcome)
Reply
#3

Hi,
It should be in the main directory. 
CI 3 already has the composer.json file as default.

when you run "composer install"  it will create a vendor file.



Code:
"require-dev": {
"mikey179/vfsStream": "1.1.*",
"phpunit/phpunit": "4.* || 5.*"
}


and because of this section exists it installs some test suits for you. (if you dont want to use these, you can remove them. and run "composer update" to update vendor folder.)


Still if you don't want to use composer, and only if you insist, these instructions are for you.

1- Download github repo. 
2- Copy src folder into your application/libraries folder,
3- rename it to Datatables from src
4- create a new file called Datatables.php in the application/libraries folder
5- copy the codes below and save it.

Datatables.php

PHP Code:
<?php
include_once "Datatables/Datatables.php";
include_once 
"Datatables/DB/DatabaseInterface.php";
include_once 
"Datatables/DB/CodeigniterAdapter.php";

use 
Ozdemir\Datatables\Datatables as DT;
use 
Ozdemir\Datatables\DB\CodeigniterAdapter;

class 
Datatables extends DT {
    function __construct()
    {
        parent::__construct(new CodeigniterAdapter);
    }



and in your controller usage will be simply like this : 

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Welcome extends CI_Controller {

    public function 
index()
    {
        
$this->load->view('welcome_message');
    }

    public function 
ajax()
    {
        
$this->load->library('Datatables');
        
        
$dt $this->datatables->query("select name, surname, phone, address from users");
        
        echo 
$dt->generate();
    }

I hope it helps you :Smile (with this, I think you can run it with CI 2.0 too.)
Regards
Reply
#4

Hello,
How can I use the library if I'm using multiple databases in my Codeigniter project??
Reply
#5

(This post was last modified: 12-28-2018, 12:57 PM by numberone.)

added online examples : https://datatables.ozdemir.be/
Reply
#6

(This post was last modified: 02-03-2022, 09:29 AM by numberone.)

added new adapter for codeigniter 4.

example usage : https://datatables.ozdemir.be/codeigniter4
Reply




Theme © iAndrew 2016 - Forum software by © MyBB