Remove the "href" from Zend2 navigation functionality

When using Zend2 navigation from an array passed in Zend\Navigation\Navigation, everything works smoothly if child items are opened on parent item mouse hover. However, undesirable actions occur when they are opened on mouse click, as the user is unable to directly access the child page. Instead, the first page from the parent menu item is opened.

<nav>
    <ul class="sidebar">
    <li>
        <a class="item-1" href="parent-page1">Parent page1</a>
        <ul>
            <li>
                <a class="item-1" href="child-page11">Child page11</a>
            </li>
            <li>
                <a class="item-1" href="child-page12">Child page12</a>
            </li>
        </ul>
    </li>

    <li>
        <a class="item-1" href="parent-page2">Parent page2</a>
        <ul>
            <li>
                <a class="item-1" href="child-page21">Child page21</a>
            </li>
            <li>
                <a class="item-1" href="child-page22">Child page22</a>
            </li>
        </ul>
    </li>

To solve this issue, it would be helpful to have an option to remove the "href" attribute from the parent item completely. Unfortunately, I could not find such an option in the Zend2 documentation or examples. I believe there must be a way to handle menus with clickable parent items, as they are commonly used in popular admin panels like AdminLTE.

Currently, my workaround involves adding 'class' => 'not-active' to each parent item in the navigation array and then using JavaScript in the application layout to remove the "href" attribute:

window.onload = function () {
    x = document.getElementsByClassName("not-active");
    for(var i=0,j=x.length; i<j; i++){
      x[i].removeAttribute("href");
    };
}

EDIT

I finally found a solution by adding a new route in module.config.php:

        'empty' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array('route' => '#'),
        ),

Then, I utilized this route in the navigation array:

         array(
            'label' => 'Parent page1', 
            'route' => 'empty',
            'controller' => 'admins',
            'resource'  => 'Admin\Controller\Admins',
            'privilege' => 'index',
            'pages' => array(
                array(
                    'label' => 'Child page11',
                    'route' => 'admin/default',
                    'controller' => 'admins',
                    'resource'  => 'Admin\Controller\Modules',
                    'privilege' => 'index',
                ),
                array(
                    'label' => 'Child page12',
                    'route' => 'admin/default',
                    'controller' => 'modules',
                    'resource'  => 'Admin\Controller\Modules',
                    'privilege' => 'index',
                ),
             ),
         ),

EDIT 2

Unfortunately, the solution does not work when the site is located in a subpath.

For example,

<a href="#">Parent 1</a>
shows up from example.com,

while

<a href="subpath/#">Parent 1</a>
appears from example.com/subpath/.

