Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure remove button is functional for newly added menu items #113

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion admin/pages/menus.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@
$post_menu_blank = $_REQUEST['menu_blank'];
$post_menu_color = $_REQUEST['menu_color'];
if (count($post_menu) != count($post_menu_link)) {
echo 'Menu count is not equal menu links. Something went wrong when sending form.';
echo 'Menu count is not equal to menu links. Something went wrong when sending form.';
return;
}

// Process removed menus
if (isset($_POST['removed_menus'])) {
foreach ($_POST['removed_menus'] as $removed_menu_name) {
$db->query('DELETE FROM `' . TABLE_PREFIX . 'menu` WHERE `name` = ' . $db->quote($removed_menu_name) . ' AND `template` = ' . $db->quote($template));
}
}

$db->query('DELETE FROM `' . TABLE_PREFIX . 'menu` WHERE `template` = ' . $db->quote($template));
foreach ($post_menu as $category => $menus) {
foreach ($menus as $i => $menu) {
Expand Down Expand Up @@ -120,6 +127,29 @@
'menus' => $menus,
'last_id' => $last_id
));

// Adding JavaScript to handle item removal
echo <<<EOT
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', (event) => {
// Add click event for all remove buttons
document.querySelectorAll('a.btn.btn-danger').forEach(button => {
button.addEventListener('click', function(e) {
e.preventDefault();
let li = this.closest('li');
li.remove(); // Remove the item from the list

// Optional: Add a hidden field to the form to indicate that this item was removed
let input = document.createElement('input');
input.type = 'hidden';
input.name = 'removed_menus[]';
input.value = li.querySelector('input[name^="menu["]').value; // Capture the menu name value
document.getElementById('menus-form').appendChild(input);
});
});
});
</script>
EOT;
?>

<?php
Expand All @@ -136,3 +166,4 @@
'templates' => $templates
));
}
?>
Loading