Skip to content

Commit

Permalink
updated metadata to support global column commands
Browse files Browse the repository at this point in the history
- .meta() returns all metadata
- .meta.clear() clears all metadata
- .meta.remove(key) removes key from all metadata (to be used by improved-filters)
  • Loading branch information
kjhangiani committed Jun 2, 2016
1 parent d005c20 commit 751f0b7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ This plugin provides the following additional API calls:

Store column specific metadata in the datatable. This is used by the filtering functions below, but can also be used to store arbitrary data related to the column as required. This data is state saved if state saving is enabled.

- `.meta()` // return all metadata for all columns
- `.meta.clear()` // clear all metadata for all columns
- `.meta.remove(key)` // remove key from all columns metadata

- `.column().meta()` // retrieve meta information for a column
- `.column().meta(key)` // retrieve meta information for a column, under the key `key`
- `.column().meta.replace(data)` // set the entire column meta to the object passed in as `data`
Expand Down
63 changes: 61 additions & 2 deletions js/dataTables.metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
* The primary use of this plugin is in conjunction with datatables-improved-filters which allow column specific filtering
*
* This plugin adds:
* .meta() returns all metadata
* .meta.clear() clears all metadata
* .meta.remove(key) removes key from all metadata
* .column().meta()
* .column().meta(key)
* .column().meta.replace(object)
Expand Down Expand Up @@ -207,6 +210,20 @@ MetaData.defaults = {
columns: [],
};

MetaData._clearAll = function(metaObj) {
var len = metaObj.length;
for (var i = 0; i < len; i ++) {
MetaData._clearData(metaObj, i);
}
};

MetaData._removeAll = function(metaObj, key) {
var len = metaObj.length;
for (var i = 0; i < len; i++) {
MetaData._removeKeyData(metaObj, i, key);
}
};

MetaData._initData = function(metaObj, index) {
if (!metaObj[index]) { metaObj[index] = {}; }

Expand All @@ -227,8 +244,7 @@ MetaData._getKeyData = function(metaObj, index, key) {
};

MetaData._clearData = function(metaObj, index) {
var meta = MetaData._initData(metaObj, index);
meta = null;
metaObj[index] = {};
};

MetaData._replaceData = function(metaObj, index, data) {
Expand Down Expand Up @@ -270,6 +286,9 @@ MetaData.version = '0.3.0';


/**
.meta() return all metadata (metadata.columns)
.meta.clear() null all columns meta data
.meta.remove(key) null key on each column
.column().meta()
.column().meta(key)
.column().meta.replace(object)
Expand Down Expand Up @@ -371,6 +390,46 @@ DataTable.Api.register( 'column().meta.clear()', function() {
return this;
});

DataTable.Api.register( 'meta()', function(key) {
var metadata = this.settings()[0]._metadata;


// we have not loaded the plugin, return this to chain
if (!metadata) {
return null;
}

return $.extend(true, {}, {}, metadata.columns);
});

DataTable.Api.register( 'meta.clear()', function() {
var metadata = this.settings()[0]._metadata;


// we have not loaded the plugin, return this to chain
if (!metadata) {
return this;
}

MetaData._clearAll(metadata.columns);

return this;
});

DataTable.Api.register( 'meta.remove()', function(key) {
var metadata = this.settings()[0]._metadata;


// we have not loaded the plugin, return this to chain
if (!metadata) {
return this;
}

MetaData._removeAll(metadata.columns, key);

return this;
});


/**
.pick(arg1, arg2, arg3...argN)
Expand Down

0 comments on commit 751f0b7

Please sign in to comment.