0

I have a function listening to select elements.

function initEventListeners () {

     // Listening to category select
     $categorySelect.on("change", function () {
        helper.createFilteredSamples();
     });

     // Listening to platform select
     $platformSelect.on("change", function () {
        helper.createFilteredSamples();
     });

     // Listening to language select
     $langugeSelect.on("change", function () {
        helper.createFilteredSamples();
     });
}

And helper method to return matched samples

var helper = {
     // create filtered samples based on the search and dropdown values
     createFilteredSamples: function () {
        var categoryValue = $categorySelect.val();
        var languageValue = $langugeSelect.val();
        var platformValue = $platformSelect.val();

        if (categoryValue === "default" && languageValue === "default" && platformValue === "default") {
           helper.createTableUi(samples);
           return;
        }

        var sampleSet = samples.filter(function (sample) {
           var cats = sample.Category.split(" ");
           var langs = sample.Language.split(" ");
           var plats = sample.Platform.split(" ");

           return ($.inArray(categoryValue, cats) !== -1 && $.inArray(languageValue, langs) !== -1 && $.inArray(platformValue, plats) !== -1);
        });

        helper.createTableUi(sampleSet);
     }
  };

The problem is this line return ($.inArray(categoryValue, cats) !== -1 && $.inArray(languageValue, langs) !== -1 && $.inArray(platformValue, plats) !== -1);. This only works when users selected all three dropdowns. How do I get the matched result in any combination that users picked?

Jason
  • 1,680
  • 3
  • 13
  • 16
  • 1
    Do you mean you want to get the order the client selects them? If so you could use an array to hold the id/value of the selected item and remove the item from the array when deselected... – NewToJS Sep 08 '17 at 00:38
  • On your selects add a value by default for the language you can take the language of the browser and switch to this to add a good way dynamic default value. https://stackoverflow.com/questions/8199760/how-to-get-the-browser-language-using-javascript –  Sep 08 '17 at 00:42
  • Here is a quick example of how it can be done using checkboxes, change event and an array. https://jsfiddle.net/7xcfa4e4/ – NewToJS Sep 08 '17 at 01:03

0 Answers0