As a result, I am back to using post-processing in JavaScript :(

Answer №1

To make the parent link inactive, just include # as the href attribute.

<a href="#">Parent Link</a>

By clicking on it, the browser will not reload or take you to a different page - you will remain on the current site.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Setting up Babel to effectively function across multiple directories

I've been utilizing Babel for some time now and it effectively transforms my ES Next code to the dist directory using this command: babel src --out-dir dist --source-maps --copy-files Up until now, all my JavaScript code has been stored in the src fo ...

assigning attributes to web addresses

Is there a way to set a style property for webpages by targeting addresses that contain /index.php/projecten/ instead of specifying each complete address in the code? Currently, I am using the following code: <ul class="subnavlist" style="display: &l ...

Dealing with click events on layers with z-index positioning

In the map application I am developing, I have implemented 2 z-index layers. However, a problem arises when attempting to zoom in by clicking on these layers. The click handler is located on the lower z-index layer and I do not want it to execute when a co ...

Increase the size of the box-shadow so that it extends beyond the boundaries

Can we create a wider box-shadow in CSS than the actual HTML element it is being applied to, while maintaining the same height as the element? Increasing the spread of the shadow typically increases the height as well. In the provided code snippet, the max ...

Having trouble with accessing PHP data through jQuery's AJAX response

Having just started exploring AJAX, I'm facing an issue with a simple login script using jQuery 1.6.4. The AJAX function sends the email address and password to 'login.php' upon clicking a button. Everything seems to be working smoothly unti ...

Steps for automatically adding a new user to the AddThis service for configuring analytics services

As I work on creating a Backoffice for my website, I am looking to provide a mobile version for all users uniformly. To enhance user experience, I plan to introduce a "Report" tab in the back office interface. This tab will display analytics information g ...

Configuring Axios header in Java backend based on the value stored in the express session

I am currently working on a frontend app using node.js express for server-side rendering. This app calls java backend endpoints and I use Axios to make the requests. A specific header named "agent-id" needs to be set in every request that is sent from expr ...

"Troubleshooting: State array in ReactJS/NextJS not rendering correctly following setState

I am facing challenges with rendering my objects using .map() within React / NextJS. Within my code, I have a function where I retrieve images from Firebase Cloud Storage as shown below: getImages = () => { let firebase = loadFirebase() ...

Display or conceal a field depending on the user's input

I need to display a checkbox only when the firstname matches abc or if the email is [email protected]. var x = abc; //will be dynamic var y = abc @gmail.com jQuery("#firstname").on('change', (function(avalue) { return function(e) { ...

Attempting to store an array of JSON objects in the state, and then utilizing the `map` method to render

I'm just starting out with React. Currently, I'm attempting to store an array of JSON objects in the state and then map those items into the component render. I'm feeling a bit lost on how to debug this as there are no console errors showi ...

Tallying the Number of Accordion Toggle Clicks

Let me present a scenario where I have some accordions and would like to keep track of how many times the user expands each one. Could you guide me on how to implement this particular feature? Appreciate your support, Kevin ...

What is the best way to initiate a change event using JavaScript?

I am attempting to enable my 4th checkbox automatically when there is a new value in the textbox (myinput). If the textbox is empty, I want to disable it. Currently, you have to tab out before the change event is triggered. Also, how can I check for an e ...

Calculating the Bounding Box of SVG Group Elements

Today I encountered a puzzling scenario involving bounding box calculations, and it seems that I have yet to fully understand the situation. To clarify, a bounding box is described as the smallest box in which an untransformed element can fit within. I h ...

Angular Material Drop Down menu with UI-Router Integration

When the user clicks on the person icon, it triggers a form (form.html) using the ui-router logic defined in app.js. There are two types of dropdown boxes - one using the select tag and the other using the md-select tag. Everything works fine until I click ...

Vue.js and TypeScript combination may result in a 'null' value when using file input

I am attempting to detect an event once a file has been uploaded using a file input. Here is the JavaScript code: fileSelected(e: Event) { if ((<HTMLInputElement>e.target).files !== null && (<HTMLInputElement>e.target).files[0] !== null) { ...

I'm having an issue where whenever I click on a different page, I keep getting redirected back to the first page

Upon conducting research, I discovered that by implementing the refined code below, I was able to resolve my issue (my other html was also corrected using this solution) setTimeout(function() { datatable_FTP.ajax.reload(null, false); }, 30000); Although I ...

Live search bar feature with jQuery更新

I am currently working on creating a dynamic search bar that updates a list of items from the database based on the input value. Below is the code I have developed for this search bar: $(document).ready(function(){ $('#search').keyup(function ...

Is it possible to transform div containers into unique shapes?

I'm working on a design where I want to have two divs that resemble teeth, one on the top half of the page and the other on the bottom half. The concept is to make these mouth piece divs open and close as you scroll with the mouse, revealing the conte ...

Click on a link to navigate to a new page using the useNavigate hook in

I have successfully implemented navigation using the useNavigate hook, but I am wondering if there is a way to open the URL in a new tab instead of the current one. Is this achievable? Here's my code: import { useNavigate } from "react-router-do ...

When a tooltip inside a button is clicked, the hover effect is passing through to the surrounding parent element

I am facing an issue with a nested tooltip within a button. The problem arises when I try to click on the 'read more' link inside the tooltip popup, intending to go to an article. However, instead of redirecting me to the article, clicking on the ...