Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Создание меню


Recommended Posts

Хочу создать меню такого типа 
image.png.8942e0d50cffcc7f97cdfc0da08ece5c.png

 

чтобы при выборе родительской категории появлялись справа дочерние категории.

 

Начал писать код для файла меню:
 

<div class="category-menu">
            <div class="parent-categories">
                <ul>
                    {% for category in categories %}
                        <li>
                            <a href="#" class="category-link" data-category-id="{{ category.category_id }}">{{ category.name }}</a>
                        </li>
                    {% endfor %}
                </ul>
            </div>
            <div class="child-categories">
                {% for category in categories %}
                    {% if category.children %}
                        <ul class="subcategory-list" id="{{ category.category_id }}" data-parent-id="{{ category.category_id}}">
                            {% for child in category.children %}
                                <li class="subcategory-item">
                                    <a href="{{ child.href }}">{{ child.name }}</a>
                                </li>
                            {% endfor %}
                        </ul>
                    {% endif %}
                {% endfor %}
            </div>
        </div>
 
        <script>
            const categoryLinks = document.querySelectorAll('.category-link');
 
            categoryLinks.forEach(link => {
            link.addEventListener('click', event => {
            event.preventDefault();
 
            const categoryId = link.dataset.categoryId;
            const subcategories = document.querySelectorAll(`.subcategory-item[data-parent-id="${categoryId}"]`);
 
            subcategories.forEach(subcategory => {
            subcategory.classList.toggle('hidden');
            });
            });
            });
        </script>
 
 
---
 
Но строчка category.category_id выводится в id пустой, и не знаю куда лезть смотреть и где она должна прописыватся. 
Link to comment
Share on other sites


  • 3 weeks later...
01.05.2023 в 12:44, WarStyle сказал:

А есть ли смысл изобретать велосипед?

Есть, если велосипед не нравится.

Link to comment
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.