How to display a custom message when no data was returned to the jQuery DataTable

I am using jQuery DataTables to display data in ASP.NET Core 6 MVC project. I want to display a custom message when no data was returned from the SQL query, instead of the default text, “Showing 0 to 0 of 0 entries”. I tried a couple of methods as shown here.

Method 1, using the language option:

var dataTableOptions = {
   "paging": true,
   "lengthChange": false,
   "searching": true,
   "ordering": true,
   "info": true,
   "autoWidth": false,
   "responsive": true,
   "columnDefs": "",
   "processing": true,
   "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
   "pageLength": 10,
   "language": {
       "emptyTable": 'No records found!',
       "zeroRecords": 'No records found!'
   },
   //More config code here
}

Method 2 displaying the message as a text of a div:

<div id="emptyRows">No records found!</div>

var myDataTable = $('#riskReport').DataTable(dataTableOptions);
if (!myDataTable.data().any()) {
    //alert('No records found!');  //This works     
    //$('#riskReport_info').text('No records found!'); //This didn't work
    $('#emptyRows').show();
}            
else {      
    $('#emptyRows').hide(); //Code never reached here
}

The second method displays the ‘No records found!’ as the text of the div, when no data was returned by the SQL. However when the search method was changed such that data was returned successfully, still the ‘No records found!’ is displayed since the div is not hidden. As an additional option, I tried to set the text of #riskReport_info, which is the id used by the DataTable natively to display the default message.

1

For Method 1, ensure emptyTable and zeroRecords are correctly set and that no server-side processing is interfering.

For Method 2, bind the draw event to show or hide the message after each table update:

myDataTable.on('draw', function () {
    if (!myDataTable.data().any()) {
        $('#emptyRows').show();
    } else {
        $('#emptyRows').hide();
    }
});

After my test, if you want to display a custom message to replace the default text, you need to use the info and infoEmpty properties in the language option to hide this part of the information, set them to empty strings, and then configure the emptyTable property and zeroRecords property as custom messages:

emptyTable: means when there is no data when the table is initialized.

zeroRecords: means when there are no matching results for the search.

As shown in the following example:

@model IEnumerable<Product>

@{
    ViewData["Title"] = "Product List";
}

<h2>@ViewData["Title"]</h2>


<table id="productTable" class="display">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Price</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var product in Model)
        {
            <tr>
                <td>@product.Id</td>
                <td>@product.Name</td>
                <td>@product.Price</td>
            </tr>
        }
    </tbody>
</table>

@section Scripts {
    <script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
    <script>
        $(document).ready(function () {
            var dataTableOptions = {
                "paging": true,
                "lengthChange": false,
                "searching": true,
                "ordering": true,
                "info": true,
                "autoWidth": false,
                "responsive": true,
                "processing": true,
                "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
                "pageLength": 10,
                "language": {
                    "emptyTable": 'No records found!',
                    "zeroRecords": 'No matching records found!',
                    "info": "",                              
                    "infoEmpty": ""
                },
          
            };

            $('#productTable').DataTable(dataTableOptions);
        });
    </script>
}

when there is no data when the table is initialized:

when there are no matching results for the search:

At the same time, if you want to hide information such as “(filtered from X total entries)”, you can set the infoFiltered property in the language option to an empty string to hide this part of the content.

About your second method:
You can use drawCallback to handle dynamic data, as shown in the example:

<h2>@ViewData["Title"]</h2>

<div id="emptyRows" style="display:none;color:red;">No records found!</div>

@section Scripts {
    <script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
    <script>
        $(document).ready(function () {
            var dataTableOptions = {
                "paging": true,
                "lengthChange": false,
                "searching": true,
                "ordering": true,
                "info": true,
                "autoWidth": false,
                "responsive": true,
                "processing": true,
                "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
                "pageLength": 10,
                "language": {
                    "info": "",                              
                    "infoEmpty": "",
                    "infoFiltered":"",
                   
                },
                    "drawCallback": function (settings) {
                        var api = this.api();
                        var rows = api.rows({ filter: 'applied' }).data().length;

                        if (rows === 0) {
                            $('#emptyRows').show();
                        } else {
                            $('#emptyRows').hide();
                        }
                    }
            };

            $('#productTable').DataTable(dataTableOptions);
        });
    </script>
}

When the query does not exist data:

When the data exists:

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật