|  | 
|  | 1 | +--- | 
|  | 2 | +title: Actions | 
|  | 3 | +weight: 2 | 
|  | 4 | +--- | 
|  | 5 | + | 
|  | 6 | +### actions() | 
|  | 7 | + | 
|  | 8 | +Define your actions using the actions() method: | 
|  | 9 | + | 
|  | 10 | +```php | 
|  | 11 | +public function actions(): array | 
|  | 12 | +{ | 
|  | 13 | +    return [ | 
|  | 14 | +        Action::make('View Dashboard') | 
|  | 15 | +        ->setRoute('dashboard'), | 
|  | 16 | +    ]; | 
|  | 17 | +} | 
|  | 18 | +``` | 
|  | 19 | + | 
|  | 20 | +## Button Available Methods | 
|  | 21 | + | 
|  | 22 | +### setLabelAttributes | 
|  | 23 | +Set custom attributes for an Action Label.  At present it is recommended to only use this for "class" and "style" attributes to avoid conflicts. | 
|  | 24 | + | 
|  | 25 | +By default, this replaces the default classes on the Action Label, if you would like to keep them, set the default flag to true. | 
|  | 26 | + | 
|  | 27 | +```php | 
|  | 28 | +Action::make('Dashboard') | 
|  | 29 | +    ->setRoute('dashboard') | 
|  | 30 | +    ->wireNavigate() | 
|  | 31 | +    ->setLabelAttributes([ | 
|  | 32 | +        'class' => 'text-xl',  | 
|  | 33 | +        'default' => true, | 
|  | 34 | +    ]), | 
|  | 35 | +``` | 
|  | 36 | + | 
|  | 37 | +### setActionAttributes | 
|  | 38 | + | 
|  | 39 | +setActionAttributes is used to pass any attributes that you wish to implement on the "button" element for the action button.  By default it will merge with the default classes.  | 
|  | 40 | + | 
|  | 41 | +You can set "default-styling" and "default-colors" to false to replace, rather than over-ride either default-styling or default-colors. | 
|  | 42 | +```php | 
|  | 43 | +public function actions(): array | 
|  | 44 | +{ | 
|  | 45 | +    return [ | 
|  | 46 | +        Action::make('View Dashboard') | 
|  | 47 | +            ->setActionAttributes([ | 
|  | 48 | +                'class' => 'dark:bg-blue-500 dark:text-white dark:border-blue-600 dark:hover:border-blue-900 dark:hover:bg-blue-800',  | 
|  | 49 | +                'default-colors' => false, // Will over-ride the default colors | 
|  | 50 | +                'default-styling' => true // Will use the default styling | 
|  | 51 | +            ]), | 
|  | 52 | +    ]; | 
|  | 53 | +} | 
|  | 54 | +``` | 
|  | 55 | + | 
|  | 56 | +### setIcon | 
|  | 57 | + | 
|  | 58 | +setIcon is used to set an icon for the action button | 
|  | 59 | + | 
|  | 60 | +```php | 
|  | 61 | +public function actions(): array | 
|  | 62 | +{ | 
|  | 63 | +    return [ | 
|  | 64 | +        Action::make('Edit Item') | 
|  | 65 | +        ->setIcon("fas fa-edit"), | 
|  | 66 | +    ]; | 
|  | 67 | +} | 
|  | 68 | +``` | 
|  | 69 | + | 
|  | 70 | +### setIconAttributes | 
|  | 71 | + | 
|  | 72 | +setIconAttributes is used to set any additional attributes for the Icon for the action button | 
|  | 73 | +```php | 
|  | 74 | +public function actions(): array | 
|  | 75 | +{ | 
|  | 76 | +    return [ | 
|  | 77 | +        Action::make('Edit Item') | 
|  | 78 | +        ->setIcon("fas fa-edit") | 
|  | 79 | +        ->setIconAttributes(['class' => 'font-4xl text-4xl']), | 
|  | 80 | +    ]; | 
|  | 81 | +} | 
|  | 82 | +``` | 
|  | 83 | + | 
|  | 84 | +### setIconLeft | 
|  | 85 | + | 
|  | 86 | +setIconLeft is used to prepend the Icon to the Text (Non-Default Behaviour) | 
|  | 87 | + | 
|  | 88 | +```php | 
|  | 89 | +public function actions(): array | 
|  | 90 | +{ | 
|  | 91 | +    return [ | 
|  | 92 | +        Action::make('Edit Item') | 
|  | 93 | +        ->setIcon("fas fa-edit") | 
|  | 94 | +        ->setIconAttributes(['class' => 'font-4xl text-4xl']) | 
|  | 95 | +        ->setIconLeft(), | 
|  | 96 | +    ]; | 
|  | 97 | +} | 
|  | 98 | +``` | 
|  | 99 | + | 
|  | 100 | +### setIconRight | 
|  | 101 | + | 
|  | 102 | +setIconRight is used to append the Icon to the Text (Default Behaviour) | 
|  | 103 | + | 
|  | 104 | +```php | 
|  | 105 | +public function actions(): array | 
|  | 106 | +{ | 
|  | 107 | +    return [ | 
|  | 108 | +        Action::make('Edit Item') | 
|  | 109 | +        ->setIcon("fas fa-edit") | 
|  | 110 | +        ->setIconAttributes(['class' => 'font-4xl text-4xl']) | 
|  | 111 | +        ->setIconRight(), | 
|  | 112 | +    ]; | 
|  | 113 | +} | 
|  | 114 | +``` | 
|  | 115 | + | 
|  | 116 | +### setRoute | 
|  | 117 | + | 
|  | 118 | +Used for non-wireable butons, to set the route that the action button should take the user to upon clicking. | 
|  | 119 | +```php | 
|  | 120 | +public function actions(): array | 
|  | 121 | +{ | 
|  | 122 | +    return [ | 
|  | 123 | +        Action::make('Dashboard') | 
|  | 124 | +        ->setRoute('dashboard') | 
|  | 125 | +    ]; | 
|  | 126 | +} | 
|  | 127 | +``` | 
|  | 128 | + | 
|  | 129 | +### wireNavigate | 
|  | 130 | + | 
|  | 131 | +Used in conjunction with setRoute - makes the link "wire:navigate" rather than default behaviour | 
|  | 132 | +```php | 
|  | 133 | +public function actions(): array | 
|  | 134 | +{ | 
|  | 135 | +    return [ | 
|  | 136 | +        Action::make('Dashboard') | 
|  | 137 | +        ->setRoute('dashboard') | 
|  | 138 | +        ->wireNavigate() | 
|  | 139 | +    ]; | 
|  | 140 | +} | 
|  | 141 | +``` | 
|  | 142 | + | 
|  | 143 | +### setWireAction | 
|  | 144 | +```php | 
|  | 145 | +public function actions(): array | 
|  | 146 | +{ | 
|  | 147 | +    return [ | 
|  | 148 | +        Action::make('Create 2') | 
|  | 149 | +        ->setWireAction("wire:click") | 
|  | 150 | +    ]; | 
|  | 151 | +} | 
|  | 152 | +``` | 
|  | 153 | + | 
|  | 154 | +### setWireActionParams | 
|  | 155 | +Specify the action & parameters to pass to the wire:click method | 
|  | 156 | + | 
|  | 157 | +```php | 
|  | 158 | +public function actions(): array | 
|  | 159 | +{ | 
|  | 160 | +    return [ | 
|  | 161 | +        Action::make('Create 2') | 
|  | 162 | +        ->setWireAction("wire:click") | 
|  | 163 | +        ->setWireActionParams(['id' => 'test']), | 
|  | 164 | +    ]; | 
|  | 165 | +} | 
|  | 166 | +``` | 
|  | 167 | + | 
|  | 168 | +### setWireActionDispatchParams | 
|  | 169 | + | 
|  | 170 | +Use $dispatch rather than a typical wire:click action | 
|  | 171 | + | 
|  | 172 | +```php | 
|  | 173 | +public function actions(): array | 
|  | 174 | +{ | 
|  | 175 | +    return [ | 
|  | 176 | +        Action::make('Create 2') | 
|  | 177 | +        ->setWireAction("wire:click") | 
|  | 178 | +        ->setWireActionDispatchParams("'openModal', { component: 'test-modal' }"), | 
|  | 179 | +    ]; | 
|  | 180 | +} | 
|  | 181 | +``` | 
|  | 182 | + | 
|  | 183 | +### setView | 
|  | 184 | + | 
|  | 185 | +This is used to set a Custom View for the Button | 
|  | 186 | + | 
|  | 187 | +```php | 
|  | 188 | +public function actions(): array | 
|  | 189 | +{ | 
|  | 190 | +    return [ | 
|  | 191 | +        Action::make('Edit Item') | 
|  | 192 | +        ->setView("buttons.edit"), | 
|  | 193 | +    ]; | 
|  | 194 | +} | 
|  | 195 | +``` | 
|  | 196 | + | 
|  | 197 | + | 
|  | 198 | +## Extending | 
|  | 199 | + | 
|  | 200 | +You can extend the Base Action class which can be a useful timesaver, when you wish to re-use the same look/feel of an Action, but wish to set a different route (for example). | 
|  | 201 | +You can set any defaults in the __construct method, ensuring that the parent constructor is called first! | 
|  | 202 | + | 
|  | 203 | +```php | 
|  | 204 | +use Rappasoft\LaravelLivewireTables\Views\Action; | 
|  | 205 | + | 
|  | 206 | +class EditAction extends Action | 
|  | 207 | +{ | 
|  | 208 | +    public function __construct(?string $label = null) | 
|  | 209 | +    { | 
|  | 210 | +        parent::__construct($label); | 
|  | 211 | +        $this | 
|  | 212 | +            ->setActionAttributes([ | 
|  | 213 | +                'class' => 'dark:bg-blue-500 dark:text-white dark:border-blue-600 dark:hover:border-blue-900 dark:hover:bg-blue-800',  | 
|  | 214 | +                'default-colors' => false,  | 
|  | 215 | +                'default-styling' => true | 
|  | 216 | +            ]) | 
|  | 217 | +            ->setIcon("fas fa-edit") | 
|  | 218 | +            ->setIconAttributes([ | 
|  | 219 | +                'class' => 'font-4xl text-4xl' | 
|  | 220 | +            ]); | 
|  | 221 | +    } | 
|  | 222 | +} | 
|  | 223 | +``` | 
|  | 224 | + | 
|  | 225 | +You may define a Custom View to be used via either the setView method, or by defining the view directly in your class. | 
|  | 226 | +```php | 
|  | 227 | +    protected string $view = 'buttons.edit-action'; | 
|  | 228 | +``` | 
|  | 229 | + | 
0 commit comments