Skip to content

Instantly share code, notes, and snippets.

@Shadow6363
Created August 12, 2011 19:59
Show Gist options
  • Save Shadow6363/1142865 to your computer and use it in GitHub Desktop.
Save Shadow6363/1142865 to your computer and use it in GitHub Desktop.
var historyCount = parseInt($('#historyCount').val());
$('#AddMaintenance').click(function(event) { event.preventDefault();
var firstMaintenance = parseInt($('#FreezerFirstMaintenance').val());
$('#preventativeMaintenance').clone(false, false).html($('#preventativeMaintenance').html()).find(':input').each(function() {
$(this).attr('id', $(this).attr('id').replace(firstMaintenance, historyCount));
$(this).attr('name', $(this).attr('name').replace('[' + firstMaintenance + ']', '[' + (historyCount) + ']'));
if($(this).is('[type!="hidden"]')) { $(this).val(''); }
}).end().insertBefore('#AddMaintenance'); historyCount++;
});
<fieldset style="border: none;">
<legend id="preventativeMaintenanceLegend">
<span class="ui-icon ui-icon-triangle-1-e" id="preventativeMaintenanceArrow" style="display: inline-block;"></span>
<span>Preventative Maintenance Information</span>
</legend>
<div id="preventativeMaintenances" style="display: none; margin-left: 2.5%;">
<div id="preventativeMaintenance">
<?php
/* THIS IS THE IMPORTANT PART:
You need a hidden field that keeps track of whatever your first hard-coded form is.
In the above JS, you need the var firstMaintenance = stuff to be named the same.
*/
echo $this->Form->hidden('firstMaintenance', array('value' => 0));
echo $this->Form->hidden('EquipmentHistory.0.subject', array('value' => '1'));
echo $this->Form->input('EquipmentHistory.0.date', array('empty' => true));
echo $this->Form->input('EquipmentHistory.0.details');
?>
</div>
<?php for($i = 4; $i < count($this->data['EquipmentHistory']); $i++): ?>
<?php if($this->data['EquipmentHistory'][$i]['subject'] == 1): ?>
<div id="preventativeMaintenance">
<?php
echo $this->Form->hidden('EquipmentHistory.' . $i . '.subject', array('value' => '1'));
echo $this->Form->input('EquipmentHistory.' . $i . '.date', array('empty' => true));
echo $this->Form->input('EquipmentHistory.' . $i . '.details');
?>
</div>
<?php endif; ?>
<?php endfor; ?>
<button id="AddMaintenance">Add Preventative Maintenance Information</button>
</div>
</fieldset>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment