quinta-feira, agosto 06, 2009

Bug quando usa inteiro na ordenação com jQuery - Bug when sorting integers with jQuery

Existe um bug quando ordenamos numero com o uso da biblioteca javascript jQuery, o bug ocorre quando o primeiro item começa com "0". Os números são interpretados como texto e no final você terá uma sequência completamente errada parecida com a sequência abaixo 1, 10, 2, 3, 31 etc. A solução é simples, basta alterar a expressão regular isDigit no arquivo javascript tablesorter.js para a expressão abaixo:


this.isDigit = function(s, config) {
var exp= '/(\0)|(^[+]?0(\.0+)?$)|(^([-+]?[1-9][0-9]*)$)|(^([-+]?((0?|[1-9][0-9]*)\.([0-9]*)))$)|(^[-+]?[1-9]+[0-9]*\.0+$)/';
return RegExp(exp).test($.trim(s.replace(/,/g, '')));

//Has bug when 0 is first digit in list and interprets as text.
//http://groups.google.com/group/jquery-dev/browse_thread/thread/d8e75a30f7ca3067
//var DECIMAL = '\\' + config.decimal;
//var exp = '/(^[+]?0(' + DECIMAL + '0+)?$)|(^([-+]?[1-9][0-9]*)$)|(^([-+]?((0?|[1-9][0-9]*)' + DECIMAL + '(0*[1-9][0-9]*)))$)|(^[-+]?[1-9]+[0-9]*' + DECIMAL + '0+$)/';
//return RegExp(exp).test($.trim(s));
};


There is a bug when sorting numbers when the first item in the table is a "0". The numbers are interpreted as text instead of digits and you will end up with a sequence like 1, 10, 2, 3, 31 etc. I found the solution in this jquery mailing list thread which uses a different regular expression in the isDigit function in the tablesorter.js plugin.

this.isDigit = function(s, config) {
var exp= '/(\0)|(^[+]?0(\.0+)?$)|(^([-+]?[1-9][0-9]*)$)|(^([-+]?((0?|[1-9][0-9]*)\.([0-9]*)))$)|(^[-+]?[1-9]+[0-9]*\.0+$)/';
return RegExp(exp).test($.trim(s.replace(/,/g, '')));

//Has bug when 0 is first digit in list and interprets as text.
//http://groups.google.com/group/jquery-dev/browse_thread/thread/d8e75a30f7ca3067
//var DECIMAL = '\\' + config.decimal;
//var exp = '/(^[+]?0(' + DECIMAL + '0+)?$)|(^([-+]?[1-9][0-9]*)$)|(^([-+]?((0?|[1-9][0-9]*)' + DECIMAL + '(0*[1-9][0-9]*)))$)|(^[-+]?[1-9]+[0-9]*' + DECIMAL + '0+$)/';
//return RegExp(exp).test($.trim(s));
};

quarta-feira, agosto 05, 2009

Zebrado com Smarty Php

Criando um relatório Zebrado com Smarty

No arquivo .php, é preciso apenas setar as cores das linhas.

$smarty = new Smarty();
$smarty->assign("cor_linha", array('#F5F5DC', '#ffffff'));


No arquivo .tpl dentro do foreach é só colocar no tr cycle que automaticamente vai ser
inserido as cores que foram setadas no parâmetro cor_linha

{foreach from=$reports key=id item=rs}

{$rs.attribute1}
{$rs.attribute2}

{foreachelse}

terça-feira, agosto 04, 2009

Setando atributos css com jQuery

$("#idDiv").attr('style','display: block');