Jan
Jan

Reputation: 582

Set autofocus on Datatable - $.focus() not making changes

I would like to set the autofocus to the first select input at the table. However, cause the select input gets created by DataTables I have to call the jQuery function after the select input and the table got rendered by DataTables. I am trying to to this with $("#autofocus_on_load").focus(). But it isn't making any changes or at least focusing the select input..

How can that be? - You guys have any solutions for this?

var table;

$(document).ready(function() {
  table = $('#assignment_overview_table').DataTable( {
    "displayStart": 0,
    "language": {
      "url": "https://cdn.datatables.net/plug-ins/1.10.19/i18n/German.json"
    },
    "processing": true,
    "pageLength": 100,
    "bSortCellsTop": true,
    "fixedHeader": true,
    initComplete: function () {
      this.api().columns().every( function () {
        var column = this;
        var select = $('<select><option value=""></option></select>');
        select.appendTo( $(column.header()).empty() )

        select.on( 'click', function(e) {
          e.stopPropagation();
        } );


        select.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>' )
        } );
      } );
    },
  });

  table.on('draw', function () {
    table.columns().indexes().each( function ( idx ) {
      var select = $(table.column( idx ).header()).find('select');

      if ( select.val() === '' ) {
        select
          .empty()
          .append('<option value=""/>');

        table.column(idx, {search:'applied'}).data().unique().sort().each( function ( d, j ) {
          select.append( '<option value="'+d+'">'+d+'</option>' );
        } );
      }
    } );
  } );


  $("#autofocus_on_load > select").focus();
} );
<script src="https://code.jquery.com/jquery-3.1.0.js"></script><script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js" type="text/javascript"></script>
<script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap.min.js" type="text/javascript"></script>
<script src="https://cdn.datatables.net/fixedheader/3.1.5/js/dataTables.fixedHeader.min.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="https://cdn.datatables.net/fixedheader/3.1.5/css/fixedHeader.bootstrap.min.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap.min.css">

<table class="table table-striped table-bordered" id="assignment_overview_table">
<thead>
      <tr>
        <th id="autofocus_on_load">ID</th>
        <th>NAME</th>
        <th>SURNAME</th>
        <th>CASTE</th>
        <th>EXTRA</th>
      </tr>
      <tr>
        <th>ID</th>
        <th>NAME</th>
        <th>SURNAME</th>
        <th>CASTE</th>
        <th>EXTRA</th>
      </tr>
    </thead>
    <tbody>
    <tr>
      <td>1</td>
      <td>xyz</td>
      <td>xyz</td>
      <td>xyz</td>
      <td>xyz</td>
    </tr>
    <tr>
      <td>2</td>
      <td>xyz</td>
      <td>xyz</td>
      <td>xyz</td>
      <td>xyz</td>
    </tr>
    <tr>
      <td>3</td>
      <td>xyz</td>
      <td>xyz</td>
      <td>xyz</td>
      <td>xyz</td>
    </tr>
    <tr>
      <td>4</td>
      <td>xyz</td>
      <td>xyz</td>
      <td>xyz</td>
      <td>xyz</td>
    </tr>
    <tr>
      <td>5</td>
      <td>xyz</td>
      <td>xyz</td>
      <td>xyz</td>
      <td>xyz</td>
    </tr>
    </tbody>
</table>

Kind regards and Thank You!

Upvotes: 0

Views: 1939

Answers (2)

Jeto
Jeto

Reputation: 14927

Just move this line:

 $("#autofocus_on_load > select").focus();

to the end of initComplete.

Your <select>'s don't exist until then.

Upvotes: 1

mustafa bagwala
mustafa bagwala

Reputation: 431

I can see that you are assigning an ID #autofocus_on_load to <th> element which is not focusable. You should use <input type="text" autofocus="true" />

Or Same I believe you cannot use id or class selector for dynamic DOM rendering. You can use EventListener instead,. Try with this. I hope this will work for you

Upvotes: 1

Related Questions