Set a default value in select dropdown menu

Set a default value in select dropdown menu

kinnkinn Posts: 1Questions: 1Answers: 0
edited June 2019 in Free community support

Hello,
I am new to Datatables and js in general. I have the following code:

initComplete: function() {
  this.api().columns([0, 1, 2, 3]).every(function() {
    var column = this;
    select = $('<select><option value=""></option></select>')
      .appendTo($(column.header()).empty())
      .on('change', function() {
        var val = $.fn.dataTable.util.escapeRegex(
          $(this).val()
        );
        column
          .search(val ? '^' + val + '$' : '', true, false)
          .draw();
      });
    column.data().unique().sort().each(function(d, j) {
      select.append('<option value="' + d + '">' + d + '</option>')
    });
  })
}

that works nicely to show me select dropdown lists on several columns with dynamically selected values.
On the other hand, the first of those columns presents always only two values ("Yes","No") and I would like to set as a default value for the dropdown list the value "Yes". I managed to do it adding

select.val('Yes').attr('selected', 'selected');

after

select.append( '<option value="'+d+'">'+d+'</option>' )

but clearly, it only puts the Yes value as default, while the items are not filtered.
How can I implement it?

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

This discussion has been closed.