1

categoriessubcategoriesproduct_typesおよびの4 つのテーブルがありproductsます。それぞれは、次の階層で互いに関連付けられています。

categories
|- subcategories
   |- product_types
      |- products

add()の行動観はProductsController

        <?= $this->Form->create($product, ['type' => 'file', 'class' => 'ajax_page']) ?>
            <fieldset>
                <legend><?= __('Add Product') ?> <div class="ajax_loading_image"></div></legend>
                <?php
                    echo $this->Form->input('category_id', ['options' => $categories, 'empty' => true]);
                    echo $this->Form->input('subcategory_id', ['options' => $subcategories]);
                    echo $this->Form->input('product_type_id', ['options' => $productTypes]);
                    echo $this->Form->input('product_code');
                    echo $this->Form->input('SKU');
                    echo $this->Form->input('title');
                    echo $this->Form->input('description');
             </fieldset>
   <?php echo $this->Form->end(); ?>

現在、それはリスト全体subcategoriesのリストです。Ajaxの変更と使用のsubcategories変更時にロードしたい。categoriesproduct_typessubcategories

私が見つけることができる CakePHP 3.x の良い例はありません。

CakePHP 3 での実装方法。私は CakePHP と Ajax も初めてです。

ありがとうございました。

4

1 に答える 1

3

以下のようなctpファイル。ここではまず、フィールドのカテゴリ、サブカテゴリ、製品タイプ、および製品コードに id を指定します。

<?= $this->Form->create($product, ['type' => 'file', 'class' => 'ajax_page']) ?>
    <fieldset>
        <legend><?= __('Add Product') ?> <div class="ajax_loading_image"></div></legend>
        <?php
                echo $this->Form->input('category_id', ['options' => $categories, 'empty' => true,'id'=>'categories']);
                echo $this->Form->input('subcategory_id', ['options' => '$subcategories','id'=>'subcategories']);
                echo $this->Form->input('product_type_id', ['options' => '$productTypes','id'=>'producttype']);
                echo $this->Form->input('product_code',['options'=>'$productcode','id'=>'productcode']);
                echo $this->Form->input('SKU');
                echo $this->Form->input('title');
                echo $this->Form->input('description');
    </fieldset>     <?php echo $this->Form->end(); ?>

サブカテゴリの ajax 呼び出しは次のとおりです。product_type と product code に対して同じ ajax 呼び出しを作成できます

<script>
    $("#categories").on('change',function() {
        var id = $(this).val();

        $("#subcategories").find('option').remove();
        if (id) {
            var dataString = 'id='+ id;
            $.ajax({
                dataType:'json',
                type: "POST",
                url: '<?php echo Router::url(array("controller" => "yourcontroller", "action" => "youraction")); ?>' ,
                data: dataString,
                cache: false,
                success: function(html) {
                    //$("#loding1").hide();
                    $.each(html, function(key, value) {              
                        //alert(key);
                        //alert(value);
                        //$('<option>').val('').text('select');
                        $('<option>').val(key).text(value).appendTo($("#subcategories"));

                    });
                } 
            });
        }
    });

このコードから、チェーンされたドロップダウン メニューを取得できます。あなたのためのその仕事。

于 2016-08-02T06:38:05.813 に答える