Last active
August 13, 2018 15:36
-
-
Save gravataLonga/312657ae2de220c014b7e46af1ca8be5 to your computer and use it in GitHub Desktop.
Table Result - Create a table on the fly with js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Usage: | |
* On the template, create the following html. | |
* <table class="table" data-table="dates" data-empty=".is-empty"><thead><tr><th data-key="Id"></th><th data-key="Media.0.LowRs|Media.0.HigRs" data-type="image"></tr></thead></table> | |
* <div class="is-empty" style="display:none;">The result was empty</div> | |
* | |
* Then, on the result of callback ajax request, call like TableResult('dates', result); Tadah!!! | |
* The rest is magic! | |
*/ | |
(function($) { | |
/** | |
* Polyfill isArray | |
*/ | |
if (!Array.isArray) { | |
Array.isArray = function(arg) { | |
return Object.prototype.toString.call(arg) === '[object Array]'; | |
}; | |
} | |
TableResult = function (typeResult, results) { | |
var self = this, | |
$table = $("table.table[data-table='"+typeResult+"']"), | |
$empty = $table.parent().find($table.attr('data-empty')), | |
$thead = $table.find('thead') | |
$tbody = $table.find('tbody'), | |
cells = [], | |
totals = null; | |
if ($table.length<=0) { | |
return; | |
} | |
$table.show().find('tbody tr').remove(); | |
totals = $thead.find('th').length; | |
$.each($thead.find('th'), function (index) { | |
var options = {}, | |
key = $(this).attr('data-key'), | |
orKey = key.indexOf('|')>0 || false, | |
multipleKey = orKey ? key.split('|') : []; | |
if (typeof $(this).attr('data-option') != 'undefined' && $(this).attr('data-option') != '') { | |
options = $(this).attr('data-option'); | |
} | |
cells.push({ | |
key: orKey ? multipleKey : key, | |
type: $(this).attr('data-type'), | |
option: options, | |
orKey: orKey, | |
classes: ($(this).attr('data-class') || '') | |
}); | |
delete options, orKey; | |
}); | |
if (results.length <= 0) { | |
$tr = $("<tr>"); | |
$td = $("<td>").attr('colspan', totals); | |
$empty.clone().show().appendTo($td); | |
$td.appendTo($tr); | |
$tr.appendTo($tbody); | |
return ; | |
} | |
$.each(results, function(index) { | |
var Item = this, | |
$tr = $("<tr>"), | |
dotArray = array_dot(Item); | |
for(var cellIndex in cells) { | |
var Cell = cells[cellIndex], | |
$td = $("<td>"); | |
if (Cell.key != '') { | |
// Key exist on dot notation | |
if (typeof dotArray[Cell.option] != 'undefined') { | |
Cell.option = dotArray[Cell.option]; | |
} | |
// In case the key not exist on dot notation | |
// but exist in key on array. | |
if (typeof Item[Cell.option] != 'undefined') { | |
Cell.option = Item[Cell.option]; | |
} | |
var value = !Array.isArray(Cell.key) ? dotArray[Cell.key] : false; | |
if (!value) { | |
// maybe, maybe is array. | |
value = []; | |
for(var index_key in Cell.key) { | |
value.push(dotArray[Cell.key[index_key]]); | |
} | |
} | |
switch(Cell.type) { | |
case 'image': | |
$td.html(ImageFormat(value, Cell.option)); | |
break; | |
case 'date': | |
$td.html(DateFormat(value, Cell.option)); | |
break; | |
case 'icon': | |
$td.html(IconFormat(value, Cell.option)); | |
break; | |
case 'price': | |
$td.html(PriceFormat(value, Cell.option)); | |
break; | |
case 'count': | |
$td.html(CountFormat(value, Cell.option)); | |
break; | |
default: | |
$td.html(TextFormat(value, Cell.option)); | |
break; | |
} | |
} else { | |
$thead.find('.operations').clone().show().appendTo($td); | |
$td.addClass('text-right'); | |
} | |
$tr.data('item', Item); | |
$td.addClass(Cell.classes); | |
$td.appendTo($tr); | |
delete $td, value; | |
} | |
$tr.appendTo($tbody); | |
delete $tr; | |
delete dotArray; | |
delete Item; | |
}); | |
// Internal functions of DataResult. | |
function ImageFormat(value, option) { | |
if (Array.isArray(value)) { | |
for(var index in value) { | |
if (value[index]) { | |
return $("<img>").attr('src', value[index]).addClass('img-thumbnail').addClass('result-image'); | |
} | |
} | |
} | |
return $("<img>").attr('src', value).addClass('img-thumbnail').addClass('result-image'); | |
} | |
function DateFormat(value, option) { | |
var d = moment(value, option); | |
return d.format('YYYY-MM-DD'); | |
} | |
function TextFormat(value, option) { | |
return value; | |
} | |
function IconFormat(value, option) { | |
$div = $("<div>"); | |
for(var i = 1; i <= value ; i++) { $('<i>').addClass('fa').addClass(option).appendTo($div) }; | |
return $div.append($("<span> x"+value+"</span>")); | |
} | |
function PriceFormat(value, option) { | |
if (option > 0) { | |
value = value * option; | |
} | |
return value + '€'; | |
} | |
function CountFormat(value, option) { | |
return option.length; | |
} | |
} | |
/** | |
* replicates array_dot feature from laravel | |
*/ | |
function array_dot(i, p){ | |
var o = {};p = p || ''; | |
if (p) { p = p+'.'; } | |
if (typeof i == 'object') | |
{ | |
for (var k in i) | |
{ | |
if (i.hasOwnProperty(k)) | |
{ | |
if (typeof i[k] == 'object') | |
{ | |
x = array_dot(i[k],k); | |
for(var l in x) | |
{ | |
o[p+l] = x[l]; | |
} | |
} | |
else | |
{ | |
o[p+k] = i[k]; | |
} | |
} | |
} | |
} | |
return o; | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment