【问题标题】:rails 3 and jQuery Dialog formrails 3 和 jQuery Dialog 表单
【发布时间】:2011-03-10 14:46:07
【问题描述】:

我谷歌它,但仍然找不到信息或教程。 我有一个与项目的链接,我想点击带有项目的 jquery 对话框表单/编辑该项目并提交。我怎么能实现这个?所有教程都从 html 中获取,我想从视图中获取它,并且字段需要填充现有值。

【问题讨论】:

  • 我想从另一个动作中渲染表单!

标签: javascript ruby-on-rails jquery-ui jquery


【解决方案1】:

对话框中的表单的行为与常规表单一样 - 它们没有什么特别之处。

如果您有一个用于编辑项目的表单,请将其包装在 <div> 中并为其指定一个 ID。 例如:

<div id="your_dialog">
  <% form_for ... %>
     ... rest of form goes here
  <% end %>
</div>

然后你会想在某个地方使用 javascript 设置对话框:

<script>
$(document).ready(function() {
  var $your_dialog = $('#your_dialog').dialog({ 
    autoOpen: false, 
    title: 'Edit',
    modal: true,
    draggable: false
  });
});
</script>

然后将对话框连接到某个链接(可能在您的 public/application.js 中):

$('#open-dialog').click(function(){
    $your_dialog.dialog('open');
});

在您的视图中的某处,您将拥有一个充当打开对话框按钮的元素:

<span id="open-dialog">Click me to open the dialog</span>

如果您希望对话框成为 AJAX 提交,请确保在表单参数中使用 :remote =&gt; true

应该这样做。在尝试将其放入对话框之前确保您的表单有效(即,在尝试连接 javascript 对话框之前完成第一个 div/form)

【讨论】:

  • @rgould +1 你的答案要简单得多。
  • 我想从另一个动作/视图呈现我的表单。假设我目前在项目/索引中,并且想要呈现为对话框视图项目/编辑。我该怎么做,我想另一种选择,只将代码从编辑视图复制/粘贴到索引视图?
  • 获取编辑视图并将form部分重构为部分。您的编辑视图将如下所示:render :partial =&gt; 'form'
  • 然后在索引中你可以为每个“编辑”按钮调用render :partial =&gt; 'form'。这是低效的,但可以用于快速而肮脏的东西。我建议提出另一个问题(并且要具体)以获得更优雅的解决方案。
  • 我有 21 个元素的表格,我需要为 td 中的所有元素呈现表单吗?
【解决方案2】:

下面的很多代码来自http://jqueryui.com/demos/dialog/#modal-form

我希望我在代码中添加的 cmets 能够更好地解释每个部分的功能

    <script>

        //taken from jQueryUI dialog 'form' demo.http://jqueryui.com/demos/dialog/#modal-form
        $(document).ready(function(event){

        var name = $( "#name" ),
                    email = $( "#email" ),
                    password = $( "#password" ),
                    allFields = $( [] ).add( name ).add( email ).add( password ),
                    tips = $( ".validateTips" );
//taken from jQueryUI dialog 'form' demo.http://jqueryui.com/demos/dialog/#modal-form
// validation function message helper
                function updateTips( t ) {
            tips
                .text( t )
                .addClass( "ui-state-highlight" );
            setTimeout(function() {
                tips.removeClass( "ui-state-highlight", 1500 );
            }, 500 );
        }
//taken from jQueryUI dialog 'form' demo.http://jqueryui.com/demos/dialog/#modal-form
// validation function 
        function checkLength( o, n, min, max ) {
            if ( o.val().length > max || o.val().length < min ) {
                o.addClass( "ui-state-error" );
                updateTips( "Length of " + n + " must be between " +
                    min + " and " + max + "." );
                return false;
            } else {
                return true;
            }
        }
          $("#dialog").dialog({
          height: 300,
                    width: 350,
                    modal: true,
                            open : function(event, ui){
                            // not needed b/c of ajax load
    },
                    buttons: {
                        "Save": function() {
                            var bValid = true;
                                            // clear previous validation errors.
                            // perform errror checking on dialog form values            
                            if ( bValid ) {
                                $.post("url/to/save/data", $("#dialog form").serializeArray(), function(data, text, xhr){
    // check for errors saving edits in the 'data' variable
    var hadErrors = true;
    if( data.someWayToCheckForErrors == hadErrors){
       // append an error message to the dialog without closing it, or, append an error message to the main page.
      //$("#dialog").append("<div class='ui-state-error'>you had an error saving</div>");
    }else{
     $( this ).dialog( "close" );
    }
    }); 
                            }
                        },
                        Cancel: function() {
                            $( this ).dialog( "close" );
                        }
                    },
                    close: function() {

                    });



      $(".editLink").click(function(event){
               var theDialog = $("#dialog");
//shorthand ajax call to query server for the Dialog Content in another action.
               theDialog.load("someUrlToGetDataFromYourAction",
               {optional json of url parameters for Action}, 
               function(data, text, jqXhr){
// success 
theDialog.dialog("open");});

          });
        });
        </script>

html 来完成上述工作,同样来自演示 http://jqueryui.com/demos/dialog/#modal-form

<div id="yourContent">
<table><!---whatever is in here.  also, this does not need to be a table.--->
<td><a href="#" class="editLink">edit</a></td>
</tr>
</table>

</div>
        <div id="dialog" title="Create new user">
   <!--- content provided by ajax $.load() call in $('.editLink').click() event.--->
        </div>

【讨论】:

  • 使用 rgould 的答案,如果您想查看可以在对话框本身中执行的所有疯狂验证内容,可以查看我的答案。此外,还有两个选项可以使用数据加载对话框的表单。
  • 如果我在表单上有链接。如何将此表单显示为模式窗口?我不想粘贴 html,我已经有了它,但它被放在另一个动作中
  • 我去掉了多余的 html。编辑链接的 html 非常简单,但真正的“获取对话框表单”工作是由 .load(); 完成的。功能。那是你想要做的吗?验证对话框表单并保存它的其余工作在理论上是相同的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 2011-08-25
相关资源
最近更新 更多