【jQuery】表の操作について ~ DataTables 編 ~

■ はじめに

 * Bootstrap4 で表を扱ってて、たまたま DataTables を見つけて、
   試しに使ってみたらよかったので、メモ。
  => Bootstrap4 対応は助かる

■ サンプル

<!DOCTYPE html>
<html lang="jp">
<head>
    <meta charset="UTF-8">
    <title>datatebles site</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap4 -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="">https://code.jquery.com/jquery-3.3.1.js">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    <!--  datatables -->
    <link rel="stylesheet" href="">https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css"/>
    <link rel="stylesheet" href="">https://cdn.datatables.net/responsive/2.2.3/css/responsive.bootstrap4.min.css"/>
    <script src="">https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js">
    <script src="">https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js">
    <script src="">https://cdn.datatables.net/responsive/2.2.3/js/dataTables.responsive.min.js">
</head>
<body>
<div class="container">
    <div class="col-xs-12">
        <table id="sampleTable"  class="table table-striped table-bordered dt-responsive nowrap">
            <thead>
            <tr>
                <th>#</th>
                <th>氏名</th>
                <th>メールアドレス</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td>1</td>
                <td>山本 太郎</td>
                <td>dummy.taro@example.com</td>
            </tr>
            <tr>
                <td>2</td>
                <td>小田 次郎</td>
                <td>dummy.jiro@example.com</td>
            </tr>
            </tbody>
        </table>
    </div>
</div>
<script>
    $(function(){
        // datatableの設定を変更
        $("#sampleTable").DataTable({
            "language": {
                "url": "https://cdn.datatables.net/plug-ins/1.10.16/i18n/Japanese.json"
            },
            pageLength: 50
        });
    });
</script>
</body>
</html>

■ DataTables あれこれ

特定の列をソートさせないようにするには...

orderable: false にする
$('#example').dataTable( {
  "columnDefs": [
    { "targets": 0, "orderable": true },  // ソートを有効
    { "targets": 1, "orderable": true },  // ソートを有効
    { "targets": 2, "orderable": false }, // ソートを無効 ★
    { "targets": 3, "orderable": false }  // ソートを無効 ★
  ]
});
API仕様
https://datatables.net/reference/option/columns.orderable


■ 関連記事

jQuery】表の操作について ~ tablesorter 編 ~

https://blogs.yahoo.co.jp/dk521123/25076367.html