Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Tailwind Datatables Laravel Package

gorlabs/tailwind-datatables

Laravel package that integrates Yajra DataTables with a Tailwind CSS + Alpine.js UI. Supports server-side processing, search, pagination, sorting, buttons, and rich column customization. Publish config and optional CSS assets for easy theming.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup Steps

  1. Installation

    composer require gorlabs/tailwind-datatables
    npm install --save-dev @gorlabs/tailwind-datatables
    

    Add the package to tailwind.config.js:

    plugins: [
        require('@gorlabs/tailwind-datatables'),
    ],
    
  2. Publish Assets

    npm run dev  # or `npm run build` for production
    
  3. First Use Case Create a basic DataTable in a Blade view:

    <x-datatable :data="users" :columns="$columns">
        <x-slot name="columns">
            <x-datatable.column>ID</x-datatable.column>
            <x-datatable.column>Name</x-datatable.column>
            <x-datatable.column>Email</x-datatable.column>
        </x-slot>
    </x-datatable>
    

    Define $columns in your controller:

    $columns = [
        'id', 'name', 'email'
    ];
    
  4. Server-Side Processing Ensure your controller extends Gorlabs\TailwindDataTables\DataTablesController and overrides getDataTable():

    public function getDataTable()
    {
        return $this->datatable
            ->eloquent($this->model)
            ->make(true);
    }
    

Implementation Patterns

Core Workflows

  1. Blade Integration Use the <x-datatable> component with slots for columns, headers, and footers:

    <x-datatable :data="$users" :columns="$columns">
        <x-slot name="header">
            <h1 class="text-2xl font-bold">User Management</h1>
        </x-slot>
    </x-datatable>
    
  2. Dynamic Column Customization Customize columns with Alpine.js directives and Tailwind classes:

    <x-datatable.column>
        <span x-text="row.name" class="font-medium"></span>
        <span x-text="row.email" class="text-gray-500"></span>
    </x-datatable.column>
    
  3. Server-Side Processing Leverage Yajra’s DataTables for server-side logic:

    $this->datatable->eloquent($model)
        ->addColumn('action', function($row) {
            return '<button class="btn btn-sm btn-primary">Edit</button>';
        })
        ->rawColumns(['action']);
    
  4. Livewire/Vue Integration

    • Livewire: Use LivewireDataTable component and bind to Livewire properties.
    • Vue: Use VueDataTable with v-model for reactivity.
  5. API-Driven Tables Fetch data via API and bind to the DataTable:

    <x-datatable :data="fetchData('api/users')" :columns="$columns"></x-datatable>
    

Integration Tips

  • Tailwind Theming: Override default classes in tailwind.config.js:
    theme: {
        extend: {
            colors: {
                primary: '#3b82f6', // Customize primary color
            },
        },
    },
    
  • Alpine.js Events: Listen to DataTable events (e.g., datatable:row-selected):
    <div x-data="{ selected: null }"
         x-on:datatable:row-selected="selected = $event.detail">
    
  • Laravel Mix/Webpack: Ensure @gorlabs/tailwind-datatables is processed in your build.

Gotchas and Tips

Common Pitfalls

  1. Missing Alpine.js Ensure Alpine.js is loaded before the DataTable script:

    <script src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
    <script src="{{ asset('js/datatables.js') }}"></script>
    
  2. Server-Side Processing Mismatch Verify getDataTable() returns a valid DataTables response:

    // Avoid returning raw data; use `make(true)`:
    return $this->datatable->make(true);
    
  3. Column Binding Issues Ensure column names in Blade match those in the server-side response (case-sensitive).

  4. Tailwind Conflicts Reset default styles if Tailwind classes clash:

    <x-datatable :options="{ classes: { table: 'w-full', cell: 'py-2 px-4' } }">
    

Debugging Tips

  • Check Network Requests: Verify server-side processing endpoints return valid JSON.
  • Alpine.js Console: Use x-init to log Alpine state:
    <div x-data x-init="console.log($data)">
    
  • Yajra Debugging: Enable Yajra’s debug mode:
    $this->datatable->debug(true);
    

Extension Points

  1. Custom Components Extend the <x-datatable> component in resources/views/components/datatable.blade.php.

  2. Alpine.js Mixins Add custom logic via Alpine mixins:

    document.addEventListener('alpine:init', () => {
        Alpine.mixin({ customMethod() { /* ... */ } });
    });
    
  3. Server-Side Extensions Override DataTablesController methods (e.g., getDataTable()) for custom logic.

  4. Tailwind Plugins Extend the @gorlabs/tailwind-datatables plugin in tailwind.config.js:

    plugins: [
        require('@gorlabs/tailwind-datatables')({
            table: {
                base: 'min-w-full divide-y divide-gray-200',
            },
        }),
    ],
    

Performance Quirks

  • Lazy Loading: Defer Alpine.js initialization for large tables:
    <div x-data x-init="initDataTable()" @load.debounce="initDataTable()">
    
  • Pagination Chunking: Optimize server-side queries:
    $this->datatable->eloquent($model)->paginate(10);
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui