btn_active() table()

An Active / Inactive toggle in each row, showing the state of the row field named after the button (active).

table()->btn_active($name = '', $link = '', $extra = [])

Options

OptionMeaning
$nameButton name and the field it reads (lower case); default Active.
$linkDefault url('/@object/active/%d').
shortA small icon button (html()->btn_active()).
rendererswitch: a Bootstrap 5 form switch (Bootstrap 3 keeps the button).
disabledShows the state without a link.
no_ajaxNo change_active class (the admin script toggles it in place).
postfalse: stays a plain link when the table POSTs its state-changing buttons.

Example

Name Actions
Spring sale Active Active
Old banner Disabled Disabled

Code

$rows = [
    1 => ['id' => 1, 'name' => 'Spring sale', 'active' => 1],
    2 => ['id' => 2, 'name' => 'Old banner', 'active' => 0],
];
return table($rows, ['no_pages' => 1])
    ->text('name')
    ->btn_active(['link' => '#toggle-%d', 'no_ajax' => 1])
    ->btn_active(['link' => '#toggle-%d', 'short' => 1])
    ->btn_active(['link' => '#toggle-%d', 'renderer' => 'switch']);

Notes

  • The labels come from the pair_active data handler.
  • Changing state is a write: in the admin with the CSRF guard on it POSTs with the token (data-yf-post).

Bootstrap 3 and 5

renderer => switch is a form-check form-switch under Bootstrap 5 only.

See also: table/yes_no, form/active_box, html/btn_active