Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

precision 0 #9

Closed
petrabarus opened this issue Feb 6, 2015 · 9 comments
Closed

precision 0 #9

petrabarus opened this issue Feb 6, 2015 · 9 comments

Comments

@petrabarus
Copy link

It's rarely we use decimal in my country. So when I tried

 <?=
        $form->field($model, 'defaultPricePerUnit')->widget(\kartik\money\MaskMoney::class,
                [
            'pluginOptions' => [
                'prefix' => 'Rp.',
                'suffix' => '',
                'thousands' => '.',
                'decimal' => ',',
                'affixesStay' => true,
                'precision' => 0,
                'allowZero' => true,
                'allowNegative' => false
            ]
        ]);
?>

I tried inputting 123456789, in the textfield it's Rp.123.456.789, but in the hidden field it's somewhat 123456.789.
Did I put wrong config?

@gharach
Copy link

gharach commented Feb 10, 2015

i have a same problem with precision 0

@gael-wogenstahl
Copy link

Hello !
Same issue here but it's not related to @kartik-v extension.
The problem stands in jquery-maskmoney plugin => see this issue
It seems however that things aren't moving so much on that repository. There are lots of open issues and pull requests waiting...
Will try to find another option for my money input without decimal and post it here.

@ShadesOB
Copy link

This is just a work around. But if your precision is "0", after you output the field you can add js like in the demo below.

$field = $form->field($model, '???');
echo $field->widget(MaskMoney::classname(), [
    'pluginOptions' => [
        'precision' => 0,
        ...
    ]
]);
$this->registerJs('
    $("[name=\''.$field->model->formName().'['.$field->attribute.']'.'\']").on("change", function () {
        var val = $(this).siblings("input").val();
        val = Number(val.replace(/[^0-9\.]+/g,""));
        $(this).val(val);
    });
');

It's not an amazing solution. But it's something.

@petrabarus
Copy link
Author

@ShadesOB: It's good work around actually. 👍 👍 👍

@ShadesOB
Copy link

I realised today that this issue also affects loading the page with data. As in what the value is when loading the page.
Kartik does:

.maskMoney('mask', val);

which doesn't work when you have precision 0;
Instead you have to do:

.val(val);
.maskMoney("mask");

So an update version of my last post would be

$this->registerJs('
    var input = $("[name=\''.$field->model->formName().'['.$field->attribute.']'.'\']");
    input.on("change", function () {
        var val = $(this).siblings("input").val();
        val = Number(val.replace(/[^0-9\.]+/g,""));
        $(this).val(val);
    });
    setTimeout(function(){                          
        var input = $("[name=\''.$field->model->formName().'['.$field->attribute.']'.'\']");                                
        input.siblings("input").val(input.val());
        input.siblings("input").maskMoney("mask");
    },1);
');

As a side question. If anyone can tell me how js scopes work in this example I would appreciate it.
I have the "var input = " in there twice as it didn't inherit the input variable. :s

@anish-adm
Copy link

@ShadesOB , Thanks you very much! It worked like a charm 👍

@wilsonxyz
Copy link

hi, i try to implement @ShadesOB solution, but i still don't understand,

please advice. how to implement to my code.
Thanks before

<?= $form->field($model, 'bruto')->widget(kartik\money\MaskMoney::className(),
            [               
                'pluginOptions' => [
                'suffix' => 'Kg',   
                'precision' => 0,
                ]
]) ?>
$('#poagen-bruto').change(function(){
    calculateNetto();
});`
$('#poagen-tarra').change(function(){
    calculateNetto();
});
function calculateNetto(){  
    var bruto = $('#poagen-bruto').val();           
    var tarra = $('#poagen-tarra').val();   
    $('#poagen-netto').val(bruto-tarra);
};

@ShadesOB
Copy link

First up, apologies as I'm on a new (non yii) project and can't test any of
this. Also it's been some time. :p

There are two problems when helping out with an answer.

  1. Your code has two inputs I know nothing about i.e. "tarra" and "netto"
  2. You didn't actually say what's wrong. Is it simply that it's working but
    you don't understand why?
    Is it that bruto \ tarra are displaying wrong (after change or on page load)
    Is it that bruto \ tarra values are wrong
    Is it that netto isn't updating? (after change or on page load)

But I would try changing:

$('#poagen-netto').val(bruto-tarra); =>
$('#poagen-netto').val(bruto-tarra).trigger("change");

Or doing:

$('#poagen-bruto,#poagen-tarra,#poagen-netto').on("change", function
() { var val = $(this).siblings("input").val(); val =
Number(val.replace(/[^0-9.]+/g,"")); $(this).val(val); });

But again. I'm not in a position to test this. So only guessing.

On Sun, Aug 28, 2016 at 7:10 PM, wilsonxyz [email protected] wrote:

hi, i try to implement @ShadesOB https://github.com/ShadesOB solution,
but i still don't understand,

please advice. how to implement to my code.
Thanks before

field($model, 'bruto')->widget(kartik\money\MaskMoney::className(), [ 'pluginOptions' => [ 'suffix' => 'Kg', 'precision' => 0, ] ]) ?>

$('#poagen-bruto').change(function(){
calculateNetto();
});`
$('#poagen-tarra').change(function(){
calculateNetto();
});
function calculateNetto(){
var bruto = $('#poagen-bruto').val();
var tarra = $('#poagen-tarra').val();
$('#poagen-netto').val(bruto-tarra);
};


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#9 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AD9xM9GG4r7YepH2zLQkj_7RkPW1ZiTUks5qkVCMgaJpZM4Dcm9p
.

@kartik-v
Copy link
Owner

Resolved via updates & enhancements to release v1.2.2. Duplicate to #26, #23, #22, #16.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants