views:

190

answers:

0

I am using the jQuery UI Dialog to show a ajax form inside a dialog box.

I have two problems with the code:

  1. after form submit, i want that the dialog is closed automatically.
  2. If the user uses the submit from the form (e.g. pressing enter), or uses the submit button from the dialog, the effect should be the same.

For now the submit only works when pressing enter inside the text-field. I can't close the dialog from within the form_remote_tag, and i can't submit the form from within the buttons-declaration (for the dialog).

I have seen examples where the ajax-code is placed together with the buttons-code, and that would definitely work, but is there a way to use the form_remote_tag as is?

Here is the code of the view, including the javascript:

<% javascript_tag do %>

  function showCloseActionLinePopup(actionline_id) {
    $j("#close_tha_ident").val(actionline_id);
    $j("#popup_window").dialog('open');
    return false;
  }

  $j(document).ready(function() {
    $j("#popup_window").dialog({
      autoOpen: false, height: 200, width: 400, modal: true,
      buttons : {
        '<%= t("actionline.close")  %>': function() {
          $j(this).closest('form').submit();
          $j(this).dialog('close');
        },
        '<%= t("cancel") %>': function() {
          $j(this).dialog('close');
        }
      },
      close: function() {}
    });
  });
<% end %>    

<div id="popup_window" class="dialog" title="<%= t 'actionline.close_title' %>">
  <h3><%= t 'actionline.close_title' %></h3>
  <% spinner_id="actionline_close_spinner" %>
  <% form_id="actionline-close" %>
  <% update_div="tab1" %>
  <div id="<%=form_id%>" class="light-borderd">
    <%- form_remote_tag :url => {:controller => :threats, :action => :close_actionline},
               :before => "Element.show('#{spinner_id}')",
               :loaded => "Element.hide('#{spinner_id}'); $j('##{form_id}').children('form').reset();",
               :complete => "Element.hide('#{spinner_id}'); $j('#popup_window').dialog('close');",
               :update => update_div do -%>
      <div>
        <label for="close-comment"><%= t 'actionline.comment' %></label>
        <%= text_field_tag "close[comment]", nil, :name => "close_comment" %>
        <%= hidden_field_tag "close[tha_ident]", 0, :name => "close_tha_ident" %>
        <%= hidden_field_tag "id", @record.id %>
      </div>
      <div>
        <%#= submit_tag t("actionline.close") %>
        <%= image_tag("/images/working.gif",
           :align => "middle",
           :border => 0,
           :id => spinner_id,
           :style => "display:none;") %>
      </div>
    <%- end -%>
  </div>
</div>

and in a table, containing each possible item for which this form can be called, i have the line

              <%= link_to_function get_icon('close'), "showCloseActionLinePopup('#{actionline.tha_ident}');" if is_open_actionline %>