How to use jQuery to paginate JSON data?

后端 未结 5 1188
迷失自我
迷失自我 2020-12-09 12:47

Duplicate:

Good jquery pagination plugin to use with json Data…

My JSON data looks like this



        
相关标签:
5条回答
  • 2020-12-09 13:05

    You can use the Array.splice-method to create groups of the size you want from the array:

    // Parse json etc.
    var json = [...];
    // Fetch the data for a page from the json-result object
    var page = 1,
        recPerPage = 5,
        // Use Math.max to ensure that we at least start from record 0
        startRec = Math.max(page - 1, 0) * 5,
        // The end index of Array.splice doesn't have to be within boundaries,
        // But if you want to ensure that it does, then use 
        // Math.min(startRec + recPerPage, json.table.length)
        endRec = startRec + recPerPage
        recordsToShow = json.table.splice(startRec, endRec);
    

    recordsToShow now contains an array of records to show for a page. Refactor out page = 1 and take it as a parameter, and do the same for recPerPage = 5, and you should be good to go. You can use jQuery.each to iterate through recordsToShow, and use some kind of templating system to create HTML-elements from each record.

    You should also add some kind of check to startRec to ensure that the starting record is within boundaries. If it is not, then either display page 1, or display an error message to the user.

    0 讨论(0)
  • 2020-12-09 13:10

    You have to be more specific about what you mean by "5 per page". Are you going to be rendering this data to HTML "pages"?

    If so, you need to split the data into groups of 5 and render it..

    0 讨论(0)
  • 2020-12-09 13:18

    If jsonObject is the JSON object, then

    jsonObject.Table[0], jsonObject.Table[1], ... jsonObject.Table[4]
    

    will be the objects of the first page. What you will do with the pages depends on your application.

    0 讨论(0)
  • 2020-12-09 13:29

    jQgrid is a great jQuery plugin for handling tables and paging, it requires json in a very specific format however

    0 讨论(0)
  • 2020-12-09 13:30

    Simple way for JQuery JSON pagination demo https://jsfiddle.net/rijo/0kjow220/

    Html code

    <div style="width:400px">
    <table class="paginated">
        <thead>
            <tr>
                <th class="col">Play Id</th>
                <th class="col">Question1</th>  
           </tr>
      </thead>
      <tbody id="myTable">
     </tbody>
    
    </table>
    <span id="nextValue">next</span><br><span id="PreeValue">Pre</span>
    </div>
    

    Script code

     $(document).ready(function(){
          var table =  $('#myTable');
    var b = [{"play_id":"1","question1":"135","q1r":"1","question2":"138","q2r":"1","question3":"","q3r":"0","question4":"","q4r":"0","total_point":"6","amount":"1.7","bet_amount":"10","winning_amount":"20","no_of_players":"10"},....]
        var max_size=b.length;
         var sta = 0;
         var elements_per_page = 4;
         var limit = elements_per_page;
         goFun(sta,limit);
         function goFun(sta,limit) {
          for (var i =sta ; i < limit; i++) {
    
            var $nr = $('<tr><td>A-' + b[i]['play_id'] + '</td><td>B-' + b[i]['question1']  + '</td></tr>');
            table.append($nr);
          }
          }
          $('#nextValue').click(function(){
    
            var next = limit;
            if(max_size>=next) {
            limit = limit+elements_per_page;
            table.empty();
            console.log(next +' -next- '+limit);
            goFun(next,limit);
            }
          });
          $('#PreeValue').click(function(){
            var pre = limit-(2*elements_per_page);
            if(pre>=0) {
            limit = limit-elements_per_page;
            console.log(pre +' -pre- '+limit);
            table.empty();
            goFun(pre,limit); 
            }
          });
    
        });
    
    0 讨论(0)
提交回复
热议问题