vdvcoder's avatar

Datatables problem

Hello all!

I have a problem with loading datatables. My table has alot of if statements but when i want to add datatables to that table. I doesn't work like it should be.

This is my table:

 <table class="table table-striped table-bordered mb-0" id="datatables">
                        <thead>
                        <tr>
                            @if (isset($table_items[0]))
                                @foreach ($table_items[0] as $key => $value)
                                    @if ($key !== "_item")
                                        <th scope="col">{{ $key }}</th>
                                    @endif
                                @endforeach
                                @if ($table_items[0]["_item"]->isTranslatable && $showOnOffSwitch)
                                    <th width="{{ count(website('languages')) * 42 }}px"></th>
                                @endif
                                @if (auth()->user()->can("{$module->slug}.create") && $canCloneItem)
                                    <th width="42px"></th>
                                @endif
                                @if (auth()->user()->can("{$module->slug}.update") && $canEditItem)
                                    <th width="42px"></th>
                                @endif
                                @if (auth()->user()->can("{$module->slug}.delete") && $canDeleteItem)
                                    <th width="42px"></th>
                                @endif
                            @else
                                <th scope="col" class="border-bottom text-center bg-light">No data found.</th>
                            @endif
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($table_items as $table_item)
                            <tr id="{{ $table_item["_item"]->id }}">
                                @foreach ($table_item as $key => $value)
                                    @if ($key !== "_item")
                                        <td style="vertical-align: middle">{!! $value !!}</td>
                                    @endif
                                @endforeach
                                @if ($table_items[0]["_item"]->isTranslatable && $showOnOffSwitch)
                                    <td style="vertical-align: middle">
                                        @if (count(website('languages')) === 1)
                                            <input type="checkbox" value="1" data-id="{{ $table_item["_item"]->id }}"
                                                   data-plugin="switchery" data-size="small"
                                                   data-color="#23BF08" data-secondary-color="#cc3300"
                                                   style="display: none;" data-switchery="true"
                                                {{ $table_item["_item"]->isOnline(website('languages')[0]) ? "checked" : "" }}>
                                        @else
                                            @foreach (website('languages') as $lang)
                                                <span
                                                    class="{{ $table_item["_item"]->isOnline($lang) ? "text-success" : "text-danger" }}">{{ strtoupper($lang) }}
                                                </span>
                                            @endforeach
                                        @endif
                                    </td>
                                @endif
                                @if (auth()->user()->can("{$module->slug}.create") && $canCloneItem)
                                    <td>
                                        <form
                                            action="{{ route('cms.{module}.duplicate', ["module" => $module->slug, "id" => $table_item["_item"]->id]) }}"
                                            method="POST">
                                            @csrf
                                            <button type="submit"
                                                    class="btn btn-warning text-white waves-effect waves-light btn-sm">
                                                <i class="mdi mdi-content-copy"></i>
                                            </button>
                                        </form>
                                    </td>
                                @endif
                                @if (auth()->user()->can("{$module->slug}.update") && $canEditItem)
                                    <td>
                                        <a href="{{ route('cms.{module}.edit', ["module" => $module->slug, "id" => $table_item["_item"]->id]) }}"
                                           class="btn btn-blue text-white waves-effect waves-light btn-sm">
                                            <i class="mdi {{ $showPage ? "mdi-arrow-right" : "mdi-pencil" }}"></i>
                                        </a>
                                    </td>
                                @endif
                                @if (auth()->user()->can("{$module->slug}.delete") && $canDeleteItem)
                                    <td>
                                        <form
                                            action="{{ route('cms.{module}.destroy', ["module" => $module->slug, "id" => $table_item["_item"]->id]) }}"
                                            method="POST">
                                            @csrf @method('DELETE')
                                            <button type="submit"
                                                    class="btn btn-danger text-white waves-effect waves-light btn-sm"
                                                    form-confirm>
                                                <i class="mdi mdi-trash-can-outline"></i>
                                            </button>
                                        </form>
                                    </td>
                                @endif
                            </tr>
                        @endforeach
                        </tbody>
                    </table>

I added the datatables css and js to the master. It shows the 'show entries' and search bar. but the pagination don't work and if i want to search it only search in the first 10 that is visible. Also i can't show more entries like 25. it doesn't do anything.

0 likes
1 reply
vdvcoder's avatar
vdvcoder
OP
Best Answer
Level 13

Ok i found it. There was a link 'show all' if i clicked that. the datatables work correctly. So i added the ?show=all to the link of the page. and now it works like it should be.

Thank you

Please or to participate in this conversation.