1

I have 4 tables categories, subcategories, product_types and products. Each is associated with other in following hierarchy.

categories
|- subcategories
   |- product_types
      |- products

The view of add() action of ProductsController is

        <?= $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(); ?>

Right now it is list whole subcategories in the list. I want to load subcategories on change of categories and product_types on change of subcategories using Ajax.

There is no good example I can found for CakePHP 3.x and also some documentation mentioned that js helper has been removed from CakePHP 3

How it can be implemented in CakePHP 3. I'm new to CakePHP and Ajax as well.

Thank You.

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 和产品代码创建相同的 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 回答