Selection bar

"3 selected — Archive, Delete, Clear selection": the bulk actions for the rows checked in a table, shown while any are.

Selection bar

Needs Alpine and the package's script. See getting started. With JavaScript off it is a row of your action buttons, always shown, and the table a column of checkboxes in a form: pressing an action posts the checked rows.

Use it

Give the table selectable and each row a value; put the bar and the table in one form, and make each action a submit button with its own formaction:

<form method="POST">
    @csrf
    <x-ds::selection-bar for="invoices" :total="$invoices->total()">
        <x-ds::button type="submit" formaction="{{ route('invoices.archive') }}" variant="secondary" size="sm">Archive</x-ds::button>
        <x-ds::button type="submit" formaction="{{ route('invoices.destroy') }}" variant="danger" size="sm">Delete</x-ds::button>
    </x-ds::selection-bar>

    <x-ds::table id="invoices" selectable :columns="['Invoice', 'Customer', ['label' => 'Total', 'align' => 'right']]">
        @foreach ($invoices as $invoice)
            <x-ds::table-row :value="$invoice->id" :selectLabel="'Select invoice '.$invoice->number">
                <x-ds::table-cell>{{ $invoice->number }}</x-ds::table-cell>
                <x-ds::table-cell>{{ $invoice->customer }}</x-ds::table-cell>
                <x-ds::table-cell align="right">{{ $invoice->total }}</x-ds::table-cell>
            </x-ds::table-row>
        @endforeach
    </x-ds::table>
</form>

{{ $invoices->links() }}

The controller reads $request->input('selected', []), and all when the reader chose every row across pages.

Props

PropTypeDefaultWhat it does
forstring—The id of the table (or anything around it). Left out, the form the bar is in.
countint0How many are selected, from the server: shown before the script runs.
countLabelstring:count selected
labelstringBulk actionsThe region's name.
clearLabelstringClear selection
totalint—How many rows match across every page. Given, "Select all :total" is offered once the whole page is checked.
allLabelstringSelect all :total
allSelectedLabelstringAll :total selected
allNamestringallThe hidden field posted as 1 once every row across pages is chosen.
formstring—The form's id, when the bar sits outside it (for the hidden field).
positioninline floatinginlineinline: a row above the table. floating: a card at the foot of the window.

The slot holds the actions: ordinary buttons, submit buttons with formaction and name, or wire:clicks.

The table's side

PropOnDefaultWhat it does
selectabletablefalseRows given a value draw a checkbox; with columns, the header gets a select-all box.
selectNametableselected[]The field every box posts.
selectModeltable—An Alpine expression every box is x-model'd to: picked, or $wire.selected in Livewire.
selectAllLabeltableSelect all rows
valuetable-row—What its box posts. A row without one draws no box (give a full-width row's colspan the extra column).
selectedtable-rowfalseIts box starts checked.
selectLabeltable-rowSelect {value}The box's name: write the thing, "Select invoice 42".

With the script, the select-all box goes mixed when some rows are checked and full when all are, and a press on it checks or clears them all; Shift with a press checks or clears the run of rows since the last one pressed. Every box it changes fires a native change, so x-model and wire:model follow. A checked row is tinted.

With Livewire

Bind the boxes with selectModel, and let the server count:

<x-ds::selection-bar for="invoices" :count="count($selected)" :total="$invoices->total()"
                     x-on:select-all="$wire.selectAllMatching()">
    <x-ds::button wire:click="archive" variant="secondary" size="sm">Archive</x-ds::button>
</x-ds::selection-bar>

<x-ds::table id="invoices" selectable selectModel="$wire.selected" :columns="$columns">
    @foreach ($invoices as $invoice)
        <x-ds::table-row :value="$invoice->id" wire:key="invoice-{{ $invoice->id }}">…</x-ds::table-row>
    @endforeach
</x-ds::table>

select-all is dispatched from the bar, with { total }, when the reader chooses every row across pages.

Accessibility

  • A region named by label, holding ordinary buttons reached by Tab. Not role="toolbar", which would promise arrow keys between them.
  • The count is said politely a moment after the reader stops pressing, so a Shift-press across twenty rows is one announcement, not twenty.
  • Clear selection hides the bar it is in, so it gives focus to the table's select-all box rather than dropping it.
  • Every row's box is named by selectLabel. The select-all box says mixed (indeterminate) when some rows are checked.

Don't

  • Don't leave out selectLabel. "Select 42" is better than nothing; "Select invoice 42" is what a screen reader moving down the column needs.
  • Don't make a destructive action the only thing in the bar without a confirm. Use a confirm button or confirm popover for Delete.
  • Don't combine selectable with tree. It throws: the tree's toggle takes the first cell.

More in specs/selection-bar.md.


From the package repo: edit this page on GitHub.

Was this page helpful?