使DataTable具有响应性

时间:2014-08-10 12:21:49

标签: javascript jquery ruby-on-rails datatables

环境:Ruby 2.0.0,Rails 4.0.3,Windows 8.1更新,jquery-datatables-rails 2.2.1,jquery-ui-rails 5.0.0,jquery-rails 3.1.1

我已启动并运行DataTables,但我需要让我的表响应。我对这样做的说明感到困惑。我写了一些JavaScript但不是咖啡。无论哪种方式,我都不确定该怎么做。

jquery-datatables-rails指令说:

5 - Initialize your datatables using:

responsiveHelper = undefined
breakpointDefinition =
  tablet: 1024
  phone: 480

tableElement = $("#example")
tableElement.dataTable
  autoWidth: false
  preDrawCallback: ->

    # Initialize the responsive datatables helper once.
    responsiveHelper = new ResponsiveDatatablesHelper(tableElement, breakpointDefinition)  unless responsiveHelper
    return

  rowCallback: (nRow) ->
    responsiveHelper.createExpandIcon nRow
    return

  drawCallback: (oSettings) ->
    responsiveHelper.respond()
    return

我目前正在初始化我的表:

$(document).ready(function () {
    // Enable any datatables
    $('#datatable').dataTable({
        "sPaginationType": "full_numbers",
        bJQueryUI: true
    });
    $('#carstable').dataTable({
        "sPaginationType": "full_numbers",
        bJQueryUI: true,
        bProcessing: true,
        bServerSide: true,
        sAjaxSource: $('#carstable').data('source')
    });

我使用compileonline.com生成JavaScript:

(function() {
  var breakpointDefinition, responsiveHelper, tableElement;

  responsiveHelper = void 0;

  breakpointDefinition = {
    tablet: 1024,
    phone: 480
  };

  tableElement = $("#example");

  tableElement.dataTable({
    autoWidth: false,
    preDrawCallback: function() {
      if (!responsiveHelper) {
        responsiveHelper = new ResponsiveDatatablesHelper(tableElement, breakpointDefinition);
      }
    },
    rowCallback: function(nRow) {
      responsiveHelper.createExpandIcon(nRow);
    },
    drawCallback: function(oSettings) {
      responsiveHelper.respond();
    }
  });

}).call(this);

但我无法看到我需要做些什么来将其集成到我当前的JavaScript初始化程序中。

1 个答案:

答案 0 :(得分:1)

长时间盯着代码有时可以解决问题...我基本上工作了,但我需要自定义它。替换代码如下:

$(document).ready(function() {
    var breakpointDefinition, responsiveHelper, tableElement;
    responsiveHelper = void 0;
    breakpointDefinition = {
        tablet: 1024,
        phone: 480
    };
    tableElement = $("#datatable");
    tableElement.dataTable({
        autoWidth: false,
        "sPaginationType": "full_numbers",
        bJQueryUI: true,
        preDrawCallback: function() {
            if (!responsiveHelper) {
                responsiveHelper = new ResponsiveDatatablesHelper(tableElement, breakpointDefinition);
            }
        },
        rowCallback: function(nRow) {
            responsiveHelper.createExpandIcon(nRow);
        },
        drawCallback: function(oSettings) {
            responsiveHelper.respond();
        }
    });
    tableElement = $("#carstable");
    tableElement.dataTable({
        autoWidth: false,
        "sPaginationType": "full_numbers",
        bJQueryUI: true,
        bProcessing: true,
        bServerSide: true,
        sAjaxSource: $('#carstable').data('source'),
        preDrawCallback: function() {
            if (!responsiveHelper) {
                responsiveHelper = new ResponsiveDatatablesHelper(tableElement, breakpointDefinition);
            }
        },
        rowCallback: function(nRow) {
            responsiveHelper.createExpandIcon(nRow);
        },
        drawCallback: function(oSettings) {
            responsiveHelper.respond();
        }
    });