0

I have a multiple select:

<select name="courier" class="selectpicker courierpicker" multiple>
    <option value="value1">Value1</option>
    <option value="value2">Value2</option>
    ...
</select>

I want to use selected options as parameters for filtering rows in my table. This code doesn't work for me (assume, just for this example, that there is always more than one selected option):

$(".selectpicker").change(function() {
    var items = [];
    $('.courierpicker option:selected').each(function(){ 
        items.push($(this).val()); 
    });
    var $result = '["' + items.join('","') + '"]';
    $('#data-table').bootstrapTable('filterBy', {
        courierfilter: $result
    });
});

When I trigger:

$('#data-table').bootstrapTable('filterBy', { 
    courierfilter: ["value1","value2"] 
});

Everything works just fine.

I know it will be some stupid mistake of the beginner, but thanks in advance for every help.

2 Answers 2

1

The issue is because the courierfilter property expects an array, yet you're providing it a string which is formatted like an array. Also note that the array generation can be made simpler through the use of the map() method. Try this:

$(".selectpicker").change(function() {
    var items = $('.courierpicker option:selected').map(function(){ 
        return this.value;
    }).get();
    $('#data-table').bootstrapTable('filterBy', {    
        courierfilter: items 
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

Just use {courierfilter: items}.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.