0

PHP からフォーム フィールドを生成できます。

<?php
    echo $this->Form->input('Model.0.name', array(
            'label' => __('Name')
        )
    );
?>

ただし、たとえば、ユーザーが「新しい名前フィールド」をクリックしたときに、フィールドを「オンザフライ」で作成する必要があります。どうやって始めたらいいのかわからない..

4

1 に答える 1

2

Cakephp の動作方法はわかりませんが、Controllers->functions (site.com/controller/function) で動作すると仮定すると、次のようなことができます。

<?

class FieldCreator extends Controller{

    public function input()
    {
        echo $this->Form->input('Model.0.name', array(
            'label' => __('%name%')
            )
        );
    }   
}

JQuery

Jquery は、コントローラgetのように GET リクエストを行うための関数を提供しますFieldCreator。ビューが次のようになっているとします。

<html>
    <head>
            <!-- import jquery framewrok -->
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
    </head>
    <body>

            <!-- This link will act like a button -->
        <a href="#" class="new_input">New name field<a>

        <form id="my_form">

        </form>

            <!-- Prepare the script once the page is ready -->
        <script type="text/javascript">
            $(document).ready(function(){  

                            var content = '';
                            var index = 1;
                            // Use click event in the link above to trigger the request
                $('.new_input').click(function(){ 

                                    if (content == '')
                                    // Do the request to site.com/FieldCreator/input
                    $.get('site.com/FieldCreator/input', function(data){ 
                            content = data;
                    });  

                                   //create the input
                                  createInput('name_' + index);
                });
            });

                    function createInput(name)
                    {
                   var input = content.replace('%name%', name);
                   $('#my_form').append(input);
                       index ++;
                     }




        </script>

    </body>
</html>

ただし、GET の結果を再利用したい場合は、次のことができます。

  1. 'label' => __('Name')に変更'label' => __('%name%')
于 2012-10-01T18:43:14.067 に答える