Strange bug between the JSON language files and unbind event

Strange bug between the JSON language files and unbind event

andreszsandreszs Posts: 5Questions: 1Answers: 0

I've added the following code to replace the auto-submit behaviour of the search input and replace it with search only after pressing Enter, which works great:

$('#dataTable_filter input').unbind();
$('#dataTable_filter input').bind('keyup', function (e) {
    if (e.keyCode == 13) {
        oTable.search(this.value).draw();
    }
});

However, whenever I add a JSON language file to translate DataTables, the above code is suddenly ignored and the search input returns to the default mode of auto-submit itself on keypress. This is a problem because I really need to translate DataTables AND prevent the auto search feature due to database performance issues.

The full code is this:

var oTable = $('#dataTable').DataTable({
    processing: true,
    responsive: true,
    serverSide: true,
    stateSave: false,
    pageLength: 25,
    order: [0, 'desc'],
    columnDefs: [
        {searchable: false, targets: [6, 7, 9, 10]}
    ],
    language: {
        url: '../sbadmin/vendor/datatables-plugins/es_es.json'
    },
    ajax: {
        url: 'get.php',
        type: 'GET',
        error: function (xhr, error, code) {
            console.warn(xhr);
            alert(xhr.responseJSON.message);
        }
    }
});
/* Evitar autobúsqueda al tipear en search */
$('#dataTable_filter input').unbind();
$('#dataTable_filter input').bind('keyup', function (e) {
    if (e.keyCode == 13) {
        oTable.search(this.value).draw();
    }
});

I'm not sure how the addition of a language file relates to some unbind() event, any ideas will be welcome. Thanks.

Replies

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    Using language.url is an synchronous process. I suspect your unbind and bind events are executing too soon before Datatables initialization has completed. Move your code inside initComplete. Let us know if that helps.

    Kevin

  • andreszsandreszs Posts: 5Questions: 1Answers: 0
    edited June 2021

    Thanks @kthorngren that worked perfectly fine and my search input now behaves as intended.

Sign In or Register to comment.