0

I have created a view for a datatable (Which is called upon a modal from a link in a datatable in another view), and I intend to put buttons to export the data to Excel, PDF, Print...etc. I have added the script, but the buttons are not displayed in the final output.

What are the library files that I want to load in order to make the code working with the results that I expect from it?

Note: I have used JQuery 3.1.1

I have tried various methods, also by changing the script library references to no avail. I even tried the download builder from datatables.net, but any of it seemed not working too.

*The html code*
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="btn btn-default" data-dismiss="modal">Back</button>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h2 class="modal-title">Item</h2>
            <h4>TRANSFER HISTORY</h4>
        </div>
        <div>
            <table id="example" class="display" style="width:100%">
                <tr style="background-color:#b5bbc8">
                    <th>
                        From
                    </th>
                    <th>
                        To
                    </th>
                    <th>
                        Date
                    </th>
                    <th>
                        Cause
                    </th>
                    <th>
                        Updated By
                    </th>
                </tr>

                @foreach (var item in Model)
            {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.Center.Name)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Center1.Name)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.TransferDate)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.TransferCause)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.User.FirstName) @Html.DisplayFor(modelItem => item.User.LastName)
                        </td>
                    </tr>
                }
            </table>
        </div>
    </div>
</div>

*The Buttons script*
<script type="text/javascript">
    $(document).ready(function () {
        $('#example').DataTable({
            dom: 'Bfrtip',
            buttons: [
                'copy', 'csv', 'excel', 'pdf', 'print'
            ]
        });
    });
</script>

Update - I just found out what was missing. Actually, it was a reference in the parent view, from which this view is loaded. I fixed it, now everything works fine. Thank you all for your answers and feedback.

4

3 Answers 3

1

You can implement the datatable in grid.you can add defer in JS

<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js" 
 type="text/javascript" defer></script>
<script 
src="https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min.js" 
type="text/javascript"></script>
<script 
src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js" 
type="text/javascript"></script>
<script 
src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js" 
type="text/javascript"></script>
<script 
src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js" 
type="text/javascript"> </script>
<script 
src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.html5.min.js" 
type="text/javascript"></script>

<link 
href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" 
rel="stylesheet"/>
<link href="https://cdn.datatables.net/buttons/1.5.6/css/buttons.dataTables.min.css"rel="stylesheet" />

It's working for me, I hope its help you Please Add Alll JS and CSS in your view page

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

Comments

0

Just need to ask you did you have verify you'r script through live data table dom, if not please go to there and check / veriry your script and design link, i hope you will resolved your matter by testing online portal.

1 Comment

this should be comment, not answer
0

as far as I understand, you are trying to initialize datatable inside a modal, so you have to make sure that the data in the modal is loaded correctly before the datatable is initialized, ideally upon the click of the modal button you should call a function in your javascript which makes sure that the data in the modal is loaded correctly and then initializes datatable.

And as far as scripts are concerned, these are the scripts you need to include for datatable buttons to work according to datatable website:-

https://code.jquery.com/jquery-3.3.1.js
https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js
https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min.js
https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js
https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js
https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js
https://cdn.datatables.net/buttons/1.5.6/js/buttons.html5.min.js

3 Comments

I tried, but nothing happened. Thanks for the quick response.
is your datatable initializing correctly? are you sure the scripts are loading in the correct order?
Yes, the datatable is initialized without a hiccup. I will try the references again on a separate dummy view again, for further review. Thanks

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.