Redisplay of DataTable causes an error

Redisplay of DataTable causes an error

GlyndwrGlyndwr Posts: 128Questions: 35Answers: 1

I had an earlier post trying to get DataTables to work for a different page. This was eventually solved. I now want to re-display a page when a button is selected. When the page is initially displayed archived events are not displayed. If the user clicks on the "Archived Hidden" button then the name changes to "Archived Shown" all activities (including archived) are retrieved and displayed. The initial display works fine; however, when I re-display the page I get the error:

    Uncaught TypeError: $(...).DataTable is not a function
    at displayActivities (e1ActivitySelect-ajax.js:13)
    at HTMLAnchorElement.<anonymous> (e1ActivitySelect-ajax.js:80)
    at HTMLAnchorElement.dispatch (jquery.min.js:3)
    at HTMLAnchorElement.r.handle (jquery.min.js:3)
    displayActivities @ e1ActivitySelect-ajax.js:13
    (anonymous) @ e1ActivitySelect-ajax.js:80
    dispatch @ jquery.min.js:3
    r.handle @ jquery.min.js:3

The ajax is:

    $(document).ready(function(){
        $("#includedContent").load("Menu.html");

        $('[data-toggle="tooltip"]').tooltip();

        displayActivities();

    }); // end document.ready

    function displayActivities() {
        $('#ajaxGetUserServletResponse1').text('');

        var activityTable = $('#activityTable').DataTable( {
            "info":     false,
            dom: 'Bfrtip',
            buttons: ['copy', 'csv', 'excel', 'pdf', 'print'],
            columns: [
                      {data: 'adActivityID'},
                      {data: 'adActivityName'},
                      {data: 'adStartDate'},
                      {data: 'adEndDate'},
                      {
                          data: null,
                          className: "center",
                          defaultContent: '<a href="" class="editor_edit">Update</a> / <a href="" class="editor_copy">Copy</a> / <a href="" class="editor_list">View Participants</a>'
                      }
                      ]
        } );

        var dataToBeSent  = {
                ssAccountLevel : sessionStorage.getItem('ssAccountLevel'),
                ssAccountID : sessionStorage.getItem('ssAccountID'),
                ssArchived : sessionStorage.getItem('ssArchived'),
        };

        //Joined
        $.ajax({
            url : 'E1ActivitySelectView', // Your Servlet mapping or JSP(not suggested)
            data : dataToBeSent, 
            type : 'POST',
        })
        .fail (function(jqXHR, textStatus, errorThrown) {
            //alert(jqXHR.responseText);
            if(jqXHR.responseText.includes('No Activities')){
                $('#ajaxGetUserServletResponse1').text('No Activities.');
            }else{
                if(jqXHR.responseText.includes('Invalid user')){
                    $('#ajaxGetUserServletResponse1').text('Invalid user.');
                }else{
                    $('#ajaxGetUserServletResponse1').text('Error getting activities.');
                }
            }
            $("#startDate").focus();
        })
        .done(function(responseJson1a){
            // JSON response to populate the activities table
            dataType: "json";

    //      alert(JSON.stringify(responseJson1a)); 
    //      Result of alert is:
    //          [{"adId":"2","grpId":"2","adActivityID":"2","adActivityName":"Visit Ryde Fire Station","adStartDate":"2017-05-24","adEndDate":"2017-05-24"}]
            activityTable.clear();
            activityTable.rows.add(responseJson1a).draw();
        })
    }

    $('#archivedHidden').click(function(){

        if($(this).text().trim() == 'Archived Hidden' ){
            $(this).text('Archived Shown');
            sessionStorage.setItem('ssArchived', "Shown");
            $('#img-container').empty();
        }else{
             $(this).text('Archived Hidden');
             sessionStorage.setItem('ssArchived', "Hidden");
             $('#img-container').empty();
        }

        displayActivities();  
    });

    $('#addActivity').click(function(){        
        window.location = "E1ActivityCreate.html";
    });

Replies

  • GlyndwrGlyndwr Posts: 128Questions: 35Answers: 1

    The HTML is:

        <!DOCTYPE html>
    
        <html>
        <head>
            <title>Activity Select</title>
            <meta http-equiv=Content-Type content="text/html; charset=utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
            <meta name="description" content="A Scout award tracking application">
            <meta name="author" content="Glyndwr (Wirrin) Bartlett">
    
            <script src="resources/jquery/jquery.js"></script>
            <script src="resources/jquery/jquery.validate.js"></script>
            <script src="resources/jquery/jquery.validate.min.js"></script>
            <script src="resources/jquery/additional-methods.js"></script>
            <script src="resources/jquery/additional-methods.min.js"></script>
    
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
            <script src="https://cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>   
            <script src="https://cdn.datatables.net/buttons/1.5.1/js/dataTables.buttons.min.js"></script>
            <script src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.flash.min.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/pdfmake.min.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/vfs_fonts.js"></script>
            <script src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.html5.min.js"></script>
            <script src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.print.min.js"></script>
    
    
    
            <!-- Le styles -->
            <link href="resources/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
            <link href="resources/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet" />
            <link href="resources/bootstrap-3.3.7-dist/css/bootstrap-theme.min.css" rel="stylesheet" />
            <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.0/bootstrap-table.css" />
            <link rel="stylesheet" href="https://cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css">
            <link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.5.1/css/buttons.dataTables.min.css">
    
            <style type="text/css">
              body {
                padding-top: 40px;
                padding-bottom: 40px;
                background-color: #f5f5f5;
              }
              . {
                max-width: 370px;
                padding: 1px 1px 1px;
                background-color: #fff;
                border: 1px solid #e5e5e5;
                -webkit-border-radius: 5px;
                   -moz-border-radius: 5px;
                        border-radius: 5px;
                -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05);
                   -moz-box-shadow: 0 1px 2px rgba(0,0,0,.05);
                        box-shadow: 0 1px 2px rgba(0,0,0,.05);
              }
    
              .form-select {
                max-width: 100%;
                padding: 19px 29px 29px;
                background-color: #fff;
                border: 1px solid #e5e5e5;
                -webkit-border-radius: 5px;
                   -moz-border-radius: 5px;
                        border-radius: 5px;
                -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05);
                   -moz-box-shadow: 0 1px 2px rgba(0,0,0,.05);
                        box-shadow: 0 1px 2px rgba(0,0,0,.05);
              }
              .form-signin .form-heading,
              .form-signin .checkbox {
                margin-bottom: 10px;
              }
              .form-signin input[type="text"],
              .form-signin input[type="password"] {
                font-size: 16px;
                height: auto;
                margin-bottom: 15px;
                padding: 7px 9px;
              }
              .myheader {
                display:flex;
                text-align: center;
              }
              .myimg {
                width: 130px;
                height: auto;
              }
              .pull-right {
                text-align: right;
              }
    
                /* Tooltip container */
    
                /* Tooltip text */
                .tooltiptext {
                    visibility: hidden;
                    width: 120px;
                    background-color: #555;
                    color: #fff;
                    text-align: center;
                    padding: 5px 0;
                    border-radius: 6px;
    
                    /* Position the tooltip text */
                    position: absolute;
                    z-index: 1;
                    bottom: 125%;
                    left: 50%;
                    margin-left: -60px;
    
                    /* Fade in tooltip */
                    opacity: 0;
                    transition: opacity 1s;
                }
    
                /* Tooltip arrow */
                .tooltiptext::after {
                    content: "";
                    position: absolute;
                    top: 100%;
                    left: 50%;
                    margin-left: -5px;
                    border-width: 5px;
                    border-style: solid;
                    border-color: #555 transparent transparent transparent;
                }
    
                /* Show the tooltip text when you mouse over the tooltip container */
                .tooltiptext {
                    visibility: visible;
                    opacity: 1;
                }
    
            </style>
            <link href="resources/bootstrap-3.3.7-dist/css/bootstrap-responsive.css" rel="stylesheet">
    
            <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
            <!--[if lt IE 9]>
              <script src="resources/js/html5shiv.js"></script>
            <![endif]-->
    
            <!-- Fav and touch icons -->
    
          </head>
    
            <div id="selectE1Activity"  class="container-fluid" style="background-repeat: repeat; background-image: url('images/body-bg.jpg');"> 
    
                <div id="includedContent"></div>
    
                <form data-toggle="validator" role="form" id="showActivityTableForm">
                    <div class="row">
                        <div class="container-fluid">
                            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                                <div class="jumbotron">
                                    <h3>Activity List</h3>
    
                                    <div class="input-group">
                                        <div class="col-lg-6 col-md-6 col-sm-6 col-xs-1 text-left">
                                            <div class="submit">
                                              <a id="archivedHidden" class="btn btn-primary" data-toggle="collapse" href="#collapseExample">Archived Hidden</a>
                                            </div>
                                        </div>
                                        <div class="col-lg-6 col-md-6 col-sm-6 col-xs-1 text-right">   
                                            <div class="submit">
                                              <a id="addActivity" class="btn btn-primary" data-toggle="collapse">Add Activity</a>
                                            </div>
                                        </div>
                                    </div>
    
                                    <div class="container" id=activity>
                                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                                            <table class="table table-hover table-bordered" id="activityTable">
                                                <thead>
                                                    <tr>
                                                      <th>ID</th>
                                                      <th>Activity</th>
                                                      <th>Start Date</th>
                                                      <th>End Date</th>
                                                      <th>Update / Copy / View Participants</th>
                                                    </tr>
                                                </thead>
                                                <tbody id="activityTablebody">
                                                </tbody>
                                            </table>
                                            <div style="text-align: center;">
                                                <span id="ajaxGetUserServletResponse1" style="color: red;"></span>
                                            </div>
                                        </div>
    
                                    </div><!-- /container -->
    
                                </div>
                            </div>
                        </div>
                    </div><!-- /row -->
    
                    <div style="text-align: center;">
                        <span id="ajaxGetUserServletResponse" style="color: red;"></span>
                    </div>
                </form>
            </div> <!-- /container -->
    
            <div id="ajaxGetUserServletResponse"></div>
    
            <!-- Le javascript
            ================================================== -->
            <!-- Placed at the end of the document so the pages load faster -->
            <script src="resources/bootstrap-3.3.7-dist/js/bootstrap-tooltip.js"></script>
            <script src="resources/bootstrap-3.3.7-dist/js/bootstrap-popover.js"></script>
            <script src="https://cdn.datatables.net/buttons/1.5.1/js/dataTables.buttons.min.js"></script>
            <script src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.flash.min.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/pdfmake.min.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/vfs_fonts.js"></script>
            <script src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.html5.min.js"></script>
            <script src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.print.min.js"></script>
    
            <script src="js/e1ActivitySelect-ajax.js"></script>
    
          </body>
        </html>
    
  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    You are loading jQuery at least twice on the page - possibly a third time if it is in the e1ActivitySelect-ajax.js file. Load jQuery only once.

    Allan

  • GlyndwrGlyndwr Posts: 128Questions: 35Answers: 1

    Hi Allan,

    Thank you very much for your reply. The line e1ActivitySelect-ajax.js is the name of the ajax module I load as per the code above. I have update the html to ensure I am not loading jQuery more than once. I am not a programmer and am doing this for my Scout Group so please bear with my lack of knowledge. The new html is:

        <!DOCTYPE html>
    
        <html>
        <head>
            <title>Activity Select</title>
            <meta http-equiv=Content-Type content="text/html; charset=utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
            <meta name="description" content="A Scout award tracking application">
            <meta name="author" content="Glyndwr (Wirrin) Bartlett">
    
            <!-- JQuery -->
                <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
                <!-- Validate -->
                <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>
                <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/additional-methods.min.js"></script>
    
                <!-- Bootstrap -->
                <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
    
                <!-- Bootstrap Date Picker-->
                <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>    
                <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/js/bootstrap-datepicker.min.js"></script>
    
                <!-- DataTables -->
                <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
    
    
                <!-- Le styles -->
                <!-- Bootstrap -->
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    
                <!-- DataTables -->
                <link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
                <link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.5.1/css/buttons.dataTables.min.css">
    
            <style type="text/css">
              body {
                padding-top: 40px;
                padding-bottom: 40px;
                background-color: #f5f5f5;
              }
              . {
                max-width: 370px;
                padding: 1px 1px 1px;
                background-color: #fff;
                border: 1px solid #e5e5e5;
                -webkit-border-radius: 5px;
                   -moz-border-radius: 5px;
                        border-radius: 5px;
                -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05);
                   -moz-box-shadow: 0 1px 2px rgba(0,0,0,.05);
                        box-shadow: 0 1px 2px rgba(0,0,0,.05);
              }
    
              .form-select {
                max-width: 100%;
                padding: 19px 29px 29px;
                background-color: #fff;
                border: 1px solid #e5e5e5;
                -webkit-border-radius: 5px;
                   -moz-border-radius: 5px;
                        border-radius: 5px;
                -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05);
                   -moz-box-shadow: 0 1px 2px rgba(0,0,0,.05);
                        box-shadow: 0 1px 2px rgba(0,0,0,.05);
              }
              .form-signin .form-heading,
              .form-signin .checkbox {
                margin-bottom: 10px;
              }
              .form-signin input[type="text"],
              .form-signin input[type="password"] {
                font-size: 16px;
                height: auto;
                margin-bottom: 15px;
                padding: 7px 9px;
              }
              .myheader {
                display:flex;
                text-align: center;
              }
              .myimg {
                width: 130px;
                height: auto;
              }
              .pull-right {
                text-align: right;
              }
    
                /* Tooltip container */
    
                /* Tooltip text */
                .tooltiptext {
                    visibility: hidden;
                    width: 120px;
                    background-color: #555;
                    color: #fff;
                    text-align: center;
                    padding: 5px 0;
                    border-radius: 6px;
    
                    /* Position the tooltip text */
                    position: absolute;
                    z-index: 1;
                    bottom: 125%;
                    left: 50%;
                    margin-left: -60px;
    
                    /* Fade in tooltip */
                    opacity: 0;
                    transition: opacity 1s;
                }
    
                /* Tooltip arrow */
                .tooltiptext::after {
                    content: "";
                    position: absolute;
                    top: 100%;
                    left: 50%;
                    margin-left: -5px;
                    border-width: 5px;
                    border-style: solid;
                    border-color: #555 transparent transparent transparent;
                }
    
                /* Show the tooltip text when you mouse over the tooltip container */
                .tooltiptext {
                    visibility: visible;
                    opacity: 1;
                }
    
            </style>
            <link href="resources/bootstrap-3.3.7-dist/css/bootstrap-responsive.css" rel="stylesheet">
    
            <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
            <!--[if lt IE 9]>
              <script src="resources/js/html5shiv.js"></script>
            <![endif]-->
    
            <!-- Fav and touch icons -->
    
          </head>
    
          <body>
    
            <div id="selectE1Activity"  class="container-fluid" style="background-repeat: repeat; background-image: url('images/body-bg.jpg');"> 
    
                <div id="includedContent"></div>
    
                <form data-toggle="validator" role="form" id="showActivityTableForm">
                    <div class="row">
                        <div class="container-fluid">
                            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                                <div class="jumbotron">
                                    <h3>Activity List</h3>
    
                                    <div class="input-group">
                                        <div class="col-lg-6 col-md-6 col-sm-6 col-xs-1 text-left">
                                            <div class="submit">
                                              <a id="archivedHidden" class="btn btn-primary" data-toggle="collapse" href="#collapseExample">Archived Hidden</a>
                                            </div>
                                        </div>
                                        <div class="col-lg-6 col-md-6 col-sm-6 col-xs-1 text-right">   
                                            <div class="submit">
                                              <a id="addActivity" class="btn btn-primary" data-toggle="collapse">Add Activity</a>
                                            </div>
                                        </div>
                                    </div>
    
                                    <div class="container" id=activity>
                                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                                            <table class="table table-hover table-bordered" id="activityTable">
                                                <thead>
                                                    <tr>
                                                      <th>ID</th>
                                                      <th>Activity</th>
                                                      <th>Start Date</th>
                                                      <th>End Date</th>
                                                      <th>Update / Copy / View Participants</th>
                                                    </tr>
                                                </thead>
                                                <tbody id="activityTablebody">
                                                </tbody>
                                            </table>
                                            <div style="text-align: center;">
                                                <span id="ajaxGetUserServletResponse1" style="color: red;"></span>
                                            </div>
                                        </div>
    
                                    </div><!-- /container -->
    
                                </div>
                            </div>
                        </div>
                    </div><!-- /row -->
    
                    <div style="text-align: center;">
                        <span id="ajaxGetUserServletResponse" style="color: red;"></span>
                    </div>
                </form>
            </div> <!-- /container -->
    
            <div id="ajaxGetUserServletResponse"></div>
    
            <!-- Le javascript
            ================================================== -->
            <!-- Placed at the end of the document so the pages load faster -->
            <script src="resources/bootstrap-3.3.7-dist/js/bootstrap-tooltip.js"></script>
            <script src="resources/bootstrap-3.3.7-dist/js/bootstrap-popover.js"></script>
            <script src="https://cdn.datatables.net/buttons/1.5.1/js/dataTables.buttons.min.js"></script>
            <script src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.flash.min.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/pdfmake.min.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/vfs_fonts.js"></script>
            <script src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.html5.min.js"></script>
            <script src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.print.min.js"></script>
    
            <script src="js/e1ActivitySelect-ajax.js"></script>
    
          </body>
        </html>
    
  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    I don't immediately see what is going wrong there I'm afraid. Can you give me a link to a page showing the issue so I can help to debug it. If you don't want to make the link public, send me a PM by clicking my forum user name above and then "Send message".

    Allan

This discussion has been closed.