0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

DataTables を使うまでメモ

Posted at

前提

  • Rails に DataTables を導入
    • 手軽に導入するため CDN を使う

導入

対象画面の View ファイルで DataTables を読み込む

app/views/test.html.haml
= stylesheet_link_tag 'https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css'
= javascript_include_tag 'https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js'

...

%table.table.table-bordered#datatables-use-id
  %thead
    %tr

...

application.js で全 JS ファイルを読み込んでいる(//= require_tree .)ので、対象画面でのみ DataTable を使うように設定する(そのままだと全 View で DataTable を使おうとして、読み込んでないのでエラーになる)
対象 View に対応する JS ファイルを用意する

app/assets/javascripts/test.js
$(function () {
  var datatables_use_id = '#datatables-use-id'

  if ($(datatables_use_id).length > 0) {
    $(datatables_use_id).DataTable();
  }
});

設定

デフォルトでも十分使えるが、設定も色々と行える

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?