jquery - how to set value in semanic ui dropdown

时间:2017-12-18 05:14:02

标签: jquery drop-down-menu semantic-ui

I want to set dropdown with a value from an array of options

statuses = [
    { value: 'all', name: 'All' },
    { value: true, name: 'Received' },
    { value: false , name: 'Not Yet' }
];

// my jquery doesn't work
if (params['invoiceStatus'] && params['invoiceStatus'] !== '') {
    this.args.invoiceStatus = params['invoiceStatus'];
    $('#invoiceStatus').dropdown(
        'set selected', params['invoiceStatus']);
} else {
    $('#invoiceStatus').dropdown('all');
}

I added this jquery:

if (params['invoiceStatus'] && params['invoiceStatus'] !== '') 
{
    this.args.invoiceStatus = params['invoiceStatus'];
    $('#invoiceStatus').dropdown('set selected', params['invoiceStatus']);
}
else {
    $('#invoiceStatus').dropdown('all');
}

But doesn't work

1 个答案:

答案 0 :(得分:0)

https://semantic-ui.com/modules/dropdown.html#/usage

According to the semantic-ui docs, if you want to have create a dropdown with a selected value:

var selectedHelper = function (params, name) {
    if (params['invoiceStatus'] === name) return true;
    return false;
}

var statuses = [
    {
      value: 'all',
      name: 'All',
      selected: selectedHelper(params, 'All'),
    },
    {
      value: true,
      name: 'Received'
      selected: selectedHelper(params, 'Received'),
    },
    {
      value: false,
      name: 'Not Yet'
      selected: selectedHelper(params, 'Not Yet'),
    }
];

$('#invoiceStatus').dropdown({ values: statuses });