Keep updated

Documentation

Examples

Working with dropdowns

jExcel is very flexible working with dropdowns and autocomplete columns. Several features are available, such as:

  • Create a simple dropdown from array
  • Value or key-value selectbox is available
  • Populate a dropdown from a external JSON request
  • Autocomplete dropdown. See an example
  • Dynamic autocomplete search based on another column value
  • Conditional dropdowns: options from a dropdown based on a method return


Source code

<html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jexcel/1.5.7/js/jquery.jexcel.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jexcel/1.5.7/css/jquery.jexcel.min.css" type="text/css" />

<div id="my"></div>

<script>
data = [
    [3, 'Cheese'],
    [1, 'Apples'],
    [2, 'Carrots'],
    [1, 'Oranges'],
];

dropdown = function(instance, cell, c, r, source) {
    // Value from the previous column
    var value = $('#my').jexcel('getValue', c-1 + '-' + r);

    // The dropdown options will be based on the value of the previous column
    if (value == 1) {
        return ['Apples','Bananas','Oranges'];
    } else if (value == 2) {
        return ['Carrots'];
    } else {
        return source;
    }
}

$('#my').jexcel({
    data:data,
    colHeaders: ['Model','Color', 'Buy'],
    colWidths: [ 300, 80, 100 ],
    columns: [
        { type: 'dropdown', source:[ {'id':'1', 'name':'Fruits'}, {'id':'2', 'name':'Legumes'}, {'id':'3', 'name':'General Food'}, ] },
        { type: 'dropdown', source:['Apples','Bananas','Carrots','Oranges','Cheese'], filter:dropdown },
        { type: 'checkbox' },
    ]
});
</script>
</html>

jsFiddle Demo