Skip to main content

dataTable in laravel without Yajra

Add this following link in the head tag of your project: 

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css"/>


<div class="container">
        <div class="row">
            <div class="col-md-12">
                <table class="table table-bordered" id="posts">
                    <thead>
                            <th>Id</th>
                            <th>Title</th>
                            <th>Body</th>
                            <th>Created At</th>
                            <th>Options</th>
                    </thead>                
                </table>
            </div>
        </div>
    </div>


<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
    <script>
        $(document).ready(function () {
            $('#posts').DataTable({
                "processing": true,
                "serverSide": true,
                "ajax":
                {
                    "url": "{{ url('allposts') }}",
                    "dataType": "json",
                    "type": "POST",
                    "data":{
                        "_token": "{{csrf_token()}}",
                    }
                },
                "columns": [
                    { "data": "id" },
                    { "data": "title" },
                    { "data": "body" },
                    { "data": "created_at" },
                    { "data": "options" }
                ]
            });

            
        });
    </script>

In the controller write:

Route::get('/post','PostController@index');
Route::post('allposts''PostController@allPosts' )->name('allposts');



public function index()
    {
        return view('homepage');
    }

    public function allPosts(Request $request)
    {
        
        $columns = array
            0 =>'id'
            1 =>'title',
            2=> 'body',
            3=> 'created_at',
            4=> 'id',
        );
  
        $totalData = Post::count();
            
        $totalFiltered = $totalData

        $limit = $request->input('length');
        $start = $request->input('start');
        $order = $columns[$request->input('order.0.column')];
        $dir = $request->input('order.0.dir');
            
        if(empty($request->input('search.value')))
        {            
            $posts = Post::offset($start)
                         ->limit($limit)
                         ->orderBy($order,$dir)
                         ->get();
        }
        else {
            $search = $request->input('search.value'); 

            $posts =  Post::where('id','LIKE',"%{$search}%")
                            ->orWhere('title''LIKE',"%{$search}%")
                            ->offset($start)
                            ->limit($limit)
                            ->orderBy($order,$dir)
                            ->get();

            $totalFiltered = Post::where('id','LIKE',"%{$search}%")
                             ->orWhere('title''LIKE',"%{$search}%")
                             ->count();
        }

        $data = array();
        if(!empty($posts))
        {
            foreach ($posts as $post)
            {
                // $show =  route('posts.show',$post->id);
                // $edit =  route('posts.edit',$post->id);

                $nestedData['id'] = $post->id;
                $nestedData['title'] = $post->title;
                $nestedData['body'] = substr(strip_tags($post->body),0,50)."...";
                $nestedData['created_at'] = date('j M Y h:i a',strtotime($post->created_at));
                // $nestedData['options'] = "&emsp;<a href='{$show}' title='SHOW' ><span class='glyphicon glyphicon-list'></span></a>
                                        //   &emsp;<a href='{$edit}' title='EDIT' ><span class='glyphicon glyphicon-edit'></span></a>";
                $nestedData['options'] = "&emsp;<a href='#' title='SHOW' ><span class='fa fa-eye'></span></a>
                                          &emsp;<a href='#' title='EDIT' ><span class='fa fa-pencil'></span></a>";
                $data[] = $nestedData;

            }
        }
          
        $json_data = array(
                    "draw"            => intval($request->input('draw')),  
                    "recordsTotal"    => intval($totalData),  
                    "recordsFiltered" => intval($totalFiltered), 
                    "data"            => $data   
                    );
            
        echo json_encode($json_data); 
        
    }


Thanks.







Comments

Popular posts from this blog

SQL: sneak peek

show databases; use mysql; show tables; select * from component; describe component; create database sql_intro; show databases; use sql_intro; create table emp_details (Name varchar(25), Age int, gender char(1), doj date, city varchar(15), salary float); describe emp_details; insert into emp_details  values("Jimmy",35,"M","2005-05-30","Chicago",70000), ("Shane",30,"M","1999-06-25","Seattle",55000), ("Marry",28,"F","2009-03-10","Boston",62000), ("Dwayne",37,"M", "2011-07-12","Austin", 57000), ("Sara",32,"F","2017-10-27","New York",72000), ("Ammy",35,"F","2014-12-20","Seattle",80000); select * from emp_details; select distinct city from emp_details; select count(name) as count_name from emp_details; select avg(salary) from emp_details; select name, age...