浏览数据表

Browserify DataTables

我尝试按照 NPM packages - DataTables 中的说明进行操作,但我仍然无法将其与 Browserify 捆绑在一起。

这是我的最小、完整且可验证的示例:

app.js

'use strict'

var $ = require('jquery')
var dt = require('datatables.net-dt')()

$(document.getElementById('table')).DataTable()

npm list

的输出
foo@1.0.0 /home/RinkAttendant6/www/foo
├─┬ datatables.net@1.10.10
│ └── jquery@2.1.4
└── datatables.net-dt@1.5.2

browserify app.js -o bundle.js

的输出
Error: Cannot find module 'datatables.net-dt' from '/home/RinkAttendant6/www/foo'
    at /usr/lib/node_modules/browserify/node_modules/resolve/lib/async.js:46:17
    at process (/usr/lib/node_modules/browserify/node_modules/resolve/lib/async.js:173:43)
    at ondir (/usr/lib/node_modules/browserify/node_modules/resolve/lib/async.js:188:17)
    at load (/usr/lib/node_modules/browserify/node_modules/resolve/lib/async.js:69:43)
    at onex (/usr/lib/node_modules/browserify/node_modules/resolve/lib/async.js:92:31)
    at /usr/lib/node_modules/browserify/node_modules/resolve/lib/async.js:22:47
    at FSReqWrap.oncomplete (fs.js:82:15)

我做错了什么?

根据软件包供应商的说法,除非使用 Bootstrap 或 Foundation,否则应使用 datatables.net 软件包而不是 datatables.net-dt

参考https://github.com/DataTables/DataTables/issues/434#issuecomment-161278064

Instead use:

var dt = require('datatables.net')()

The reason for this is that the datatables.net-dt package does not contain a Javascript file - it doesn't need one - it contains only CSS (it should actually contain a couple of images as well, that will be corrected in 1.10.11).

No Javascript file is required there since the DataTables defaults are suitable for DataTables styling. The same is not the case for Bootstrap, etc.

使用那个 github 问题线程中的另一个建议对我有用。

参见:https://github.com/DataTables/DataTables/issues/434#issuecomment-113286718

var DataTable = require('datatables.net')()

$.fn.DataTable = DataTable

该代码在有或没有数据表的 browserify-shim 条目的情况下都有效。