numString

針對 Regex 中任何位置包含的數字值進行排序。

此外掛程式將僅針對 Regex 中任何位置包含的數字值進行排序。這對於需要將一些額外上下文包含在表格中的資料排序很有用。

使用

此外掛程式可以通過多種不同的方式取得和使用。

瀏覽器

此外掛程式可在 DataTables CDN 上取得

JS

然後,此外掛程式會自動向全域 DataTables 實例註冊。如果您使用 AMD 載入器(例如 Require.js),也可以使用此檔案。

請注意,如果您使用多個外掛程式,則將這些外掛程式組合到單個檔案並將其託管在您自己的伺服器上,而不是向 DataTables CDN 發出多個請求,在效能方面可能會有好處。

NPM

所有外掛程式都可以在 NPM 上取得(也可以與 Yarn 或任何其他 Javascript 套件管理器一起使用),作為 datatables.net-plugins 套件的一部分。要使用此外掛程式,請先安裝外掛程式套件

npm install datatables.net-plugins

ES 模組

然後,如果您使用的是 ES 模組,請匯入 datatables.net、您需要的任何其他 DataTables 擴充功能以及此外掛程式

import DataTable from 'datatables.net';
import 'datatables.net-plugins/sorting/numString.mjs';

CommonJS

如果您使用 Node 的 CommonJS 載入器(例如,使用較舊版本的 Webpack 或非模組 Node 程式碼),請使用以下方法 require 此外掛程式

var $ = require('jquery');
var DataTable = require('datatables.net');
require('datatables.net-plugins/sorting/numString.js');

範例

// This example shows a mixture of text and number values, with the number at the start of the expression.
// It is using regex and start and end of expression tags.
// It will match "5 examples completed." for example.
DataTable.numString(/^\d+ examples? completed.$/);
var table = $('#example').DataTable();
// This example shows a mixture of text and number values, with the number at the end of the expression.
// It is using regex and start and end of expression tags.
// It will match "Examples left: 67" for example.
DataTable.numString(/^Examples? left: \d+$/);
var table = $('#example').DataTable();
// This example shows a mixture of text and number values, with the number in the middle of the expression.
// It is using regex and no start and end of expression tags.
// It will match "Only 1 left." for example.
DataTable.numString(/Only \d+ left./);
var table = $('example').DataTable();

版本控制

如果您對如何改進此外掛程式有任何想法,或發現任何錯誤,它可以在 GitHub 上取得,並且非常歡迎提交 pull request!