How can confirm or cancel the action in toggle

hey guys, i have this code
Tables\Columns\ToggleColumn::make('status')
->label('Calculo cerrado')
->disabled(fn($record) => $record->status)
->action(function ($record, $column) {
$newState = !$record->status;
Notification::make()
->title('Confirmación requerida')
->body('¿Estás seguro de que deseas cambiar el estado?')
->success()
->actions([
Notification\Actions\ButtonAction::make('Confirmar')
->label('Sí')
->action(function () use ($record, $newState) {
$record->update(['status' => $newState]);
Notification::make()
->title('Estado actualizado')
->success()
->send();
}),
Notification\Actions\ButtonAction::make('Cancelar')
->label('No')
])
->send();
})
Tables\Columns\ToggleColumn::make('status')
->label('Calculo cerrado')
->disabled(fn($record) => $record->status)
->action(function ($record, $column) {
$newState = !$record->status;
Notification::make()
->title('Confirmación requerida')
->body('¿Estás seguro de que deseas cambiar el estado?')
->success()
->actions([
Notification\Actions\ButtonAction::make('Confirmar')
->label('Sí')
->action(function () use ($record, $newState) {
$record->update(['status' => $newState]);
Notification::make()
->title('Estado actualizado')
->success()
->send();
}),
Notification\Actions\ButtonAction::make('Cancelar')
->label('No')
])
->send();
})
What I want is that when I click the toggle to go to true it asks me if I really want to confirm the action or the change of state from false to true, if I click yes to make the state change, if I click no that keep the same state in false... I'm trying this code but it doesn't work for me, is there something in filament that allows me to do this? Thanks friends!
No description
4 Replies
TranceCode
TranceCodeOP6mo ago
any idea bout this friends? i try with this code but is not working!
Tables\Columns\ToggleColumn::make('status')
->label('Calculo cerrado')
//->disabled(fn($record) => $record->status)
->action(fn($record, $column) => Action::make('confirmToggle')
->modalHeading('Confirmación requerida')
->modalSubheading('¿Estás seguro de que deseas cambiar el estado?')
->modalButton('Confirmar')
->action(function () use ($record) {
$record->update(['status' => $record->status]);
Notification::make()
->title('Estado actualizado')
->success()
->send();
})
->cancelButton('Cancelar')
->color('primary')
->openModal()
),
Tables\Columns\ToggleColumn::make('status')
->label('Calculo cerrado')
//->disabled(fn($record) => $record->status)
->action(fn($record, $column) => Action::make('confirmToggle')
->modalHeading('Confirmación requerida')
->modalSubheading('¿Estás seguro de que deseas cambiar el estado?')
->modalButton('Confirmar')
->action(function () use ($record) {
$record->update(['status' => $record->status]);
Notification::make()
->title('Estado actualizado')
->success()
->send();
})
->cancelButton('Cancelar')
->color('primary')
->openModal()
),
i found this in the documentation, but can't understand how can add a confirm or cancel modal?
ToggleColumn::make()
->beforeStateUpdated(function ($record, $state) {
// Runs before the state is saved to the database.
})
->afterStateUpdated(function ($record, $state) {
// Runs after the state is saved to the database.
})
ToggleColumn::make()
->beforeStateUpdated(function ($record, $state) {
// Runs before the state is saved to the database.
})
->afterStateUpdated(function ($record, $state) {
// Runs after the state is saved to the database.
})
TranceCode
TranceCodeOP6mo ago
finally i found a solution for my problem... i use an Action for this case that i need a modal with confirmation!
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('mescobro')
->label('Mes de cobro')
->sortable()
->searchable(),
Tables\Columns\TextColumn::make('fechavencimiento')
->label('Fecha de vencimiento')
->sortable()
->searchable(),
Tables\Columns\TextColumn::make('porcentajefondoreserva')
->label('Porcentaje de reserva')
->sortable()
->searchable(),
Tables\Columns\ToggleColumn::make('status')
->label('Calculo cerrado')
->disabled()
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make()
->disabled(fn ($record) => $record->status == true),
Tables\Actions\Action::make('cerrarCalculo')
->label('Cerrar Cálculo')
->icon('heroicon-o-lock-closed')
->requiresConfirmation()
->modalHeading('Confirmar cierre de cálculo')
->modalSubheading('¿Estás seguro de que deseas cerrar este cálculo? Esta acción no se puede deshacer.')
->modalButton('Sí, cerrar')
->action(function ($record, $data) {
$record->status = true;
$record->save();
})
->disabled(fn($record) => $record->status == true),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
//Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('mescobro')
->label('Mes de cobro')
->sortable()
->searchable(),
Tables\Columns\TextColumn::make('fechavencimiento')
->label('Fecha de vencimiento')
->sortable()
->searchable(),
Tables\Columns\TextColumn::make('porcentajefondoreserva')
->label('Porcentaje de reserva')
->sortable()
->searchable(),
Tables\Columns\ToggleColumn::make('status')
->label('Calculo cerrado')
->disabled()
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make()
->disabled(fn ($record) => $record->status == true),
Tables\Actions\Action::make('cerrarCalculo')
->label('Cerrar Cálculo')
->icon('heroicon-o-lock-closed')
->requiresConfirmation()
->modalHeading('Confirmar cierre de cálculo')
->modalSubheading('¿Estás seguro de que deseas cerrar este cálculo? Esta acción no se puede deshacer.')
->modalButton('Sí, cerrar')
->action(function ($record, $data) {
$record->status = true;
$record->save();
})
->disabled(fn($record) => $record->status == true),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
//Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
after i close the action i disable action edit and update the toggle to true and all the actions is disable now 😎 💪 🍻
No description
No description
No description
LeandroFerreira
LeandroFerreira6mo ago
maybe this can help you, if you don't want to add a row action https://discord.com/channels/883083792112300104/1247909942317547601/1250347583277105242
TranceCode
TranceCodeOP6mo ago
Thank you bro, but the row action it's working perfect for me... thank you so much
Want results from more Discord servers?
Add your server