Giter VIP home page Giter VIP logo

ci4-datatables's Introduction

GitHub GitHub repo size Hits

ci4-datatables

Server Side Datatables Library for CodeIgniter 4 Framework NOTE: This lib is under early development.

Description

Library to make server side Datatables on CodeIgniter 4 to be more easy

Requirements

  • Codeigniter 4.*
  • JQuery 3.*
  • JQuery Datatables

Installation

Installation is best done via Composer, you may use the following command:

composer require irsyadulibad/codeigniter4-datatables

This will add the latest release of codeigniter4-datatables as a module to your project.

Manual Installation

Should you choose not to use Composer to install, you can download this repo, extract and rename this folder to codeigniter4-datatables. Then enable it by editing app/Config/Autoload.php and adding the Irsyadulibad\DataTables namespace to the $psr4 array. For example, if you copied it into app/Libraries:

    $psr4 = [
        'Config'      => APPPATH . 'Config',
        APP_NAMESPACE => APPPATH,
        'App'         => APPPATH,
        'Irsyadulibad\DataTables'   => APPPATH .'Libraries/codeigniter4-datatables/src',
    ];

Example:

This is an example code for using this library:

  • PHP:
<?php namespace App\Controllers;

use Irsyadulibad\DataTables\DataTables;

class Home extends BaseController
{
	public function json()
	{
		return DataTables::use('users')
			->where(['role' => 'admin'])
			->hideColumns(['password'])
			->rawColumns(['bio'])
			->make(true);
	}
}
  • Javascript
$('#table').DataTable({
  processing: true,
  serverSide: true,
  ajax:{
    url: 'http://localhost:8080/json'
  },
  columns: [
	  {data: 'username', name: 'username'},
	  {data: 'email', name: 'email'},
	  {data: 'fullname', name: 'fullname'}
	  {data: 'bio', name: 'bio'}
  ]
});

Documentation:

Now you can use this without instantiate class

DataTables::use('table');

We did not use the POST method due to a problem with the CSRF

$routes->get('datatables/json', 'Controller::method', ['as' => 'dt-json']);
  • Select Table
    Select the table that you want to use
DataTables::use('table')
  • Set Output
    The default parameter is true, which is automatically return the JSON data. You can return the data's dump by passing the false param
DataTables::use('table')
	->make(false);
  • Select Fields
    Select the sepicifics column in the table
->select('username, password')
  • Where Clause
->where(['role' => 'user', 'active' => 1])
  • Join Clause
// <table>, <condition>, <type>
->join('address', 'users.id = address.uid', 'INNER JOIN')
  • Add Column
    Add custom column which is not in the table
// <name>, <callback>
->addColumn('action', function($data) {
	return '<a href="/edit/'.$data->id.'">edit</a>';
})
  • Edit Column\
// <name>, <callback>
->editColumn('created_at', function($data) {
	return format($data);
})
  • Raw Columns
    By default, all of the data is escaped to prevent XSS. But if you want to unescape them, you can use this method
->rawColumns(['bio'])
  • Hide Columns
    Hide columns from JSON output
->hideColumns(['password'])

Notes:

  • For now, we don't use the POST method due to a problem with the CSRF

Author's Profile:

Github: [https://github.com/irsyadulibad]
Website: [http://irsyadulibad.my.id]
Facebook: [https://facebook.com/irsyadulibad.dev]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.