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

Select does not work without jQuery #43

Closed
wuda-io opened this issue Oct 20, 2020 · 5 comments · Fixed by #57
Closed

Select does not work without jQuery #43

wuda-io opened this issue Oct 20, 2020 · 5 comments · Fixed by #57

Comments

@wuda-io
Copy link
Member

wuda-io commented Oct 20, 2020

First: Thank you dudes! The compiliation process worked! Awesome... I see materialize is moving forward.

When I remove the jQuery dependency and there is a small issue when i am trying to expand a Select Component / DropDown Menu. The Error in the Console says:

Uncaught ReferenceError: $ is not defined
    getClosestAncestor https://localhost/_WORK/developing/AutoKMS/js/materialize.min.js:6
    value https://localhost/_WORK/developing/AutoKMS/js/materialize.min.js:6
    value https://localhost/_WORK/developing/AutoKMS/js/materialize.min.js:6
    value https://localhost/_WORK/developing/AutoKMS/js/materialize.min.js:6
materialize.min.js:6:14917

Steps to Reproduce (for bugs)

  1. Create a Select (not multi)
  2. Load Elements
  3. Click on Select

Your Environment

  • Version used: latest v1-dev from yesterday 20.10.2020
  • Browser Name and version: Firefox 81
  • Operating System and version (desktop or mobile): Windows 10 desktop
@wuda-io wuda-io changed the title Dropdown does not work without jQuery Select does not work without jQuery Oct 20, 2020
@AvnerCohen
Copy link

I suspect without jquery, you should be using the alternative init code:

				document.addEventListener('DOMContentLoaded', function() {
			    	  var elems = document.querySelectorAll('select');
			    	  var instances = M.FormSelect.init(elems);
			  	});

@wuda-io
Copy link
Member Author

wuda-io commented Nov 14, 2020

Yes I have done it exactly like you described it.
But it seems to be a bug somewhere in the newest version, because with the old versions it works without jquery.

My workaround for now is to include jquery...

@wuda-io
Copy link
Member Author

wuda-io commented Dec 8, 2020

Any updates on That?
The relevant Line is in the Global.js:

M.getClosestAncestor = function(el, condition) {
  let ancestor = el.parentNode
  while (ancestor !== null           >>>>>     && !$(ancestor).is(document)      <<<<        ) {
    if (condition(ancestor)) {
      return ancestor;
    }
    ancestor = ancestor.parentNode;
  }
  return null;
};

But when I comment the part out I still can not use the Library....
Who implemented this Function an why is it needed?
It seems that it was implemented later and was not Tested without jQuery! So you can mark this issue as a BUG!

I wanted to implement a better Select with Ajax-Loading and IDs instead of names and search in Dropdown.
But Materialize itself does not even work properly... thats really bad :(
maybe I better switch to the Material Design Light from Google

@wuda-io
Copy link
Member Author

wuda-io commented Dec 8, 2020

OK I fixed the Bug!!! Since the Function is only used in Dropdown.js at a specific place, here is the Solution:

    /**
     * Place dropdown
     */
    _placeDropdown() {
      // Container here will be closest ancestor with overflow: hidden
      
      const getClosestAncestor = function(el, condition) {
        let ancestor = el.parentNode;
        while (ancestor !== null && !$(ancestor).is(document)) {
          if (condition(ancestor)) {
            return ancestor;
          }
          ancestor = ancestor.parentNode;
        }
        return null;
      };


      let closestOverflowParent = getClosestAncestor(this.dropdownEl, (ancestor) => {
        return $(ancestor).css('overflow') !== 'visible';
      });

Now it Works!!! :) How does a pull request work

I also deleted the Function below, since it is never used...

M.elementOrParentIsFixed = function(element) {
  let $element = $(element);
  let $checkElements = $element.add($element.parents());
  let isFixed = false;
  $checkElements.each(function() {
    if ($(this).css('position') === 'fixed') {
      isFixed = true;
      return false;
    }
  });
  return isFixed;
};```

@wuda-io
Copy link
Member Author

wuda-io commented Dec 10, 2020

Dogfalo#6389

JayDijkstra pushed a commit to JayDijkstra/materialize that referenced this issue Mar 7, 2021
…Query. Especially the function getClosestAncestor(). See issue materializecss#43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants