AddIns reference
The use case for this concept is TableComponent's colType. Tables are a fundamental piece of dashboards / visualizations and there needs to be simple ways to extend the ways they are rendered
AddIn Implementation
In order to implement an AddIn, you need to create an object like the following:
var sparkline = {
name: "sparkline",
label: "Sparkline",
defaults: {
type: 'line'
},
init: function(){
// Register this for datatables sort
var myself = this;
$.fn.dataTableExt.oSort[this.name+'-asc'] = function(a,b){
return myself.sort(a,b)
};
$.fn.dataTableExt.oSort[this.name+'-desc'] = function(a,b){
return myself.sort(b,a)
};
},
sort: function(a,b){
return this.sumStrArray(a) - this.sumStrArray(b);
},
sumStrArray: function(arr){
return arr.split(',').reduce(function(prev, curr, index, array){
console.log("Current " + curr +"; prev " + prev);
return parseFloat(curr) + (typeof(prev)==='number'?prev:parseFloat(prev));
});
},
implementation: function (tgt, st, opt) {
var t = $(tgt);
t.sparkline(st.value.split(/,/),opt);
t.removeClass("sparkline");
}
};
Dashboards.registerAddIn("Table", "colType", new AddIn(sparkline));
name, label, defaults, init and implementation are the important bits
here. This specific code is an implementation of a sparkline using the jquery plugin.
Setting options
function f(){
// Option 1 :Static list
this.setAddInOptions("colType","sparkline",{barColor: "red"});
// option 2: function
this.setAddInOptions("colType","sparkline",function(state){
// Let's turn the second sparkline into a bar
if(state.colIdx == "2"){
return { type:'bar'};
}
});
}
Setting defaults
Dashboards.setAddInDefaults("Table","colType","sparkline",{fillColor:"#aaa"});
Dashboards.setAddInDefaults("Table","colType","sparkline",function(state){
return state.rowIdx%2?{fillColor:"#aaa"}:{fillColor:"#fff"};
});
Implementation arguments
implementation: function (tgt, st, opt) {}
- tgt
- target - Target for the action. eg: On a cellType, it will be the td cell
- state
- state - Information about the specific addin call. On a cellType will be an object with: {rawData, tableData, colIdx, rowIdx, series, category, value, colFormat}
- opt
- options passed to this addIn
Calling AddIns from components
var addIn = myself.getAddIn("colType",colType);
addIn.call(td,state,myself.getAddInOptions("colType",addIn.getName()));
From this point on, there will be a new colType available to register.Implemented AddIns:
- sparkline
- pvSparkline
- dataBar
- trendArrow
- hyperlink
Here's some screenshots for the implemented addins. More details under cde_samples in the solution repository after installing ctools:
All credits to Brandon Jackson for sponsoring this huge develpment! /bow!




No comments:
Post a Comment