Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 1.67 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. jQuery(function($) {
  2.  
  3.     /* Make the form sortable */
  4.     $("form.sortable ul").sortable();
  5.  
  6.     /* On submit, set the value of the position inputs to the value of their current physical position in the list */
  7.     $('form.sortable').submit(function() {
  8.  
  9.         $("form.sortable ul li input.position").each(function(index) {
  10.             $(this).val(index);
  11.          });
  12.  
  13.         return true;
  14.     });
  15.  
  16.  
  17.     /* Bind the + behavior */
  18.     $(".add a").click(function() {
  19.         var num = $("form.sortable ul").children().length + 1;
  20.         var newInput = getNewInputHtml(num);
  21.  
  22.         $("form.sortable ul").append(newInput);
  23.  
  24.         /* Bind the X behavior to the new input element*/
  25.         bindIconCloseEvent();
  26.         return false;
  27.     });
  28.  
  29.     /* Bind the X behavior to the original elements*/
  30.     bindIconCloseEvent();
  31.  
  32. });
  33.  
  34. function bindIconCloseEvent(){
  35.     $(".ui-icon-close").click(function() {
  36.         $(this).parent().children('input.destroy').val(1);
  37.         $(this).parent().hide("slow");
  38.         return false;
  39.     });
  40. }
  41.  
  42. function getNewInputHtml(num){
  43.     var newInput = '' +
  44.             '<li class="clearfix">' +
  45.             '<span class="ui-icon ui-icon-arrowthick-2-n-s"></span>' +
  46.             '<input class="name" name="workflow[workflow_states_attributes][' + num + '][name]" size="30" type="text" value="" />' +
  47.             '<input class="position"name="workflow[workflow_states_attributes][' + num + '][position]" type="hidden" value="" />' +
  48.             '<input class="destroy" name="workflow[workflow_states_attributes][' + num + '][_destroy]" type="hidden" value="false">' +
  49.             '<span class="ui-icon ui-icon-close"></span>' +
  50.             '</li>';
  51.  
  52.     return newInput;
  53. }