Skip to content

Instantly share code, notes, and snippets.

@simkimsia
Created August 18, 2013 04:02
Show Gist options
  • Select an option

  • Save simkimsia/6259835 to your computer and use it in GitHub Desktop.

Select an option

Save simkimsia/6259835 to your computer and use it in GitHub Desktop.
This is how we make the form input fields in CakePHP more like Excel Currency Format cells http://stackoverflow.com/a/1162744/80353
<div class="voucherCategories container_16">
<div class="grid_16" style="text-align: center" >
<?php echo $this->element('products_link_bar'); ?>
<h2><?php echo __('Edit ' . $this->Form->value('VoucherCategory.name')); ?></h2>
</div>
<?php echo $this->Form->create('VoucherCategory'); ?>
<fieldset class="grid_16">
<?php
echo $this->Form->input('id');
echo $this->Form->input('year_id');
echo $this->element('select_projects', array('projects' => $allProjects, 'selected' => $selectedProjects));
echo $this->Form->input('name');
echo $this->Form->input('site_count');
echo $this->Form->input('net_total_after_payment_terms', array('class' => "niceCurrencyFormat", 'ref' => $this->Form->value('VoucherCategory.net_total_after_payment_terms'),
'type' => 'text'));
echo $this->Form->input('voucher_value', array('class' => "niceCurrencyFormat", 'ref' => $this->Form->value('VoucherCategory.voucher_value'),
'type' => 'text'));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<?php $this->append('scriptBottom'); ?>
<script type="text/javascript">
$(document).ready(function() {
$.each($(".niceCurrencyFormat"), function (index, value){
var originalNumber = $(value).attr('ref');
var niceNumber = '$' + formatNumber(originalNumber, 0, 2, true);
$(value).val(niceNumber);
});
$(".niceCurrencyFormat").focus(function() {
$(this).val($(this).attr('ref'));
});
$(".niceCurrencyFormat").focusout(function() {
var originalNumber = $(this).attr('ref');
var niceNumber = '$' + formatNumber(originalNumber, 0, 2, true);
$(this).val(niceNumber);
});
$(".niceCurrencyFormat").keyup(function(event) {
var currentValue = $(this).val();
$(this).attr('ref', currentValue);
});
$("#VoucherCategoryEditForm").on('submit',function (){
$.each($(".niceCurrencyFormat"), function (index, value){
var originalNumber = $(value).attr('ref');
$(value).val(originalNumber);
});
});
});
</script>
<?php $this->end('scriptBottom'); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment