How enable button when no results found datatables

て烟熏妆下的殇ゞ 提交于 2019-12-12 02:06:22

问题


<script>
    $(document).ready(function() {
        $('#dataTables-example').DataTable({
                responsive: true
        });
    });
    </script>
<script>
$(document).ready(function () {

   $('#dataTables-example').dataTable( {
    "fnDrawCallback": function( oSettings ) {
        if ($(".dataTables_empty")[0]) {
            $('#newclient').prop("disabled", false);
        } else {
            $('#newclient').prop("disabled", true);
        }
    }
  } );
});
</script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js"></script>
    <script src="//cdn.datatables.net/plug-ins/3cfcc339e89/integration/bootstrap/3/dataTables.bootstrap.js"></script>
	<link href="//cdn.datatables.net/plug-ins/3cfcc339e89/integration/bootstrap/3/dataTables.bootstrap.css" rel="stylesheet">
    <link href="//cdn.datatables.net/1.10.4/css/jquery.dataTables.min.css" rel="stylesheet">    
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">

</head>

<body>


                        	<div class="add-client"><button type="button" class="btn btn-primary btn-sm" id="newclient"  disabled="true">Add Client</button></div>
                    
                                <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                    <thead>
                                        <tr>
                                            <th>Name</th>
                                            <th>File Number</th>
                                            <th>Department</th>
                                            <th>Date</th>
                                            <th>User Name</th>
                                            <th>Password</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr class="odd gradeX">
                                            <td><a href="view-client.html">Mbello Elizabeth</a></td>
                                            <td>954687</td>
                                            <td>w547</td>
                                            <td>October 15 2013</td>
                                            <td>Mbello</td>
                                            <td>cfhqsuvx</td>
                                        </tr>

                                    </tbody>
                                </table>

I'm using DataTables and I need to enable a button when no results found after searching, Any one can help me please.

Thanks

it works here but it dosen't work in my page, I don't know what is the problem


回答1:


You could use the DataTables draw callback to find out if there are no results in the current table. fnDrawCallback

This is a simple fiddle to check how to enable a button after searching with no results.

Javascript:

$(document).ready(function () {
  $('#example').dataTable( {
    "fnDrawCallback": function( oSettings ) {
        if ($(".dataTables_empty")[0]) {
            $('#test').prop("disabled", false);
        } else {
            $('#test').prop("disabled", true);
        }
    }
  } );
});


来源:https://stackoverflow.com/questions/28221203/how-enable-button-when-no-results-found-datatables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!