【问题标题】:DataTable not rendering properly on small deviceDataTable 无法在小型设备上正确呈现
【发布时间】:2019-05-30 19:12:36
【问题描述】:

我有一个正常工作的数据表,但我面临的问题是当我在小型设备上使用同一个表时它不能正常工作

片段

var data = [{
    "amount": 518212,
    "billdate": "2018-08-04",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 104801,
    "billdate": "2018-08-04",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 138151,
    "billdate": "2018-08-04",
    "outlet": "KOLAR"
  },
  {
    "amount": 628358,
    "billdate": "2018-08-05",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 115223,
    "billdate": "2018-08-05",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 134107,
    "billdate": "2018-08-05",
    "outlet": "KOLAR"
  },
  {
    "amount": 177866,
    "billdate": "2018-08-06",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 66095,
    "billdate": "2018-08-06",
    "outlet": "KOLAR"
  },
  {
    "amount": 284069,
    "billdate": "2018-08-07",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 58789,
    "billdate": "2018-08-07",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 67886,
    "billdate": "2018-08-07",
    "outlet": "KOLAR"
  },
  {
    "amount": 313128,
    "billdate": "2018-08-08",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 59939,
    "billdate": "2018-08-08",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 68558,
    "billdate": "2018-08-08",
    "outlet": "KOLAR"
  },
  {
    "amount": 321797,
    "billdate": "2018-08-09",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 64431,
    "billdate": "2018-08-09",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 57352,
    "billdate": "2018-08-09",
    "outlet": "KOLAR"
  },
  {
    "amount": 323556,
    "billdate": "2018-08-10",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 58772,
    "billdate": "2018-08-10",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 43722,
    "billdate": "2018-08-10",
    "outlet": "KOLAR"
  },
  {
    "amount": 464127,
    "billdate": "2018-08-11",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 93110,
    "billdate": "2018-08-11",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 62213,
    "billdate": "2018-08-11",
    "outlet": "KOLAR"
  },
  {
    "amount": 626772,
    "billdate": "2018-08-12",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 126933,
    "billdate": "2018-08-12",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 63119,
    "billdate": "2018-08-12",
    "outlet": "KOLAR"
  },
  {
    "amount": 167391,
    "billdate": "2018-08-13",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 27110,
    "billdate": "2018-08-13",
    "outlet": "KOLAR"
  },
  {
    "amount": 275115,
    "billdate": "2018-08-14",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 58633,
    "billdate": "2018-08-14",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 37920,
    "billdate": "2018-08-14",
    "outlet": "KOLAR"
  }
];
var columndef = [{
  title: "amount",
  data: "amount"
}, {
  title: "billdate",
  data: "billdate"
}, {
  title: "outlet",
  data: "outlet"
}];
$('#tbl').DataTable({
  columns: columndef,
  data: data,
  scrollY: '50vh',
  scrollCollapse: true,
  paging: false
});
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">

<table id="tbl" class="table table-striped table-bordered "></table>

现在在大型设备上发生的事情它工作正常,但是当我在小型设备上使用它时,tbody 部分可以正确水平滚动,但thead 部分是静态的,在ui 上看起来不太好。

要重现该问题,请调整浏览器大小并水平滚动是否有其他资源可供我使用?

【问题讨论】:

  • 您添加了视口标签吗?
  • @ThivankaW 是的,我已经在我的代码中添加了
  • 在使用小型设备时,您的头是否溢出当前视口?
  • @ThivankaW yupp 喜欢这样..你只需检查我的 sn-p,然后调整浏览器大小,你会发现问题

标签: javascript jquery html css datatable


【解决方案1】:

我建议将scrollX: "100%"(如果您使用scrollY,通常需要scrollX)添加到您的datatables 选项以及width: 100% 到您的CSS 表中,这应该可以解决您的问题.请检查下面的代码和底部链接的小提琴。

var data = [{
    "amount": 518212,
    "billdate": "2018-08-04",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 104801,
    "billdate": "2018-08-04",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 138151,
    "billdate": "2018-08-04",
    "outlet": "KOLAR"
  },
  {
    "amount": 628358,
    "billdate": "2018-08-05",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 115223,
    "billdate": "2018-08-05",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 134107,
    "billdate": "2018-08-05",
    "outlet": "KOLAR"
  },
  {
    "amount": 177866,
    "billdate": "2018-08-06",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 66095,
    "billdate": "2018-08-06",
    "outlet": "KOLAR"
  },
  {
    "amount": 284069,
    "billdate": "2018-08-07",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 58789,
    "billdate": "2018-08-07",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 67886,
    "billdate": "2018-08-07",
    "outlet": "KOLAR"
  },
  {
    "amount": 313128,
    "billdate": "2018-08-08",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 59939,
    "billdate": "2018-08-08",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 68558,
    "billdate": "2018-08-08",
    "outlet": "KOLAR"
  },
  {
    "amount": 321797,
    "billdate": "2018-08-09",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 64431,
    "billdate": "2018-08-09",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 57352,
    "billdate": "2018-08-09",
    "outlet": "KOLAR"
  },
  {
    "amount": 323556,
    "billdate": "2018-08-10",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 58772,
    "billdate": "2018-08-10",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 43722,
    "billdate": "2018-08-10",
    "outlet": "KOLAR"
  },
  {
    "amount": 464127,
    "billdate": "2018-08-11",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 93110,
    "billdate": "2018-08-11",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 62213,
    "billdate": "2018-08-11",
    "outlet": "KOLAR"
  },
  {
    "amount": 626772,
    "billdate": "2018-08-12",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 126933,
    "billdate": "2018-08-12",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 63119,
    "billdate": "2018-08-12",
    "outlet": "KOLAR"
  },
  {
    "amount": 167391,
    "billdate": "2018-08-13",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 27110,
    "billdate": "2018-08-13",
    "outlet": "KOLAR"
  },
  {
    "amount": 275115,
    "billdate": "2018-08-14",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 58633,
    "billdate": "2018-08-14",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 37920,
    "billdate": "2018-08-14",
    "outlet": "KOLAR"
  }
];
var columndef = [{
  title: "amount",
  data: "amount"
}, {
  title: "billdate",
  data: "billdate"
}, {
  title: "outlet",
  data: "outlet"
}];
$('#tbl').DataTable({
  columns: columndef,
  "columnDefs": [{
    "className": "dt-left",
    "targets": "_all"
  }],
  data: data,
  scrollY: "50vh",
  responsive: true,
  paging: false,
  scrollX: "100%",
  scrollCollapse: true
});
#tbl {
  width: 100%!important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">

<table id="tbl" class="table table-striped table-bordered "></table>

小提琴:https://jsfiddle.net/bucvf6ek/2/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 2022-06-23
    • 1970-01-01
    相关资源
    最近更新 更多