Combine Double Initialization of Table - Deep Linking

Combine Double Initialization of Table - Deep Linking

jkallajkalla Posts: 6Questions: 2Answers: 0

Error messages shown: "...Cannot reinitialize datatable..."
Description of problem: I have two scripts that need to be combined, but I'm not sure how to do it.

$(document).ready( function () {
    $('#myTable').DataTable({
        language: {
            search: "Filter records: "
        },
        dom: 'Bfrtip',
        buttons: [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ],
        bLengthChange:true,
        bInfo:true,
        bPaginate:true,
        order: [[2, "desc"]],
        search: {
            return: false
        }
    });
} );

$('#myTable').DataTable( $.fn.dataTable.ext.deepLink( [
    'search.search', 'order', 'displayStart'
] ) );

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    Answer ✓

    I think you will need to use the Extending defaults example code. Something like this:

    $(document).ready( function () {
        var defaultOptions = {
            language: {
                search: "Filter records: "
            },
            dom: 'Bfrtip',
            buttons: [
                'copy', 'csv', 'excel', 'pdf', 'print'
            ],
            bLengthChange:true,
            bInfo:true,
            bPaginate:true,
            order: [[2, "desc"]],
            search: {
                return: false
            }
        };
    
     
        var searchOptions = $.fn.dataTable.ext.deepLink( [
            'search.search', 'order', 'displayStart'
        ] );
    
    
        $('#myTable').DataTable(
            $.extend( defaultOptions, searchOptions );
        );
    
    } );
    

    Kevin

  • jkallajkalla Posts: 6Questions: 2Answers: 0

    Thanks, @kthorngren! It didn't work, and I'm not smart enough to troubleshoot. I'll see if I can implement it on this network and post a link.

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    edited November 2023 Answer ✓

    If you look at the browser's console you will see an error similar to this:

    Uncaught SyntaxError: missing ) after argument list

    Its a syntax error. The ; at the end of $.extend( defaultOptions, searchOptions ); shouldn't be there. Just remove the trailing semicolon.

    The error is in the deeplining docs. @allan will need to remove the semicoln.

    Here is a running example of your code:
    https://live.datatables.net/lakepinu/1/edit

    This shows loading with page 2:
    https://live.datatables.net/lakepinu/1?displayStart=10

    Kevin

  • jkallajkalla Posts: 6Questions: 2Answers: 0

    @kthorngren! Thanks! That fixed it for me.

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Oops - thanks for flagging that Kevin. I've corrected it in source control now and will deploy the fix soon.

    Allan

Sign In or Register to comment.