DataTable not responsive

DataTable not responsive

FullstackFullstack Posts: 3Questions: 1Answers: 0

Hi,

I followed this page: https://datatables.net/download/ to make my vendor data table responsive with no success.

  1. Chose a styling framework- I selected bootstrap 5 (Not sure if this is the right choice).

  2. Select Packages- I chose DataTables (Not sure if this is the right choice).

  3. Extension- I chose Responsive (Again not sure if this is the right choice but I chose since I want the data table to be responsive).

The console is not showing an error. Grateful for your guidance.

Here is the setup:

This is the VendorProductDataTable.php:


<?php namespace App\DataTables; use App\Models\Product; use Illuminate\Database\Eloquent\Builder as QueryBuilder; use Illuminate\Support\Facades\Auth; use Yajra\DataTables\EloquentDataTable; use Yajra\DataTables\Html\Builder as HtmlBuilder; use Yajra\DataTables\Html\Button; use Yajra\DataTables\Html\Column; use Yajra\DataTables\Html\Editor\Editor; use Yajra\DataTables\Html\Editor\Fields; use Yajra\DataTables\Services\DataTable; class VendorProductDataTable extends DataTable { /** * Build the DataTable class. * * @param QueryBuilder $query Results from query() method. */ public function dataTable(QueryBuilder $query): EloquentDataTable { return (new EloquentDataTable($query)) ->addColumn('action', function ($query) { $editBtn = "<a href='" . route('vendor.products.edit', $query->id) . "' class='btn btn-primary'><i class='far fa-edit'></i></a>"; $deleteBtn = "<a href='" . route('vendor.products.destroy', $query->id) . "' class='btn btn-danger delete-item' ><i class='far fa-trash-alt'></i></a>"; $moreBtn = '<div class="btn-group dropstart" style="margin-left:3px"> <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false"> <i class="fas fa-cog"></i> </button> <ul class="dropdown-menu"> <li><a class="dropdown-item has-icon" href="' . route('vendor.products-image-gallery.index', ['product' => $query->id]) . '"> Image Gallery</a></li> </ul> </div>'; return $editBtn . $deleteBtn . $moreBtn; }) ->addColumn('image', function ($query) { return "<img width='70px' src='" . asset($query->thumb_image) . "' ></img>"; }) ->addColumn('type', function ($query) { switch ($query->product_type) { case 'new_arrival': return '<i class="badge bg-success">New Arrival</i>'; break; case 'featured_product': return '<i class="badge bg-warning">Featured Product</i>'; break; case 'top_product': return '<i class="badge bg-info">Top Product</i>'; break; case 'best_product': return '<i class="badge bg-danger">Top Product</i>'; break; default: return '<i class="badge bg-dark">None</i>'; break; } }) ->addColumn('status', function ($query) { if ($query->status == 1) { $button = '<div class="form-check form-switch"> <input checked class="form-check-input change-status" type="checkbox" id="flexSwitchCheckDefault" data-id="' . $query->id . '"></div>'; } else { $button = '<div class="form-check form-switch"> <input class="form-check-input change-status" type="checkbox" id="flexSwitchCheckDefault" data-id="' . $query->id . '"></div>'; } return $button; }) ->rawColumns(['image', 'type', 'status', 'action']) ->setRowId('id'); } /** * Get the query source of dataTable. */ public function query(Product $model): QueryBuilder { return $model->where('vendor_id', Auth::user()->vendor->id)->newQuery(); } /** * Optional method if you want to use the html builder. */ public function html(): HtmlBuilder { return $this->builder() ->setTableId('vendorproduct-table') ->columns($this->getColumns()) ->minifiedAjax() ->orderBy(0) ->selectStyleSingle() ->pageLength(5) // Set the default number of items per page ->buttons([ Button::make('excel'), Button::make('csv'), Button::make('pdf'), Button::make('print'), Button::make('reset'), Button::make('reload') ]); } /** * Get the dataTable columns definition. */ public function getColumns(): array { return [ Column::make('id'), Column::make('image')->width(150), Column::make('product_name'), Column::make('price'), Column::make('type')->width(150), Column::make('status'), Column::computed('action') ->exportable(false) ->printable(false) ->width(200) ->addClass('text-center'), ]; } /** * Get the filename for export. */ protected function filename(): string { return 'VendorProduct_' . date('YmdHis'); } }

This is the index.blade.php:


@extends('vendor.dashboard.layouts.master') @section('content') <!-- ======= Vendor Dashboard ======= --> <section id="wsus__dashboard"> <div class="container-fluid"> @include('vendor.dashboard.layouts.sidebar') <div class="row"> <div class="col-xl-9 col-xxl-10 col-lg-9 ms-auto"> <div class="dashboard_content mt-2 mt-md-0"> <h3><i class="far fa-user"></i> Produkte</h3> <div class="create_button"> <a href="{{ route('vendor.products.create') }}" class="btn btn-primary"><i class="fas fa-plus"></i> Produkt erstellen</a> </div> <div class="wsus__dashboard_profile"> <div class="wsus__dash_pro_area"> {{ $dataTable->table() }} </div> </div> </div> </div> </div> </div> </section> @endsection @push('scripts') {{ $dataTable->scripts(attributes: ['type' => 'module']) }} <script> $(document).ready(function() { $('body').on('click', '.change-status', function() { let isChecked = $(this).is(':checked'); let id = $(this).data('id'); $.ajax({ url: "{{ route('vendor.product.change-status') }}", method: 'PUT', data: { status: isChecked, id: id }, success: function(data) { toastr.success(data.message) }, error: function(xhr, status, error) { console.log(error); } }) }) }) </script> @endpush

Answers

  • FullstackFullstack Posts: 3Questions: 1Answers: 0

    This is the master.blade.php:


    <!DOCTYPE html> <html lang="de"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="csrf-token" content="{{ csrf_token() }}" /> <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700;800&display=swap" rel="stylesheet"> <title>Online Shop | e-Commerce HTML Template</title> <link rel="icon" type="image/png" href="images/favicon.png"> <link rel="icon" type="image/png" href=""> <link rel="stylesheet" href="{{ asset('backend/userdashboard/css/all.min.css') }}"> <link rel="stylesheet" href="{{ asset('backend/userdashboard/css/bootstrap.min.css') }}"> <link rel="stylesheet" href="{{ asset('backend/userdashboard/css/select2.min.css') }}"> <link rel="stylesheet" href="{{ asset('backend/userdashboard/css/slick.css') }}"> <link rel="stylesheet" href="{{ asset('backend/userdashboard/css/jquery.nice-number.min.css') }}"> <link rel="stylesheet" href="{{ asset('backend/userdashboard/css/jquery.calendar.css') }}"> <link rel="stylesheet" href="{{ asset('backend/userdashboard/css/add_row_custon.css') }}"> <link rel="stylesheet" href="{{ asset('backend/userdashboard/css/mobile_menu.css') }}"> <link rel="stylesheet" href="{{ asset('backend/userdashboard/css/jquery.exzoom.css') }}"> <link rel="stylesheet" href="{{ asset('backend/userdashboard/css/multiple-image-video.css') }}"> <link rel="stylesheet" href="{{ asset('backend/userdashboard/css/ranger_style.css') }}"> <link rel="stylesheet" href="{{ asset('backend/userdashboard/css/jquery.classycountdown.css') }}"> <link rel="stylesheet" href="{{ asset('backend/userdashboard/css/venobox.min.css') }}"> <link rel="stylesheet" href="{{ asset('backend/assets/modules/summernote/summernote-bs4.css') }}"> <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css"> <link href="https://cdn.datatables.net/v/bs5/dt-2.1.7/r-3.0.3/datatables.min.css" rel="stylesheet"> <link rel="stylesheet" href="{{ asset('backend/assets/modules/bootstrap-daterangepicker/daterangepicker.css') }}"> <link rel="stylesheet" href="{{ asset('backend/userdashboard/css/style.css') }}"> <link rel="stylesheet" href="{{ asset('backend/userdashboard/css/responsive.css') }}"> </head> <body> <!-- Dashboard Menu Start --> <div class="wsus__dashboard_menu"> <div class="wsusd__dashboard_user"> <img src="{{ Auth::user()->image ? asset(Auth::user()->image) : asset('images/') }}" alt="User Image" class="img-fluid"> <p>Hallo, {{ auth()->user()->name }}!</p> <!-- Display the user's name here --> </div> </div> {{-- Explanation {{-- * {{ Auth::user()->image ? asset(Auth::user()->image) : asset('images/') }}: This is a ternary operation within a Blade template. * Auth::user()->image: This retrieves the image attribute of the currently authenticated user. * asset(Auth::user()->image): This generates a URL for the image if it exists. * asset('images/'): This generates a URL for a default image directory if the user's image does not exist. * src="{{ ... }}": The URL for the image is dynamically set based on whether the user has an image or not. * alt="User Image": This sets the alternative text for the image. * class="img-fluid": This applies the Bootstrap class img-fluid to the image, making it responsive. * * User Greeting: * <p>Hallo, {{ auth()->user()->name }}!</p> * {{ auth()->user()->name }}: This retrieves and displays the name attribute of the currently authenticated user. * This line displays a greeting message, "Hallo, [User's Name]!". --}} <!-- Dashboard Menu End --> <!--Dashboard Start--> @yield('content') <!--Scroll Button Start--> <div class="wsus__scroll_btn"> <i class="fas fa-chevron-up"></i> </div> <!--jquery library js--> <!--jquery library js--> <script src="{{ asset('backend/userdashboard/js/jquery-3.6.0.min.js') }}"></script> <!--bootstrap js--> <script src="{{ asset('backend/userdashboard/js/bootstrap.bundle.min.js') }}"></script> <!--font-awesome js--> <script src="{{ asset('backend/userdashboard/js/Font-Awesome.js') }}"></script> <!--select2 js--> <script src="{{ asset('backend/userdashboard/js/select2.min.js') }}"></script> <!--slick slider js--> <script src="{{ asset('backend/userdashboard/js/slick.min.js') }}"></script> <!--simplyCountdown js--> <script src="{{ asset('backend/userdashboard/js/simplyCountdown.js') }}"></script> <!--product zoomer js--> <script src="{{ asset('backend/userdashboard/js/jquery.exzoom.js') }}"></script> <!--nice-number js--> <script src="{{ asset('backend/userdashboard/js/jquery.nice-number.min.js') }}"></script> <!--counter js--> <script src="{{ asset('backend/userdashboard/js/jquery.waypoints.min.js') }}"></script> <script src="{{ asset('backend/userdashboard/js/jquery.countup.min.js') }}"></script> <!--add row js--> <script src="{{ asset('backend/userdashboard/js/add_row_custon.js') }}"></script> <!--multiple-image-video js--> <script src="{{ asset('backend/userdashboard/js/multiple-image-video.js') }}"></script> <!--sticky sidebar js--> <script src="{{ asset('frontend/assets/js/sticky_sidebar.js') }}"></script> <!--price ranger js--> <script src="{{ asset('backend/userdashboard/js/ranger_jquery-ui.min.js') }}"></script> <script src="{{ asset('backend/userdashboard/js/ranger_slider.js') }}"></script> <!--isotope js--> <script src="{{ asset('backend/userdashboard/js/isotope.pkgd.min.js') }}"></script> <!--venobox js--> <script src="{{ asset('backend/userdashboard/js/venobox.min.js') }}"></script> <!--classycountdown js--> <script src="{{ asset('backend/userdashboard/js/jquery.classycountdown.js') }}"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script> <!--Sweetalert js--> <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script> <script src="https://cdn.datatables.net/v/bs5/dt-2.1.7/r-3.0.3/datatables.min.js"></script> <!--main/custom js--> <script src="{{ asset('backend/userdashboard/js/main.js') }}"></script> <script> @if ($errors->any()) @foreach ($errors->all() as $error) toastr.error("{{ $error }}") @endforeach @endif </script> <!-- Dynamic Delete alart --> <script> $(document).ready(function() { $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); $('body').on('click', '.delete-item', function(event) { event.preventDefault(); let deleteUrl = $(this).attr('href'); Swal.fire({ title: 'Are you sure?', text: "You won't be able to revert this!", icon: 'warning', showCancelButton: true, confirmButtonColor: '#3085d6', cancelButtonColor: '#d33', confirmButtonText: 'Yes, delete it!' }).then((result) => { if (result.isConfirmed) { $.ajax({ type: 'DELETE', url: deleteUrl, success: function(data) { if (data.status == 'success') { Swal.fire( 'Deleted!', data.message, 'success' ) window.location.reload(); } else if (data.status == 'error') { Swal.fire( 'Cant Delete', data.message, 'error' ) } }, error: function(xhr, status, error) { console.log(error); } }) } }) }) }) </script> @stack('scripts') </body> </html>
  • kthorngrenkthorngren Posts: 21,449Questions: 26Answers: 4,975

    Chose a styling framework- I selected bootstrap 5 (Not sure if this is the right choice).

    It is if yu are using Bootstrap 5. Its difficult to tell with the names of the Bootstrap library you are loading.

    Select Packages- I chose DataTables (Not sure if this is the right choice).

    Perfect!

    Extension- I chose Responsive (Again not sure if this is the right choice but I chose since I want the data table to be responsive).

    Yes, but it doesn't look like you have enabled the use of responsive in you Datatable. I think you are defining the Datatable options on line 94 with return $this->builder(). The Responsive docs show how to enable Responsive using Javascript. However it looks like you are using Yarja Datatables. You will need to refer to their docs or support to learn how to enable Responsive. This forum doesn't have anyone who is familiar with the Yarja package.

    Kevin

  • FullstackFullstack Posts: 3Questions: 1Answers: 0

    Hi Kevin,

    Many thanks for your guidance. It seems to have worked now- I tested how the page would like like on various mobile devices using Chrome.

    These are the changes I made:


    @extends('vendor.dashboard.layouts.master') @section('content') <!-- ======= Vendor Dashboard ======= --> <section id="wsus__dashboard"> <div class="container-fluid"> @include('vendor.dashboard.layouts.sidebar') <div class="row"> <div class="col-xl-9 col-xxl-10 col-lg-9 ms-auto"> <div class="dashboard_content mt-2 mt-md-0"> <h3><i class="far fa-user"></i> Produkte</h3> <div class="create_button"> <a href="{{ route('vendor.products.create') }}" class="btn btn-primary"><i class="fas fa-plus"></i> Produkt erstellen</a> </div> <div class="wsus__dashboard_profile"> <div class="wsus__dash_pro_area"> {{ $dataTable->table() }} </div> </div> </div> </div> </div> </div> </section> @endsection @push('scripts') {{ $dataTable->scripts(attributes: ['type' => 'module']) }} <script> $(document).ready(function() { // Event handler for changing the product status $('body').on('click', '.change-status', function() { let isChecked = $(this).is(':checked'); let id = $(this).data('id'); $.ajax({ url: "{{ route('vendor.product.change-status') }}", method: 'PUT', data: { status: isChecked, id: id }, success: function(data) { toastr.success(data.message); }, error: function(xhr, status, error) { console.log(error); } }); }); }); </script> @endpush

    For the VendorProductDataTable.php I added this:


    public function html(): HtmlBuilder { return $this->builder() ->setTableId('vendorproduct-table') ->columns($this->getColumns()) ->minifiedAjax() ->orderBy(0) ->selectStyleSingle() ->pageLength(5) // Set the default number of items per page ->responsive(true) // Enable responsiveness ->buttons([ Button::make('excel'), Button::make('csv'), Button::make('pdf'), Button::make('print'), Button::make('reset'), Button::make('reload') ]); }
Sign In or Register to comment.