Modification to fnGetColumnData plugin to strip HTML.
Modification to fnGetColumnData plugin to strip HTML.
Hey all,
So when using fnGetColumnData() along with the based individual column filter, I came across an instance where I needed to have the content stripped of HTML tags. This was on an 'html' type column. To get around this, I added an additional default parameter, bFilterHtml, which will filter html for any column of the 'html' type. It'd be pretty trivial to do similarly for the 'num-html' and similar types. Questions? Ridicule? Feel free to comment. I'm always open to suggestions!
P.S. Also modified slightly to handle AMD-style loading, per various comments floating around in the forum.
[code]
/**
* @fileOverview
* DataTables plugin for allowing us to get all data in a column for our table
* Altered to filter out html tags if type is html and bFilterHtml is true
*
* @see http://www.datatables.net/plug-ins/api#fnGetColumnData
*
* @author Benedikt Forchhammer
* @author dlakes
* @version $Id$
*/
(function (root, factory) {
// Set up DT_bootstrap appropriately for the environment.
if (typeof define === 'function' && define.amd) {
// AMD
define(['jquery', 'dataTables'], function ($) {
factory($);
});
} else {
// Browser globals
factory(root.jQuery);
}
}(this, function ($) {
$.fn.dataTableExt.oApi.fnGetColumnData = function ( oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty, bFilterHtml ) {
// check that we have a column id
if ( typeof iColumn == "undefined" ) return new Array();
// by default we only want unique data
if ( typeof bUnique == "undefined" ) bUnique = true;
// by default we do want to only look at filtered data
if ( typeof bFiltered == "undefined" ) bFiltered = true;
// by default we do not want to include empty values
if ( typeof bIgnoreEmpty == "undefined" ) bIgnoreEmpty = true;
//by default we want to filter out HTML elements
if( typeof bFilterHtml == "undefined" ) bFilterHtml = true;
// list of rows which we're going to loop through
var aiRows;
// use only filtered rows
if (bFiltered == true) aiRows = oSettings.aiDisplay;
// use all rows
else aiRows = oSettings.aiDisplayMaster; // all row numbers
// set up data array
var asResultData = new Array();
var iRow;
var columnType;
var sValue;
var $valObj;
for (var i=0,c=aiRows.length; i -1) continue;
// else push the value onto the result data array
else asResultData.push(sValue);
}
return asResultData;
}
}));
[/code]
So when using fnGetColumnData() along with the based individual column filter, I came across an instance where I needed to have the content stripped of HTML tags. This was on an 'html' type column. To get around this, I added an additional default parameter, bFilterHtml, which will filter html for any column of the 'html' type. It'd be pretty trivial to do similarly for the 'num-html' and similar types. Questions? Ridicule? Feel free to comment. I'm always open to suggestions!
P.S. Also modified slightly to handle AMD-style loading, per various comments floating around in the forum.
[code]
/**
* @fileOverview
* DataTables plugin for allowing us to get all data in a column for our table
* Altered to filter out html tags if type is html and bFilterHtml is true
*
* @see http://www.datatables.net/plug-ins/api#fnGetColumnData
*
* @author Benedikt Forchhammer
* @author dlakes
* @version $Id$
*/
(function (root, factory) {
// Set up DT_bootstrap appropriately for the environment.
if (typeof define === 'function' && define.amd) {
// AMD
define(['jquery', 'dataTables'], function ($) {
factory($);
});
} else {
// Browser globals
factory(root.jQuery);
}
}(this, function ($) {
$.fn.dataTableExt.oApi.fnGetColumnData = function ( oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty, bFilterHtml ) {
// check that we have a column id
if ( typeof iColumn == "undefined" ) return new Array();
// by default we only want unique data
if ( typeof bUnique == "undefined" ) bUnique = true;
// by default we do want to only look at filtered data
if ( typeof bFiltered == "undefined" ) bFiltered = true;
// by default we do not want to include empty values
if ( typeof bIgnoreEmpty == "undefined" ) bIgnoreEmpty = true;
//by default we want to filter out HTML elements
if( typeof bFilterHtml == "undefined" ) bFilterHtml = true;
// list of rows which we're going to loop through
var aiRows;
// use only filtered rows
if (bFiltered == true) aiRows = oSettings.aiDisplay;
// use all rows
else aiRows = oSettings.aiDisplayMaster; // all row numbers
// set up data array
var asResultData = new Array();
var iRow;
var columnType;
var sValue;
var $valObj;
for (var i=0,c=aiRows.length; i -1) continue;
// else push the value onto the result data array
else asResultData.push(sValue);
}
return asResultData;
}
}));
[/code]
This discussion has been closed.
Replies