Materialize the selection "Check / Uncheck All"

I have the following markup

<select ng-model="sltStatus" id="sltStatusFilter" multiple class="right">
    <option value="0">View All</option>
    <option value="1">Study 1</option>
    <option value="2">Study 2</option>
    <option value="3">Study 3</option>
    <option value="4">Study 4</option>
</select>

      

And below is the JavaScript code

$('select').material_select();

      

Check the boxes to use for this.

fiddle

How can I check / uncheck all functionality, I have searched many times but did not find anything related.

+3


source to share


5 answers


As I understand from your comments, I think this is what you want

$('ul.dropdown-content li').first().click(function(){    
  $('ul.dropdown-content li:not(:first-child)').each(function(index) {
           $(this).find("input[type=checkbox]").prop("checked", $('ul.dropdown-content li').first().find("input[type=checkbox]").prop("checked"));                       
        });
});

      

updated script https://jsfiddle.net/xv0p06d3/21/

just be careful with that because I'm using what appears to be a class from Materialize ... so if changing the class you will need to change the code. You can find a way to use your code to get ul

/ elements li

.

Hope it helps.



UPDATE the
answer above is pure jquery (like your fiddle like just jquery) ... one way for you to make this work with angular is to create a directive like this.

app.directive('selectMultiple', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            $(element).find('select').material_select();
            $(element).find("ul li").first().click(function(){  
            $(element).find("ul li:not(:first-child)").each(function(index) {
                   $(this).find("input[type=checkbox]").prop("checked", $(element).find("ul li").first().find("input[type=checkbox]").prop("checked"));                      
                 });
            });
        }
    };
});

      

Check out the angular fiddle http://jsfiddle.net/TH87t/1545/

Sincerely.

+2


source


MaterializeCSS does not support this feature internally. However, this can be accomplished using jQuery DOM manipulation methods.

  • Bind click

    Event: The plugin will add a class initialized

    to <select>

    and a unique identifier to the corresponding <ul>

    one that starts with select-options-

    . We can use this knowledge to choose the first <li>

    child, i.e. "Check All" from the dropdown list.
  • Check if "Check All" is active

    selected or not: the plugin will add a class to the selected elements. Using hasClass()

    , we can check if the class is added active

    to the first parameter in the select element.
  • Select / Deselect other parameters: Using the previous step, we can get the status "Check all". Using this, we can iterate over other parameters and fire an event click

    on the corresponding elements.
  • Bonus . Updating the parameter text. To update the option text, get the text using the method text()

    and compare it with Check All

    . Using contents()

    and textContent

    , the text can be easily updated.

Note. ... This code is totally dependent on DOM elements created by a third party plugin. If the plugin changes the HTML structure after the update, this code will not work or must be updated accordingly.



Demo

$('select').material_select();

$('select[select-all].initialized').prev('ul[id^="select-options-"]').children('li:first').on('click', function() {
  var selector = 'li.active';
  if ($(this).hasClass('active')) {
    selector = 'li:not(".active")';
  }

  $(this).siblings(selector).each(function() {
    $(this).click();
  });

  $('span', this).contents().last()[0].textContent = $(this).text().trim() === 'Check All' ? 'Uncheck All' : 'Check All';
  // Check values of selected options
  console.log($(this).parent().next().val());
});
      

<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.96.0/css/materialize.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/js/materialize.min.js"></script>


With "select-all" option
<div class="input-field col s5">
  <select multiple select-all searchable="search here..">
    <option value="0">Check All</option>
    <option value="1">Yellow</option>
    <option value="2">Green</option>
    <option value="3">Pink</option>
    <option value="4">Orange</option>
  </select>
  <label>Materialize Multiple Select</label>
</div>


<br />
Without "Select All" option
<div class="input-field col s5">
  <select multiple searchable="Animals">
    <option value="0">Cat</option>
    <option value="1">Dog</option>
    <option value="2">Horse</option>
    <option value="3">Pig</option>
    <option value="4">Lion</option>
  </select>
  <label>Materialize Multiple Select</label>
</div>
      

Run codeHide result


+2


source


An opinion is materialized about how UX elements should behave and interact visually. for this, I suggest you add a personal jQuery function to fix what you want to do like this:

    // Listen for click on toggle checkbox
$('#select-all').click(function(event) {   
    if(this.checked) {
        // Iterate each checkbox
        $(':checkbox').each(function() {
            this.checked = true;                        
        });
    }
});

<input type="checkbox" name="select-all" id="select-all" />

      

0


source


Hard to materialize to customize! As you will see in the check item, your select is writing to several different divs with a class select-wrapper

So I am doing it with this class for the check / uncheck function ...

$(".select-wrapper ul li:first").on("click",function(){    
    if($(this).hasClass("active")){
        $(this).parent().find("li").addClass("active selected");
        $(this).parent().find("input[type=checkbox]").prop("checked", true);
    } else {
        $(this).parent().find("li").removeClass("active selected");
        $(this).parent().find("input[type=checkbox]").prop("checked", false);
    }    
});

      

0


source


The problem I found is that if you try to set values ​​with the $('select').val(...)

checkboxes are not updated. You need to update them manually by calling $('select').material_select()

again:

$('select').material_select();
$('select').on('change',function () {
    var selected=$('select').val();
  if (selected.indexOf('0')===0) {
    $('select').val(['0','1','2','3','4']).material_select();
  }
})

      

This workaround marks all options, but closes the selection, and it seems that starting an open activity does not work. Documentation about materializing web is bad about it, it might not be possible to control the component programmatically.

Here's an incomplete demo, but you can get the general idea:

https://jsfiddle.net/xv0p06d3/23/

0


source







All Articles