0

Is it possible to add the following jQuery DataTable?

$('#myDataTable').dataTable({

    });

to this query?

 $(document).on('click', '#PlayStatisticeight', function (e) {

    $.ajax({
        url: '@Url.Action("_PartialViewTopPlayedTracksList", "ReportStatistic")',
        type: 'GET',
        success: function (data) {
            $("#PartialViewTopPlayedTracksList").empty();
            $("#PartialViewTopPlayedTracksList").append(data);

            $('#myDataTable').dataTable({

            });


            $(function () {
                $("#PartialViewTopPlayedTracksList").load('@Url.Action("_PartialViewTopPlayedTracksList", "ReportStatistic")');
            });

        },
        error: function (xhr, textStatus, exceptionThrown) {

            var json = $.parseJSON(xhr.responseText);

            if (json.Authenticated) {
                window.location.href = '/UnAuthorizedUser/UnAuthorizedUser';
            }
            else {
                window.location.href = '/UnAuthenticatedUser/UnAuthenticatedUser';
            }
        }
    });

 });

I don't know how and if it is possible to do so? any help is highly appreciated :)

2 Answers 2

1

Yes, just call it once the new partial has been added to the DOM, in the success callback function.

        success: function (data) {

            $.ajax({
                url: '@Url.Action("_PartialViewTopPlayedTracksList", "ReportStatistic")',
                type: 'GET',
                success: function (data) {
                    $("#PartialViewTopPlayedTracksList").empty();
                    $("#PartialViewTopPlayedTracksList").append(data);
            });

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

6 Comments

This likely won't work, as there won't be anything to apply dataTable to until the _PartialViewTopPlayedTracksList Action loads. He'll need to do a $.get() for _PartialViewTopPlayedTracksList and then apply the dataTable in the success function for that.
@Justin Thanks :) I tried it, but it didn't show the DataTable and stopped other queries from working.
@Dave: Thanks :) - How can I do that? Sorry I am not very familier with jQuery.
@pv619, Dave has spotted it, see my updates above, this loads using a GET and appends the result.
@JustinHarvey - Thanks for taking time to edit the code. I have updated my initial code with the new one. All the queries are working now but the DataTable is not showing up.
|
1

you can initialize datatable, after the partialview is appended on the view in the ajax call complete function like this:

success: function (data) {

            $.ajax({
                url: '@Url.Action("_PartialViewTopPlayedTracksList", "ReportStatistic")',
                type: 'GET',
                success: function (data) {
                    $("#PartialViewTopPlayedTracksList").empty();
                    $("#PartialViewTopPlayedTracksList").append(data);

                    $('#myDataTable').dataTable({ });
            });

    }

2 Comments

thanks :) it worked very much, but after final testing - the query stops other queries from working. i.e. I am also using jTables. Any suggestion? :)
if these plugins are applied on the partial view html that is rendered dynamically, you need to initialize that plugin as well after the ajax call succes

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.