1

I'm building an interactive floorplan directory with svgs using AngularJS and a little bit of jQuery. The idea is that when you click on a name and/or department the slider will scroll to the floor its on and the room its located in using an href response. Once the room is found a flicker animation is supposed to happen over that room.

This was the original way the code was written and it worked fine.

<h2 class="show"  id="roomsHeading">Rooms</h2>
<ul class="rooms">
   <li class="show" ng-repeat="x in pan.people"><a href="{{x.room}}">{{x.department}}</a></li>
</ul>

But I wanted to replace the header with select and options tags to organize the directory. You can see below.

<select id="roomsHeading" class="custom-select show" ng-model="change">
   <option value="Default">All</option>
   <option class="options show" value="Departments">Departments</option>
   <option class="options show" value="Faculty">Faculty</option>
</select>
<ul class="rooms" ng-switch="change" >
   <li class="show" ng-switch-when="Default" ng-repeat="x in pan.people"><a href="{{x.room}}">{{x.department}}</a><a href="{{x.room}}">{{x.name}}</a></li>
   <li class="show" ng-switch-when="Departments" ng-repeat="x in pan.people">
      <a href="{{x.room}}">{{x.department}}</a>
   </li>
   <li class="show" ng-switch-when="Faculty" ng-repeat="x in pan.people">
      <a href="{{x.room}}">{{x.name}}</a>
   </li>
</ul>

Once I changed the header to select/options tags the jQuery stopped working. Here is a link to the demo. Demo

jQuery below:

$(function() {

    //overwrite jquery to match any case (by making uppercase)
    jQuery.expr[':'].contains = function(a, i, m) {
        return jQuery(a).text().toUpperCase()
            .indexOf(m[3].toUpperCase()) >= 0;
    };

    var deactivateAll=function() {
        $('.roomHighlight').removeClass('active');
        $('.directory a').removeClass('active');
        $('g').removeClass('hasActiveCircle');
    }    

    var hideShowHeadings=function() {
        if($('.rooms .show').length >0) {
            $('#roomsHeading').addClass('show');
        }
        else {
            $('#roomsHeading').removeClass('show');
        }
        if($('.people .show').length >0) {
            $('#peopleHeading').addClass('show');
        }
        else {
            $('#peopleHeading').removeClass('show');
        }
    }

    $('.directory .rooms a').on( "click", function(e) {
        e.preventDefault();
        var target = $(this).attr('href');
        deactivateAll();

        $('#'+target).addClass('active')
        $(this).addClass('active')
        $('.slider').animate({scrollTop: $("#"+target).offset().top});
    });

    $('.directory .people a').on( "click", function(e) {
        e.preventDefault();
        var target = $(this).attr('href');
        deactivateAll();

        $(this).addClass('active')
        $('.slider').animate({
            scrollTop: $("#"+target).offset().top}, 200);
        $("#"+target).addClass('hasActiveCircle')
    });

    $('#searchBox').on( "input", function(e) {
        var searchMatch = $(this).val();
        console.log( searchMatch);

        if(searchMatch =="") {
            $("li").addClass("show");
        }
        else {
            $("li").removeClass("show");
            $("li:contains("+searchMatch+")").addClass("show");
            hideShowHeadings();
        }

    });
});
Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62
Nick
  • 21
  • 5
  • 2
    Typically, it's because jQuery and Angular have no business together. Angular generates and regenerates the DOM on the fly, without a clue of jQuery's presence and actions. And jQuery manipulates the same DOM, without a clue of Angular's presence and actions. They only disturb each other. This can only lead to messy and complex applications, full of workaround and hacks. Not to mention that you have to load two libraries instead of one. Solution : use Angular OR jQuery, not both at the same time. – Jeremy Thille May 13 '20 at 14:49
  • Interesting. I've been using both libraries through out the creation of multiple projects without issue, well until now. But thank you I was not aware of there incompatibility I'll see if I can accomplish my objective without jquery then. – Nick May 13 '20 at 15:15
  • Their philosophies are incompatible. Angular works by generating and regenerating the DOM when data changes. jQuery works by modifying the DOM by selecting existing elements. You can't make them work together without issues. Do everything with Angular or everything with jQuery. – Jeremy Thille May 14 '20 at 07:07

0 Answers0