0

I am attempting to implement a bootstrap 4 datatable, and cannot seem to get it to work. In My head I have the required CSS

    <link href="https://cdn.datatables.net/1.10.20/css/dataTables.bootstrap4.min.css" rel="stylesheet">

I then have the following required scripts below:

    <script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap4.min.js"></script>

I then call the scripts onto my standard formatted HTML table, with the correct ID as follows:

<script>
$(document).ready(function() {
    $('#myTable').DataTable();
} );
</script>

None of this However works, and the table displays using normal Bootstrap 4 Table, with no search/sorting function. Could anyone explain what I'm doing wrong here?

Thanks

EDIT: I tried this unfortunately it seemed to break the CSS for the whole site. All I really want to do is only show the first 10 rows of my bootstrap 4 table, but every time I google it it comes up with results for data tables. Is there any way to show just the first 10 rows of a standard bootstrap 4 table?

2
  • Have you checked your console for error messages? Commented Nov 2, 2019 at 22:37
  • Yep, nothing to be seen... Commented Nov 2, 2019 at 22:43

1 Answer 1

5

It seems like you haven't imported Jquery in your HTML File. you need to use 3 files for Data Tables as follow

https://code.jquery.com/jquery-3.3.1.js
https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js
https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap4.min.js

But you only added

https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js
https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap4.min.js

Import JQuery before

jquery.dataTables.min.js and
dataTables.bootstrap4.min.js

after the imports of js and jquery files write your Jquery Code

<script>
   $(document).ready(function() {
      $('#myTable').DataTable();
    });
</script>

and for header

https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css
https://cdn.datatables.net/1.10.20/css/dataTables.bootstrap4.min.css

check that as follow, This will work fine.

Sign up to request clarification or add additional context in comments.

12 Comments

I tried this unfortunately it seemed to break the CSS for the whole site. All I really want to do is only show the first 10 rows of my bootstrap 4 table, but every time I google it it comes up with results for data tables. Is there any way to show just the first 10 rows of a standard bootstrap 4 table?
if it breaks CSS of the whole site it means you are using any files twice or used jquery twice .. it should not break the CSS, double-check it if you have imported CSS properly. if its possible upload your code on jsfiddle so i can check and let you know the exact problem.
Working Example of DataTables. Try it jsfiddle.net/ebRXw/2985
I will try again- if I have multiple versions of JQuery, should I use the most up to date version?
yes use latest it will not make any issue.. and do check that fiddle I shared with you.. its working example. so you just need to copy and change as per your requirements.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.