creativetimofficial / material-dashboard

Material Dashboard - Open Source Bootstrap 5 Material Design Admin
https://www.creative-tim.com/product/material-dashboard
MIT License
10.99k stars 4.21k forks source link

Problems with the search field and menu elements on mobile #42

Closed juliocast closed 6 years ago

juliocast commented 6 years ago

Hello CreativeTim! First I want to thank you for this amazing dashboard.

I'm having problems with the search field and some menu items when using mobile screens. I'm using this Dashboard as the UI for an Electron App, and I gave an ID to the search field. When the page loads, I'm using a JQuery Mask plugin to mask the field and I'm preventing the submit event and instead calling a function.

Here's the code: image

Everything seems to run well when I load my app in desktop view, but when it changes to mobile view, something seems to break. The mask doesn't let the user input anything different from 3 letters and 4 numbers (SSS0000). It seems that after any changes are made to the form, my code doesn't recognizes the element by its ID.

I hope the following video can clarify the issue I'm running into: 2017-10-19_10-51-54 In the video I inspected the element just to show that the ID is there and towards the end, when I hit enter/submit my code doesn't recognizes the event anymore so it reacts as a default submit.

I'm sorry I'm no Javascript master, this may be a simple issue but could anyone help me?

Thank you very much, love you guys!

alexandru-paduraru commented 6 years ago

@juliocast thank you for pointing that out. It seems the issue is coming from how we handle the sidebar on mobile. We basically move anything out of the navbar and add it to the sidebar for mobile. You can see the code in material-dashboard.js starting with line 117:


if(!mobile_menu_initialized){
            $navbar = $('nav').find('.navbar-collapse').first().clone(true);

            nav_content = '';
            mobile_menu_content = '';

            $navbar.children('ul').each(function(){

                content_buff = $(this).html();
                nav_content = nav_content + content_buff;
            });

            nav_content = '<ul class="nav nav-mobile-menu">' + nav_content + '</ul>';

            $navbar_form = $('nav').find('.navbar-form').clone(true);

            $sidebar_nav = $sidebar_wrapper.find(' > .nav');

            // insert the navbar form before the sidebar list
            $nav_content = $(nav_content);
            $nav_content.insertBefore($sidebar_nav);
            $navbar_form.insertBefore($nav_content);

            $(".sidebar-wrapper .dropdown .dropdown-menu > li > a").click(function(event) {
                event.stopPropagation();

            });

            mobile_menu_initialized = true;

Basically, we move the search using the clone, that means it is losing all the JS properties. Maybe you can add the clone(true,true) as presented here: https://stackoverflow.com/questions/9549643/jquery-clone-not-cloning-event-bindings-even-with-on

I will do some further checks with my colleague @dragosct10 and come back with a solution on Monday/Tuesday.

Keep in touch, Alex

juliocast commented 6 years ago

Hi Alex!

Thank you very much for explaining. I could solve the issue but I may have overcomplicated it. I added the mask setting in the file too and it solved the mask issue.

        mobile_menu_initialized = true;

        // Plate mask for search field
        $('#plate-search-field').mask('SSS0000');

Now for the form handling what I did is the following:

        // Form submit event listener and handler
        document.addEventListener('submit', eventHandler);

        function eventHandler(e) {
            e.preventDefault();
            let sourceFormId = e.srcElement.id;

            if (sourceFormId === 'plate-search-form') {
                let plate = e.target["0"].value;
                searchPlate(plate);
            }
        }

I'm looking for submit events and checking the source element ID, this way I can have as many forms as I want and just treat them by their IDs.

One thing I wanted to point too is that there is one doubled menu item within the mobile view sidebar: image I understand that this happens because all of the desktop search menu is put within the sidebar. image

I think I can hide the second one and give an "active" class to the first one, right?

Thank you very much!