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.

Anuj TBE
  • 9,198
  • 27
  • 136
  • 285
  • Tip: http://sandbox.dereuromark.de/sandbox/ajax-examples/chained-dropdowns :) It contains also the source code. – mark Jul 29 '16 at 10:01

1 Answers1

3

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.

  • what will be the code for action. I tried this but it is not working. I tried with action `public function getSubcategories() { $id = $this->request->data('id'); $subcategories = $this->Subcategories->find('all', [ 'conditions' => [ 'category_id' => $id ] ]); echo json_encode($subcategories); }` – Anuj TBE Aug 17 '16 at 08:00
  • Thanks it works. There thing I missing was `$(' – Anuj TBE Aug 17 '16 at 09:41