Anuj TBE
Anuj TBE

Reputation: 9790

Chained dropdown menu in cakephp 3

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.

Upvotes: 1

Views: 1391

Answers (1)

Pruthviraj Chudasama
Pruthviraj Chudasama

Reputation: 448

ctp file as below. here first of all i give id to field categories,subcategories,producttype and productcode .

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

and in your ajax call for subcategories is as below. you can create same ajax call for product_type and product code

<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"));

                    });
                } 
            });
        }
    });

from this code you can get chained dropdown menu. its work for you.

Upvotes: 3

Related Questions