Code Monkey home page Code Monkey logo

bitdatatable's Introduction

BITDataTable : jQuery DataTables for Laravel 5

This package is created to handle server-side works of DataTables jQuery Plugin via AJAX option by using Eloquent ORM / Query Builder.

This package only tested on Laravel 5.6, 5.7 & 5.8 so if you have any problem or any question you can call me or open new issues

Quick Installation

composer require bitstudio-id/bitdatatable

Requirements

Javascript and CSS

<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css"/>
<link rel="stylesheet" href="//cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css"/>

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script src="//cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>

Blade view

<table id="table" class="table table-bordered table-striped">
  <thead>
  <tr>
      <th>#ID</th>
      <th>Name</th>
      <th>Role</th>
      <th>Email</th>
      <th>...</th>
  </tr>
  </thead>
  <tbody></tbody>
  </table>

Javascript

<script>
  $(document).ready(function () {
    //hide error/warning on datatable
    $.fn.dataTable.ext.errMode = 'none';
    
    var table = $('#table').DataTable({
      //enable filter
      
      bFilter: true,
      processing: true,
      serverSide: true,
      ajax: {
        url: "/dummy/dtb-v2/get",
        type: 'get',
      },
      columns: [
        {data: "id", name: "id", searchable: false, orderable: false},
        {data: "employee.code"},
        {data: "name", name: "user_name"},
        {data: "employee.role.name"},
        {data: "email"},
        {data: "action", searchable: false, orderable: false}, // use searchable: false, orderable: false for custom column
      ],
    });
  });
</script>

How to use with eloquent

use BITStudio\BITDataTable\BITDataTable;
...
...
public function dtbGetV2(Request $request)
{
  $dtb = new BITDataTable();

  // Set request
  $dtb->setRequest($request);

  $user = User::query()->with('employee', 'employee.role');

  $dtb->from($user);
  
  $state = "admin";

  $dtb->addCol(function ($user){
    $user->action = "<a target='_blank' href='//lorem.com/{$user->id}' class='btn btn-danger'>action-{$item->id}</a>";
    return $user;
  });

  return $dtb->generate();
}

How to use logic on addCol

$state = "admin";

$dtb->addCol(function ($user) use ($state){
  //use logic on addCol
      
  //set as empty default
  $user->admin_col = "";
  
  if($state == $user->role->name) {
      $user->admin_col .= "admin-col";
  }
   return $user;
});

How to use with Query Builder

use BITStudio\BITDataTable\BITDataTable;
...
...
public function dtbGetV2(Request $request)
  $dtb = new BITDataTable();
  
  $dtb->setRequest($request);
  
  $q = DB::table("orders as o");
  $q->select("o.*", "o.no_cs as customer_number", "e.employee_name as emp_name");
  $q->leftJoin("employee as e", "e.id", "=", "o.employee_id");
  
  $dtb->from($q);
  
  
  //add custom column
  $dtb->addCol(function ($user){
    $user->action = "<a target='_blank' href='//google.com/{$item->id}' class='btn btn-danger'>action-{$item->id}</a>";
  
    return $user;
  });
  
  return $dtb->generate();
}

add value class attribute

$dtb->addClass("text-danger");

add value id attribute

//create custom from collection property
$dtb->setRowId("id");

//create custom from addCol or setRowId for custom id attribute
$dtb->setRowId(function($item) {
    $item->DT_RowId = "id-".$item->id;
    return $item;
});

License

The MIT License (MIT). Please see License File for more information.

bitdatatable's People

Contributors

cacing69 avatar salihinlandrex avatar

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.