3

I have just come across SumoSelect.js for dropdowns in my MVC project. I just wanted to know if there is way to know if the select all option of the sumoselect dropdown is checked or not. This is my sumoslect drop down code.

   <div>
    @Html.LabelFor(m => m.Territory)
    @Html.DropDownListFor(m => m.Territory, Model.Territories, new { @class = "form-control", id = "ddlTerritory", multiple = "multiple", placeholder = "Select Territory" })
    @Html.ValidationMessageFor(m => m.Territory)
   </div>

JavaScript Code

 $(document).ready(function () 
  {
    $('#ddlTerritory').SumoSelect({ selectAll: true });
    if ($('#ddlTerritory')[0].sumo.selectAll())
        {
           some code
        }
  }

There is no mention of such method in the documentation of sumoselect. It would be great if some one can guide me in the right direction.

Edit: As Suggested by @stephen

$(document).ready(function () 
      {
        $('#ddlTerritory').SumoSelect({ selectAll: true });
var isChecked = $('#ddlTerritory').closest('.SumoSelect').find('.select-all')‌​.hasClass('.select')‌​;
        if (isChecked)
            {
               some code
            }
      }
Nimeshka Srimal
  • 8,012
  • 5
  • 42
  • 57
WorksOnMyLocal
  • 1,617
  • 3
  • 23
  • 44
  • `selectAll: true` will make all elements selected. No need to apply condition after that.. – Rayon Nov 08 '16 at 05:16
  • @Rayon I get that, but i want to know if the selectAll property is true or not so that i can bind further dependent dropdowns. – WorksOnMyLocal Nov 08 '16 at 05:21
  • Looking at the html it generates, you should be able to use `var isChecked = $('#ddlTerritory').closest('SumoSelect').find('.select-all').hasClass('.select');` –  Nov 08 '16 at 05:28
  • @StephenMuecke I just tried that , it throws an invalid character error at that line, Even the debugger prior to that line doesn't isn't hit due to this error. Any changes to the line of code you suggested? – WorksOnMyLocal Nov 08 '16 at 05:42
  • Sorry - missed the class selector - should be `.closest('.SumoSelect')` –  Nov 08 '16 at 05:45
  • @StephenMuecke still Doesn't work stephen. – WorksOnMyLocal Nov 08 '16 at 05:49
  • `$('#ddlTerritory').closest('SumoSelect').find('select')‌.find('option').length == ​$('#ddlTerritory').closest('SumoSelect').find('select')‌.find('option:selected').length` – Rayon Nov 08 '16 at 05:51
  • According to the [demos](https://hemantnegi.github.io/jquery.sumoselect/sumoselect_demo.html) - the plugin is wrapping your ` –  Nov 08 '16 at 05:54
  • @StephenMuecke I understood the logic behind your line of code, but it still shows invalid character error. – WorksOnMyLocal Nov 08 '16 at 05:58
  • How are you calling it (just using the code I showed with the corrections cant generate that error? –  Nov 08 '16 at 06:01
  • @Rayon it expects a ' ; ' or a ' ) ' and throws an error at 'find' – WorksOnMyLocal Nov 08 '16 at 06:02
  • @StephenMuecke I have updated the question with the line of code you suggested. – WorksOnMyLocal Nov 08 '16 at 06:07
  • You did not correct the `.​hasClass('select')‌​` bit. But there would be no point putting that code in `$(document).ready()` - you need to check it in response to user changing it. –  Nov 08 '16 at 06:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127591/discussion-between-shekar-gvr-and-stephen-muecke). – WorksOnMyLocal Nov 08 '16 at 06:12
  • 1
    @StephenMuecke My purpose of writing the code in ready() is to check if selectAll is checked and instead of showing the number of selectedoptions in the placeholder text, i would like to show it as ALL. – WorksOnMyLocal Nov 08 '16 at 08:37
  • @Shekar.gvr Do you find a solution? – Volodymyr Aug 10 '18 at 10:37

1 Answers1

1

this quite tricky, that I count selected items with total options:

function isMultipleSelectAll(id) {
    var ids = $(id).val();
    let options = $(id + " option");

    let totalSelected = ids.length;
    let totalOptions = options.length;

    if (totalSelected >= totalOptions) {
        return true;
    } else {
        return false;
    }
}
Lê Văn Hiếu
  • 171
  • 1
  • 6