Datatable start with default value

Asked

Viewed 151 times

2

I’d like some help with my problem. I wonder if there is the possibility to start datatables with the search field already filled with a default value. I tried it this way.

$(document).ready(function(){
 $('input[type="search"').val('valor_default');
$('#table').DataTable();
});

That way it doesn’t work. If you can help or have some light thank you.

  • I believe that the order of the code ai should be first the date table, and then the fill.

  • @Robsonbraga true, I’ll test

1 answer

1


Beyond what the @Robson Braga commented (first start component), you need to call two actions of component API: .search() and .draw(), otherwise you will only put a value in the "search" input without filtering the table.

Would look like this:

$(document).ready( function () {
    var table = $('#myTable').DataTable();
    var valor_padrao = 'Data 3';
    $('input[type="search"').val(valor_padrao);
    table.search(valor_padrao).draw();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>

<table id="myTable" class="display">
    <thead>
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Row 1 Data 1</td>
            <td>Row 1 Data 2</td>
        </tr>
        <tr>
            <td>Row 2 Data 1</td>
            <td>Row 2 Data 3</td>
        </tr>
    </tbody>
</table>

  • This ai vlw by help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.