0

I've been using a modal where i can update user accounts that are in the datatable. I've been trying to figure out how to use ajax to refresh data after submitting the modal but what usually happens, is the table keeps duplicating and ends up crashing the browser before i can even try out the edit account function itself.

datatable from admin.html page I wish to refresh

<div class="x_content">
                <div class="col-md-9" style="padding-left: 0;width: 80%" id="testRefresh">
                     <table id="datatable" class="table table-striped table-bordered projects dt-responsive" style="border:0; width: 100%">
                      <thead>
                        <tr>
                          <th style="text-align: center; width:10%; font-weight: 700">Group</th>
                          <th style="text-align: center; width:10%; font-weight: 700">Member</th>
                          <th style="text-align: center; width:10%; font-weight: 700">Router</th>
                          <th style="text-align: center; width:10%; font-weight: 700">Switch</th>
                          <th style="text-align: center; width:10%; font-weight: 700">Terminal</th>
                        </tr>
                      </thead>
                      <tbody>
                      {% for groups in groups %}
                        <tr>
                          <td>
                            <a href="#editGrp-{{groups.id}}" data-toggle="modal"><strong>{{ groups.name }}</strong></a>
                            <br />
                          </td>
                          <td>
                          <ul class="list-unstyled">
                          {% for user in users %}
                            {% ifequal user.profile.group.id groups.id %}
                            <li><a href="#editAcct-{{user.id}}" data-toggle="modal">{{user.username}}</a></li>
                            {% endifequal %}
                          {% endfor %}
                          </ul>
                        </td>


                        <td>
                        {% for gtd in grouptodevice %}
                         {% ifequal gtd.group.id groups.id %}
                          {% ifequal gtd.type 'AP' %}
                           {% ifequal gtd.device.type 'Router' %}
                            <ul class="allocatedDev-list">
                              <li style="padding-left: 0">                                 
                                <a>
                                  <img src="{% static 'production/images/routerAvatar.png' %}" class="avatar" alt="Avatar" style="margin-right: 6px">
                                  <strong>{{gtd.device.name}}</strong>
                                  <br/>
                                  <small>{{ gtd.startDateTime }} - {{ gtd.endDateTime }}</small>
                                </a>
                              </li>
                            </ul>
                           {% endifequal %}
                          {% endifequal %}
                         {% endifequal %}
                        {% endfor %}
                        </td>
                        <td>
                        {% for gtd in grouptodevice %}
                         {% ifequal gtd.group.id groups.id %}
                          {% ifequal gtd.type 'AP' %}
                           {% ifequal gtd.device.type 'Switch' %}
                            <ul class="allocatedDev-list">
                              <li style="padding-left: 0">                                 
                                <a>
                                  <img src="{% static 'production/images/switchAvatar.png' %}" class="avatar" alt="Avatar" style="margin-right: 6px">
                                  <strong>{{gtd.device.name}}</strong>
                                  <br/>
                                  <small>{{ gtd.startDateTime }} - {{ gtd.endDateTime }}</small>
                                </a>
                              </li>
                            </ul>
                           {% endifequal %}
                          {% endifequal %}
                         {% endifequal %}
                        {% endfor %}
                        </td>
                        <td>
                        {% for gtd in grouptodevice %}
                         {% ifequal gtd.group.id groups.id %}
                          {% ifequal gtd.type 'AP' %}
                           {% ifequal gtd.device.type 'Terminal' %}
                            <ul class="allocatedDev-list">
                              <li style="padding-left: 0">                                 
                                <a>
                                  <img src="{% static 'production/images/terminalAvatar.png' %}" class="avatar" alt="Avatar" style="margin-right: 6px">
                                  <strong>{{gtd.device.name}}</strong>
                                  <br/>
                                  <small>{{ gtd.startDateTime }} - {{ gtd.endDateTime }}</small>
                                </a>
                              </li>
                            </ul>
                           {% endifequal %}
                          {% endifequal %}
                         {% endifequal %}
                        {% endfor %}
                        </td>        
                        </tr>
                      {% endfor %}
                      </tbody>

                    </table>                      
                </div>

javascript ajax script

<script>
  // Refresh the Table every 5 seconds
 function refresh(){
      $.ajax({    
         url: '/ #datatable',
            success: function(data) {
            $('#testRefresh').html(data);
            setTimeout(refresh, 1000);
            }
      });
  }

  setTimeout(refresh, 1000);
</script>
ariseus
  • 13
  • 3
  • Where is the function refresh() called? – rajkris Nov 16 '17 at 06:49
  • What is happening here is that your code actually is that you are actually calling the refresh function in the success of ajax, resulting in a recursive call which gets called infinitely, after each time limit you have set in the timeout. That is the reason why the browser gets crashed.What is your actual requirement here? – rajkris Nov 16 '17 at 06:55
  • @RajKris My requirement is for the datatable to auto-refresh whenever I submit new data – ariseus Nov 16 '17 at 07:01
  • Checkout this: [long polling](https://stackoverflow.com/questions/12776531/long-polling-freezes-browser-and-block-other-ajax-request). It seems you are trying to do the same thing. – rajkris Nov 16 '17 at 07:03

0 Answers0