我一直在尝试使用分页建立dc.js数据表。如何修复``对dc.table的分页''?

时间:2019-06-27 05:03:13

标签: javascript d3.js dc.js crossfilter

我正在尝试向dc.datatable添加分页。

分页不起作用,也不显示页面。

使用此代码将显示整个数据表,并且仅在表末尾显示“ next”和“ prev”按钮,但这些按钮不起作用。

如何解决在一页显示20行的分页?

我的代码

 <div class="chart col-sm-12" id="all-characters-div">
            <div class="content-wrapper">
                <h2>Comic Characters</h2>
                <table id="all-characters"> </table>
            </div>
        </div>
        <div id="pagination">
            <span id="start"></span>-<span id="end"></span> of <span id="totalSize"></span>
            <button id="prev" class="btn" onclick="prev()">Prev</button>
            <button id="next" class="btn" onclick="next()">Next</button>
        </div>

function makeGraphs(error, charactersData) {
    var ndx = crossfilter(charactersData);

show_listCharacters(ndx);
       updateResult();
         dc.renderAll();

 function show_listCharacters(ndx) {  
   var dataTable = dc.dataTable("#all-characters");

   var dim = ndx.dimension(dc.pluck("name"));
 // console.log(dim.top(Infinity));
       dataTable
      .dimension(dim)
      .group(function(d) {
        return "";
      })
      .columns(["name", "urlslug", "first appearance"])
      .size(Infinity)
      .sortBy(dc.pluck("name")) 
      .order(d3.ascending)
      .transitionDuration(1000);
       }
  /*-----------------Table Pagination-----------*/
          var resultStart = 0; var resultEnd =21;
          var ndx;
          var dataTable = dc.dataTable;

          function displayResult() {

            document.getElementById("start").innerHTML = resultStart;
            document.getElementById("end").innerHTML = resultStart + resultEnd-1;

            document.getElementById("totalSize").innerHTML = ndx.size();


            d3.select('#prev').attr('disabled', resultStart-resultEnd < 0 ? 'true' : null);
            d3.select('#next').attr('disabled', resultStart+resultEnd >= ndx.size() ? 'true' : null);
        }
          function updateResult() {

            dataTable.beginSlice(resultStart);
            dataTable.endSlice(resultStart + resultEnd);
            displayResult();

        }
        function prev() {
          resultStart -= resultEnd;
          updateResult();
          dataTable.redraw();
        }
          function next() {
            resultStart += resultEnd;
            updateResult();
            dataTable.redraw();
        }

1 个答案:

答案 0 :(得分:1)

查看该项目中的代码:https://tttp.eu我有分页和可排序的列

相关部分

 function drawPagination(){

  var ofs = 0, pag = 10;
  chart.on('preRedraw.pagination', display);

  function display() {
      d3.select('#begin')
          .text(1+ofs);
      d3.select('#end')
          .text(ofs+pag);
      d3.selectAll('#next,#last')
          .attr('disabled', ofs+pag>=graphs.total.data() ? 'true' : null);
      d3.selectAll('#first,#prev')
          .attr('disabled', ofs<=0 ? 'true' : null);
      d3.select('#size').text(graphs.total.data());
  }

  function update() {
      chart.beginSlice(ofs);
      chart.endSlice(ofs+pag);
      chart.redraw();
  }

  function next() {
      ofs += pag;update();
  }
  function prev() {
      ofs -= pag;
      if (ofs <=0) {
        d3.selectAll("#prev,#first").attr("disabled",true);
        ofs=0;
      }
      update();
  }

  d3.selectAll("#pagination")
    .html (`
        Showing <span id="begin"></span>-<span id="end"></span> of <span id="size"></span><span class="btn-group">
        <button id="first" disabled="disabled" class="btn btn-primary"><i class="glyphicon glyphicon-fast-backward"></i>First</button>
        <button id="prev" disabled="disabled" class="btn btn-primary"><i class="glyphicon glyphicon-step-backward"></i>Prev</button>
        <button id="next" class="btn btn-primary">Next<i class="glyphicon glyphicon-step-forward"></i></button>
        <button id="last" class="btn btn-primary">Last<i class="glyphicon glyphicon-fast-forward"></i></button>
        </span>
    `);
  d3.select("#first").on("click",function(){ofs=0;update()});
  d3.select("#last").on("click",function(){ofs=graphs.total.data()-pag;update()});
  d3.select("#prev").on("click",prev);
  d3.select("#next").on("click",function(){ofs +=pag;update()});

  update();
}