Skip to content

Instantly share code, notes, and snippets.

@felipe-prenholato
Created August 30, 2011 21:07
Show Gist options
  • Save felipe-prenholato/1182049 to your computer and use it in GitHub Desktop.
Save felipe-prenholato/1182049 to your computer and use it in GitHub Desktop.
function handle_forms(event) {
var $triggerObj = $(event.target)
var num = parseInt($("#id_form-TOTAL_FORMS").val())
var $last_row = $("#unidades_inline > tbody > tr:last")
var $triggerObjRow = $triggerObj.parents("tr")
if ($last_row.attr('id') == $triggerObjRow.attr('id')) {
// than we are playing on last line, add new line if have something typed
console.log(event,event.keyCode,'LAST_ROW',$triggerObj.attr('id'),$triggerObj.val(),event.target.value)
if ($triggerObj.val().length > 0) {
$("#unidades_inline > tbody").append(_EMPTY_INLINE_FORM_.replace(/__prefix__/g,num))
$("#id_form-TOTAL_FORMS").val(num+1)
$("#unidades_inline > tbody > tr:last input.trigger").bind('keyup',handle_forms)
$("#unidades_inline > tbody > tr:last").fadeIn(600);
}
} else {
// than we are playing on some other line
console.log(event,event.keyCode,'SOME_ROW',$triggerObj.attr('id'),$triggerObj.val(),event.target.value)
if ($("#unidades_inline > tbody > tr:last input.trigger").val() == "" && $("input.trigger",$("#unidades_inline > tbody > tr:last").prev()).val() == "") {
// if two last inputs are blank, remove the last one
$("#unidades_inline > tbody > tr:last").fadeOut(600,function(){
$("#id_form-TOTAL_FORMS").val(num-1)
$("#unidades_inline > tbody > tr:last input.trigger").unbind('keyup',handle_forms)
$(this).remove()
$("#unidades_inline > tbody > tr:last input.trigger").bind('keyup',handle_forms)
})
}
}
}
console.log($("#unidades_inline > tbody > tr:last input.trigger"));
$("#unidades_inline > tbody > tr:last input.trigger").bind('keyup',handle_forms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment