Add support for static methods and defaults overwrite for datatables.net (#20685)

* Add static methods and default overwrites for datatables.net

* Add support for $("selector").dataTable()
* Add support for $.fn.dataTable.XXX and $.fn.dataTable.util.XXX
* Add support for overwriting datatables.net defaults, see test

* Remove fixed comments

* Great improvement for datatable.net-buttons

* Add all supported plugins for buttons
* Better integration in datatables.net d.ts

* Fix Api datatables.net

* Add support for datatables.net-buttons Api calls

Support for button api

* Add support for datatables-fixedheader Api

* Support for fixedHeader Api calls
* Add static functions
* Updated test

* TSLint all files

* TSLint datatables.net, datatables.net-buttoms index.d.ts
* Fix travis errors

* TSLint Tests

* Fix Travis Errors

* Fix: Add TypeScript Version

* Fix: Set TypeScript Version to 4

* Fix: Test error
This commit is contained in:
Kiarash
2017-10-24 16:37:30 +02:00
committed by Andy
parent 1882fc533c
commit df39576b74
8 changed files with 2633 additions and 1855 deletions

View File

@@ -1,40 +1,187 @@
$(document).ready(function () {
var config: DataTables.Settings =
{
// Buttons extension options
buttons: [
{
extend: 'excel',
text: 'Excel',
className: 'class',
filename: "exported_file.csv",
exportOptions: {
columns: ':visible'
}
},
{
extend: 'excel',
text: 'Excel',
className: 'class',
filename: "exported_file.csv",
exportOptions: {
columns: [1, 6, 2, 3, 4]
}
},
{
action: function (e, dt, node, config) { },
available: function (dt, config) { return true; },
destroy: function (dt, node, config) { },
enabled: true,
init: function (dt, node, config) { },
key: 'a',
name: 'name',
namespace: 'namespace',
titleAttr: 'title',
title: 'title'
}
],
const config_1: DataTables.Settings = {
// Buttons extension options
buttons: [{
extend: 'excel',
text: 'Excel',
className: 'class',
filename: "exported_file.csv",
exportOptions: {
columns: ':visible'
}
},
{
extend: 'excel',
text: 'Excel',
className: 'class',
filename: "exported_file.csv",
exportOptions: {
columns: [1, 6, 2, 3, 4]
}
},
{
action(e, dt, node, config) { },
available(dt, config) { return true; },
destroy(dt, node, config) { },
enabled: true,
init(dt, node, config) { },
key: 'a',
name: 'name',
namespace: 'namespace',
titleAttr: 'title',
title: 'title'
}],
};
});
const config_2: DataTables.Settings = {
// Buttons extension options
buttons: {
buttons: [
"csv",
{
extend: "csv",
name: "CSV-Export",
className: "test-class"
},
(dt) => {
return {
extend: "csv",
name: "CSV-Function",
className: "test-class"
};
}
],
}
};
const config_3: DataTables.ButtonsSettings = {
name: "Test",
tabIndex: 1,
buttons: [
"copy",
{
extend: "csv",
name: "CSV-Export",
className: "test-class"
},
(dt) => {
return {
extend: "csv",
name: "CSV-Function",
className: "test-class"
};
},
{
extend: 'colvis',
columnText(dt, idx, title) {
return (idx + 1) + title;
}
},
{
extend: 'colvis',
columns: ':gt(0)'
},
{
extend: 'copy',
text: 'Copy current page',
exportOptions: {
modifier: {
page: 'current'
}
}
},
{
extend: 'csv',
// Name the CSV
filename: 'file_name',
text: 'Customized CSV',
exportOptions: {
columns: [0, 1, $("#name_column"), $("#test_column"), $("#height_column"), $("#area_column")]
},
// Function which customize the CSV (input : csv is the object that you can preprocesss)
customize(csv) {
if (typeof (csv) !== "string") {
return;
}
// Split the csv to get the rows
const split_csv = csv.split("\n");
// Remove the row one to personnalize the headers
split_csv[0] = '"Latitude","Longitude","Site Name","Description","Antenna Height","Antenna gain","Env loss","Candidate"';
// For each row except the first one (header)
$.each(split_csv.slice(1), (index, csv_row) => {
// Split on quotes and comma to get each cell
const csv_cell_array = csv_row.split('","');
// Remove replace the two quotes which are left at the beginning and the end (first and last cell)
csv_cell_array[0] = csv_cell_array[0].replace(/"/g, '');
csv_cell_array[5] = csv_cell_array[5].replace(/"/g, '');
// RANDOM EXAMPLE : Make some test, special cutomizing depending of the value of the cell (if cell 5 is equal to a certain value, give a value to row 6)
if (csv_cell_array[5].toLowerCase().trim() === "a certain value") {
csv_cell_array[6] = "2";
} else if (csv_cell_array[5].toLowerCase() === "another value") {
csv_cell_array[6] = "5";
} else {
csv_cell_array[6] = "";
}
// RANDOM EXAMPLE : Empty the 5th cell and set the 7th to true
csv_cell_array[5] = "";
csv_cell_array[7] = "true";
// Join the table on the quotes and comma; add back the quotes at the beginning and end
const csv_cell_array_quotes = `"${csv_cell_array.join('","')}"`;
// Insert the new row into the rows array at the previous index (index +1 because the header was sliced)
split_csv[index + 1] = csv_cell_array_quotes;
});
// Join the rows with line breck and return the final csv (datatables will take the returned csv and process it)
csv = split_csv.join("\n");
return csv;
}
}
],
dom: {
button: {
active: "active"
}
}
};
// Statics
const buttons = new $.fn.dataTable.Buttons($("selector").DataTable(), config_3);
const version = $.fn.dataTable.Buttons.version;
$.fn.dataTable.Buttons.defaults = {
buttons: []
};
$.fn.dataTable.ext.buttons.collection.className += ' dropdown-toggle';
// API
let dt = $("selector").DataTable();
const export_1 = dt.buttons.exportData({
columns: '',
});
dt.buttons.resize();
dt.button(0).action((e, dt, button, config) => {
console.log('Button activated');
});
dt.button().add(0, {
action: (e, dt, button, config) => {
dt.ajax.reload();
},
text: 'Reload table'
});
dt.button(0)
.nodes()
.css('background', 'blue');
dt.buttons().destroy();

View File

@@ -1,109 +1,272 @@
// Type definitions for JQuery DataTables Buttons extension 1.1.0
// Type definitions for JQuery DataTables Buttons extension 1.4
// Project: http://datatables.net/extensions/buttons/
// Definitions by: Sam Germano <https://github.com/SammyG4Free>, Jim Hartford <https://github.com/jimhartford>
// Definitions by: Kiarash Ghiaseddin <https://github.com/Silver-Connection>, Sam Germano <https://github.com/SammyG4Free>, Jim Hartford <https://github.com/jimhartford>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
// TypeScript Version: 2.4
/// <reference types="jquery" />
/// <reference types="datatables.net"/>
declare namespace DataTables {
export interface Settings {
interface Settings {
/**
* Buttons extension options
*/
buttons?: boolean | string[] | ButtonSettings[];
buttons?: boolean | string[] | ButtonsSettings | ButtonSettings[];
}
//#region "button-settings"
interface StaticFunctions {
Buttons: ButtonStaticFunctions;
}
interface ButtonStaticFunctions {
new (dt: Api, settings: boolean | string[] | ButtonsSettings | ButtonSettings[]): undefined;
version: string;
defaults: ButtonsSettings;
}
interface ExtSettings {
buttons: ExtButtonsSettings;
}
interface Api {
button(groupSelector?: any, buttonSelector?: any): ButtonApi;
buttons: ButtonsGlobalApi;
}
interface ButtonsGlobalApi {
(groupSelector?: any, buttonSelector?: any): ButtonsApi;
/**
* Buttons extension options
*/
export interface ButtonSettings {
/**
* Action to take when the button is activated
*/
* Resize the Flash movie clips to take account of the current button dimensions.
*/
resize(): Api;
/**
* Display / hide an information message to the end user to indicate that something has happened.
*/
info(title: string, message?: string, time?: number): Api;
/**
* Get meta information that is common to many different button types.
*/
exportInfo(options?: ButtonsApiExportInfoParameter): ButtonsApiExportInfoReturn;
/**
* Obtain data from a DataTable that is suitable for exporting by saving into a file or copying to clipboard.
*/
exportData(options?: ButtonsApiExportDataParameter): ButtonsApiExportDataReturn;
}
interface ButtonApi {
/**
* Get the action function for the selected button.
*/
action(): FunctionButtonAction;
/**
* Set the action function for the selected button.
*/
action(set: FunctionButtonAction): Api;
/**
* Get the active state for the selected button.
*/
active(): boolean;
/**
* Set the active state for the selected button.
*/
active(state: boolean): Api;
/**
* Create a new button, adding it to the selected button instance and inserting immediately into the document.
*/
add(index: number | string, config: string|FunctionButtom|ButtonSettings): Api;
/**
* Disable the selected buttons.
*/
disable(): Api;
/**
* Set the enabled state for the selected button.
*/
enable(state?: boolean): Api;
/**
* Get a jQuery object that contains a reference to the node for the selected button.
*/
node(): JQuery;
nodes(): JQuery;
/**
* Determine if a button is currently in the processing state or not.
*/
processing(): boolean;
/**
* Set the processing state for the selected button.
*/
processing(set: boolean): Api;
/**
* Set the processing state for the selected button.
*/
processing(set: boolean): Api;
/**
* Remove the selected button from the display. The button is destroyed and can no longer be used once removed.
*/
remove(): Api;
/**
* Get / Set the display text for the selected button
*/
text(title?: string | FunctionButtonText): Api;
/**
* Programmatically trigger the action of the selected button.
*/
trigger(): Api;
}
interface ButtonsApi extends ButtonApi {
/**
* Get a jQuery instance that contains a reference to the button container instance.
*/
container(): JQuery;
containers(): JQuery;
/**
* Destroy the selected button instances, removing the container and all button elements from the document.
*/
destroy(): Api;
}
interface ButtonsApiExportInfoParameter {
extension?: string | (() => string);
filename?: string | (() => string);
messageBottom?: null | string | (() => string);
messageTop?: null | string | (() => string);
title?: null | string | (() => string);
}
interface ButtonsApiExportInfoReturn {
filename: string;
messageTop: string;
messageBottom: string;
title: string;
}
interface ButtonsApiExportDataParameter {
rows?: any;
columns?: any;
modifier?: any;
orthogonal?: string;
stripHtml?: boolean;
stripNewlines?: boolean;
decodeEntities?: boolean;
trim?: boolean;
format?: any;
}
interface ButtonsApiExportDataReturn {
header: string[];
footer: string[];
body: string[];
}
//#region "Button Settings"
interface ButtonsSettings {
name?: string;
tabIndex?: number;
buttons: Array<string|FunctionButtom|ButtonSettings>;
dom?: ButtonDomSettings;
}
interface ButtonDomSettings {
button?: ButtonDomButtomButton;
buttonContainer?: ButtonDomButtomCommon;
buttonLiner?: ButtonDomButtomCommon;
collection?: ButtonDomButtomCommon;
container?: ButtonDomButtomCommon;
}
interface ButtonDomButtomCommon {
className?: string;
tag?: string;
}
interface ButtonDomButtomButton extends ButtonDomButtomCommon {
active?: string;
disabled?: string;
}
interface ButtomSettingsCommon {
/**
* Action to take when the button is activated
*/
action?: FunctionButtonAction;
/**
* Ensure that any requirements have been satisfied before initialising a button
*/
* Ensure that any requirements have been satisfied before initialising a button
*/
available?: FunctionButtonAvailable;
/**
* Set the class name for the button
*/
* Set the class name for the button
*/
className?: string;
/**
* Function that is called when the button is destroyed
*/
* Function that is called when the button is destroyed
*/
destroy?: FunctionButtonInit;
/**
* Set a button's initial enabled state
*/
* Set a button's initial enabled state
*/
enabled?: boolean;
/**
* Define which button type the button should be based on
*/
* Define which button type the button should be based on
*/
extend?: string;
/**
* Initialisation function that can be used to add events specific to this button
*/
* Initialisation function that can be used to add events specific to this button
*/
init?: FunctionButtonInit;
/**
* Define an activation key for a button
*/
* Define an activation key for a button
*/
key?: string | ButtonKey;
/**
* Set a name for each selection
*/
* Set a name for each selection
*/
name?: string;
/**
* Unique namespace for every button
*/
* Unique namespace for every button
*/
namespace?: string;
/**
* The text to show in the button
*/
text?: string | ButtonText;
* The text to show in the button
*/
text?: string | FunctionButtonText;
/**
* Button 'title' attribute text
*/
* Button 'title' attribute text
*/
titleAttr?: string;
/**
* Button 'title' text
*/
title?: string;
/**
* Define what the exported filename should be
*/
filename?: string;
exportOptions?: ButtonExportOptions;
autoPrint?: boolean;
customize?: FunctionButtonCustomize;
}
export interface FunctionButtonAvailable {
(dt: DataTables.Api, config: any): boolean
}
export interface ButtonExportOptions {
columns?: string | number | string[] | number[];
}
export interface ButtonKey {
interface ButtonKey {
key?: string;
shiftKey?: boolean;
altKey?: boolean;
@@ -111,19 +274,176 @@ declare namespace DataTables {
metaKey?: boolean;
}
export interface ButtonText {
(dt: DataTables.Api, node: JQuery, config: any): string
}
export interface FunctionButtonInit {
(dt: DataTables.Api, node: JQuery, config: any): void
}
// api object?
export interface FunctionButtonAction {
(e: any, dt: DataTables.Api, node: JQuery, config: any): void
/**
* A function that will be executed upon creation of the buttons.
*/
type FunctionButtom = (dt: Api) => ButtomSettingsCommon;
type FunctionButtonText = (dt: Api, node: JQuery, config: any) => string;
type FunctionButtonAvailable = (dt: Api, config: any) => boolean;
type FunctionButtonInit = (dt: Api, node: JQuery, config: any) => void;
type FunctionButtonAction = (e: any, dt: Api, node: JQuery, config: ButtonSettings) => void;
type FunctionButtonCustomize = (win: Window|string) => void;
type FunctionExtButtonsCollectionText = (a: any) => string;
interface ExtButtonsSettings {
collection: ExtButtonsCollectionSettings;
}
export interface FunctionButtonCustomize {
(win: Window): void
interface ExtButtonsCollectionSettings {
action: FunctionButtonAction;
autoClose: boolean;
background: boolean;
backgroundClassName: string;
className: string;
collectionLayout: string;
fade: number;
text: FunctionExtButtonsCollectionText;
}
//#endregion "Button Defaults"
//#region "Add-Ons"
/**
* Buttons extension options
*/
interface ButtonSettings extends ButtomSettingsCommon {
//#region (HTML-)File-Export
/**
* CSV / EXCEL: Define what the exported filename should be
*/
filename?: string;
/**
* COPY / CSV: field separator
*/
fieldSeparator?: string;
/**
* COPY / CSV: field boundary
*/
fieldBoundary?: string;
/**
* COPY / CSV: field separator
*/
newLine?: string;
/**
* CSV / EXCEL / PDF: file extension
*/
extension?: string;
/**
* CSV: UTF-8 boom
*/
bom?: boolean;
/**
* CSV: charset
*/
charset?: string|boolean;
/**
* CSV: escape char
*/
escapeChar?: string;
/**
* EXCEL
*/
customizeData?: FunctionButtonCustomizeData;
/**
* PDF: portrait / landscape
*/
orientation?: string;
/**
* PDF: A3 / A4 / A5 / LEGAL / LETTER / TABLOID
*/
pageSize?: string;
//#endregion (HTML-)File-Export
//#region Export and Print
/**
* COPY / CSV / EXCEL / PDF / PRINT: show header
*/
exportOptions?: ButtonExportOptions | object;
/**
* COPY / CSV / EXCEL / PDF / PRINT: show header
*/
customize?: FunctionButtonCustomize;
/**
* COPY / CSV / EXCEL / PDF / PRINT: show header
*/
header?: boolean;
/**
* COPY / CSV / EXCEL / PDF / PRINT: show footer
*/
footer?: boolean;
/**
* COPY / PRINT: title
*/
title?: string;
/**
* COPY / EXCEL / PDF / PRINT: field separator
*/
messageTop?: string;
/**
* COPY / EXCEL / PDF / PRINT: field separator
*/
messageBottom?: string;
/**
* PDF / PRINT: Extra message
*/
message?: string|Api|JQuery|object;
/**
* PRINT: Show print dialoge on click
*/
autoPrint?: boolean;
//#endregion Export and Print
//#region ColVis
/**
* COLVIS: Column selector
*/
columns?: any;
/**
* COLVIS:
*/
columnText?: FunctionButtonColvisColumnText;
//#endregion ColVis
}
interface ButtonExportOptions {
columns?: string | number | string[] | number[];
}
type FunctionButtonCustomizeData = (content: any) => void;
type FunctionButtonColvisColumnText = (dt: Api, i: number, title: string) => string;
//#endregion "button-settings
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

View File

@@ -1,11 +1,34 @@
$(document).ready(() => {
const config: DataTables.Settings = {
// FixedHeader extension options
fixedHeader: {
footer: true,
footerOffset: 4,
header: true,
headerOffset: 3
}
};
let config_1: DataTables.Settings = {
// FixedHeader extension options
fixedHeader: {
footer: true,
footerOffset: 4,
header: true,
headerOffset: 3
}
};
let dt: DataTables.Api = $("selector").DataTable({
fixedHeader: true
});
// Statics
let fixed_1 = new $.fn.dataTable.FixedHeader(dt, {
footer: true,
});
let fixed_2 = new $.fn.dataTable.FixedHeader(dt, true);
let version = $.fn.dataTable.FixedHeader.version;
$.fn.dataTable.FixedHeader.defaults.footer = false;
// API
dt.fixedHeader.adjust();
dt.fixedHeader.disable();
dt.fixedHeader.enable(true);
// Chaining
dt
.fixedHeader.footerOffset(10)
.fixedHeader.headerOffset(10);

View File

@@ -1,7 +1,9 @@
// Type definitions for datatables.net-fixedheader 3.1
// Project: https://datatables.net/extensions/fixedheader/
// Definitions by: Jared Szechy <https://github.com/szechyjs>
// Definitions by: Jared Szechy <https://github.com/szechyjs>, Kiarash Ghiaseddin <https://github.com/Silver-Connection>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.4
/// <reference types="jquery" />
/// <reference types="datatables.net"/>
@@ -13,6 +15,47 @@ declare namespace DataTables {
fixedHeader?: boolean | FixedHeaderSettings;
}
interface StaticFunctions {
FixedHeader: FixedHeaderStaticFunctions;
}
interface FixedHeaderStaticFunctions {
new (dt: Api, settings: boolean | FixedHeaderSettings): undefined;
version: string;
defaults: FixedHeaderSettings;
}
interface Api {
fixedHeader: FixedHeaderApi;
}
interface FixedHeaderApi {
/**
* Recalculate the position of the DataTable on the page and adjust the fixed element as appropriate.
*/
adjust(): Api;
/**
* Disable the fixed elements
*/
disable(): Api;
/**
* Enable / disable the fixed elements
*/
enable(enable: boolean): Api;
/**
* Get the fixed footer's offset.
*/
footerOffset(offset: number): Api;
/**
* Get the fixed header's offset.
*/
headerOffset(offset: number): Api;
}
/*
* FixedHeader extension options
*/

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }