volosoft / jtable

A JQuery plugin to create AJAX based CRUD tables.
http://www.jtable.org
1.1k stars 506 forks source link

Decimal separator #535

Open javibishop opened 11 years ago

javibishop commented 11 years ago

I need to use the pont "." to separate decimal, its only works when i use the comma. Where is the function which converts , to .? or how to format currency fields? Javier

crynos commented 11 years ago

I use this to Currency (Brazilian Real format - R$ 10.258,55) :

    foreach ($rows as &$val) 
    {

        $val['value'] = "R$ ".number_format((double)$val['value'],2,",",".");
    }
alejandri commented 11 years ago

where put this lines?????

crynos commented 11 years ago

Inside your PHP file ... just below the:

//Add all records to an array $rows = array(); while($row = mysql_fetch_array($result)) { $rows[] = $row; }

HERE

Where ['value'] = the name of your DB field (ex: currency, price, cost, ...)

alejandri commented 11 years ago

thanks Crynos ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡

crynos commented 11 years ago

By the way: You will have to treat the string when update.

What i mean is, your database field probably will be float or double... like 9999.99 so, you will change the Array to show the value like 9.999,99 (or 9,999.99) . When you edit the row, the value will be 9.999,99 (or $ 9,999.00 , or something else). So you'll need to treat the string back to float (9999.99)

crynos commented 11 years ago

To convert it back to float format, i did this:

    $value = str_replace('R$ ','',$_POST['value']); //remove the R$
           $value = str_replace('.','',$value); //replacing the '.' to nothing
    $value = str_replace(',','.',$value); //replacing the ',' to '.'

inside the else if($_GET["action"] == "update") {...}

Just to remember, my currency format is like 99.999,99, thats why i did this replacements. EUA currency is diferent and will only need to replace the thousands separator

Must be a better way, but it works for me.

crynos commented 11 years ago

Finally a better way (Issue #15) (Thank you - @Runamok81).

And then, format the number as you wish (Examples at the download site). I'm using this to Brazilian Currency:

value: { title: 'Valor da Nota/Recibo', display: function (data) { return 'R$ ' + number_format(data.record.value, 2, ',', '.'); } }

To treat the update form, i'm using a MASK script (http://plentz.github.io/jquery-maskmoney/) (Issue #313 - Thanks @botoko), to force the number format to be the same as the database (float) when i update the row:

fields: { ...}, formCreated: function (event, data) { data.form.find('[name=value]').maskMoney({thousands:'', decimal:'.'}); }

And again, you can change the mask as you wish to , examples at the download site.

Hope this can help ya.