1

I have an HTML table with JSON data which I am rendering on UI in parts.

What I am doing

  • I am calling full data at once via ajax, then dividing the data into 12-12 rows because one page can max have 12 rows as of my requirement
  • I am refreshing the div in each 3 seconds and showing 12-12 rows at one time
  • When page lodes I am showing first 12 rows then after 3 seconds hiding first 12 and showing next 12
  • This is how I am doing My work

What I am trying to achieve

  • When full data is loaded then page shows blank which is not to the requirement

  • I am trying to make that ajax call again when full data is loaded

Working code

$(document).ready(function() {
  myFun();

  function myFun() {
    $.ajax({
      async: true,
      url: "MenuDisplay",
      method: "GET",
      dataType: "json",
      contentType: "application/json; charset=utf-8",
      success: function(tableValue) {

        addTable(tableValue)
        window.setInterval(showRows, 3000);
        showRows();

      }
    });
  }






  function showRows() {
    // Any TRs that are not hidden and not already shown get "already-shown" applies
    if ($(".hidden:lt(12)").length > 0) {
      $("tr:not(.hidden):not(.already-shown)").addClass("already-shown");
    } else {
      $("tr:not(.hidden):not(.already-shown)").addClass("already-shown"); // this one is also calling after 3 seconds after last page i want to call this imidiatly 
      $.ajax({
        async: true,
        url: "MenuDisplay",
        method: "GET",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function(tableValue) {

          addTable(tableValue)


        }
      });
    }

    $(".hidden:lt(12)").removeClass("hidden"); // this one is to hide previous  rows and show next 
  }

  function addTable(tableValue) {
    var $tbl = $("<table />", {
        "class": "table fixed"
      }),
      $tb = $("<tbody/>"),
      $trh = $("<tr/>");

    var split = Math.round(tableValue.length / 4);
    for (i = 0; i < split; i++) {
      $tr = $("<tr/>", {
        class: "hidden "
      });

      for (j = 0; j < 4; j++) {
        $.each(tableValue[split * j + i], function(key, value) {
          if (typeof(value) === "number") {
            $("<td/>", {
              "class": "text-right color" + (j + 1)
            }).html(value).appendTo($tr);
          } else {
            $("<td/>", {
              "class": "text-left color" + (j + 1)
            }).html(value).appendTo($tr);
          }

        });
      }
      $tr.appendTo($tb);
    }
    $tbl.append($tb);
    $("#DisplayTable").html($tbl);
  }



});
tbody>tr>td {
  white-space: normal;
  border-collapse: collapse;
  font-family: Verdana;
  font-weight: bold;
  font-size: .9em;
}

td:nth-child(2),
td:nth-child(4),
td:nth-child(6),
td:nth-child(8) {
  width: 85px;
  max-width: 85px;
  height: 63px
}

.fixed {
  table-layout: fixed;
}

.color1 {
  background: #4AD184;
}

.color2 {
  background: #EA69EF;
}

.color3 {
  background: #E1A558;
}

.color4 {
  background: #F4F065;
}

.hidden,
.already-shown {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<div id="DisplayTable"></div>

Check This fiddle for working example with JSON data

manish thakur
  • 760
  • 9
  • 35
  • 76
  • 1
    Use [setInterval](https://www.w3schools.com/jsref/met_win_setinterval.asp) to call `myFun()` every 3 seconds – Ngoc Nam May 30 '19 at 05:52
  • but in every 3 second i am calling showRows(), once it will be finished then i want to run myFun() – manish thakur May 30 '19 at 05:54
  • 1
    `showRows()` will not change your data/result, because you get data from `myFunc()`. You must add a flag to check all data was loaded or not. – Ngoc Nam May 30 '19 at 05:58
  • @NgocNam my data is loading at once only,whenever page is loading, I am showing data in every 3 seconds from full data with css hide and show – manish thakur May 30 '19 at 06:02
  • I got your idea. Try this format: 1. `loadDataIntoArray()`. 2. `showDataTable(array, index)`. array is data you loaded in first function, then `index=1; setInterval(function(){showDataTable(array, index); index+=12; }, 3000);` – Ngoc Nam May 30 '19 at 06:15
  • one more suggestion: you should use Vue or AngularJs for better code viewing and maintanence. – Ngoc Nam May 30 '19 at 06:18

1 Answers1

2

I checked your fiddle and understood that you need to stop adding the 'already-shown' class in your last page. I have wrapped an if condition here.

if($(".hidden:lt(12)").length > 0){
  $("tr:not(.hidden):not(.already-shown)").addClass("already-shown");
}

You should also consider clearing the setInterval in the else block.

Adding fiddle for your reference https://jsfiddle.net/s7gqe1na/1/

Birju Shah
  • 1,220
  • 9
  • 18
  • in else condition I am calling `myFun` again but that's not rendering properly – manish thakur May 30 '19 at 06:28
  • Something like this? https://jsfiddle.net/s7gqe1na/2/ I have updated the fiddle @manishthakur – Birju Shah May 30 '19 at 06:33
  • Yup but please check my code with `ajax` here in code snippet, i don't want to call that addTable(tableValue) function again i want to call that ajax again so that new data can load – manish thakur May 30 '19 at 06:38
  • When you say it is not rendering properly, you are not getting any error right? I suspect it is because you are not clearing the previous setInterval and creating new interval so now you have two intervals running. – Birju Shah May 30 '19 at 06:42
  • hey i almost got that please check my code snippet here, in `else condition` I am calling ajax again, but issue is after last page it is also taking 3 seconds to call ajax again which i don't want, after last page i want call ajax immediately – manish thakur May 30 '19 at 06:45
  • I have worked out a fiddle similar to your code snippet. Please have a look. https://jsfiddle.net/m2ruqhnd/ – Birju Shah May 30 '19 at 06:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194151/discussion-between-manish-thakur-and-birju-shah). – manish thakur May 30 '19 at 06:53
  • hey can you please check this out once https://stackoverflow.com/questions/56831552/how-to-auto-select-the-dropdown – manish thakur Jul 01 '19 at 09:29