javascript - 使数据表响应

标签 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,但没有写 Coffee。不管怎样,我不知道该怎么办。

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 初始值设定项中。

最佳答案

盯着代码足够长的时间有时可以解决问题...我基本上可以正常工作,尽管我需要对其进行自定义。替换代码如下:

$(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();
        }
    });

关于javascript - 使数据表响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25228559/

相关文章:

javascript - 为什么我的表格单元格区域上方有多余的空白区域?

jquery - 组合更改、模糊和就绪回调方法

javascript - 如何使用javascript根据输入值突出显示相关数字范围?

mysql - 创建自定义多对多关联 - Ruby on Rails

ruby-on-rails - 在 Ubuntu Server 12.04 上安装 Ruby gem "ffi"时出现问题

javascript - 到底什么才算内联 JavaScript? (回顾速度/效率缓存比较)

javascript - 使用 webpack 和 babel 从 es6 模块导出和更改 javascript 对象

ruby-on-rails - Rails 模型中的两级命名空间

javascript - 如何将数据从指令函数传递到服务?

javascript - li .sibling .fadeTo 问题与 jQuery