Hide Fields Dynamically on Backpack CRUD Edit Page Based on Switch Selection #1304
-
|
This is my code, if visible_for_new_version is 1, then visible_for_old_version gets hidden, and if visible_for_old_version is 1, then visible_for_new_version gets hidden. But now, I want it to work in such a way that when I click on edit:
protected function setupUpdateOperation() |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
To achieve the dynamic hiding of fields on the Backpack CRUD edit page based on switch selection, you can use the CrudField JavaScript API provided by Backpack. Here's how you can modify your code to implement this functionality: protected function setupUpdateOperation()
{
$this->setupCreateOperation();
CRUD::addField([
'name' => 'visible_for_new_version',
'type' => 'switch',
'label' => 'Visible for New Version',
]);
CRUD::addField([
'name' => 'visible_for_old_version',
'type' => 'switch',
'label' => 'Visible for Old Version',
]);
// Add custom JavaScript to manage field visibility
CRUD::addField([
'type' => 'custom_html',
'name' => 'my_custom_html',
'value' => '<script>
document.addEventListener("DOMContentLoaded", function() {
crud.field("visible_for_new_version").onChange(function(field) {
crud.field("visible_for_old_version").show(field.value == 0);
}).change();
crud.field("visible_for_old_version").onChange(function(field) {
crud.field("visible_for_new_version").show(field.value == 0);
}).change();
});
</script>'
]);
}This solution uses the CrudField JavaScript API to add interactivity to your form. Here's how it works:
This code will ensure that:
For more information on using the CrudField JavaScript API, you can refer to the CrudField JavaScript Library documentation. ℹ️ Please note our AI Bot does not reply to follow-ups, this will be the only AI answer in this thread. If the AI helped you find the answer, please mark it as answered or write details below so it can learn. Then close the conversation. If not, please give more information for when a human reads this. ℹ️ Relevant sources: CrudField JavaScript Library|(12) When a checkbox is checked, hide repeatable and disable all subfields |
Beta Was this translation helpful? Give feedback.
-
|
I'm glad our bot help you find the answer. I will close this issue, but please feel free to re-open or create a new one if needed. Cheers. |
Beta Was this translation helpful? Give feedback.
To achieve the dynamic hiding of fields on the Backpack CRUD edit page based on switch selection, you can use the CrudField JavaScript API provided by Backpack. Here's how you can modify your code to implement this functionality: