CakePHP-2.4:按ajax分类的子类别

时间:2014-11-05 06:05:24

标签: ajax cakephp cakephp-2.4

我试图通过ajax按类别获取子类别。所以我已经通过ajax get方法在控制器中发送数据,如add.ctp中的bellow代码

$('document').ready(function(){
            $( "#division" ).change(function() {

                        var value=$('#division').val(); 

                            $.get("<?php echo   Router::url(array('controller'=>'userStoreSelections','action'=>'add'));?>",{list:value},function(data){
                    $('.districts').html(data);
                    alert(data);
                });
            });
    });

在编写方法的控制器中,当我编写下面的代码时,它工作正常。

$madDistricts = $this->UserStoreSelection->MadDistricts->find('list',array(
            'conditions'=>array('mad_divisions_id'=>3)
));

但是,当我给出由ajax发送的价值时,它不起作用。我写的是这样的

$madDistricts = $this->UserStoreSelection->MadDistricts->find('list',array(
            'conditions'=>array('mad_divisions_id'=>"$list")
));

它显示了这样的查询

SELECT `MadDistricts`.`id`, `MadDistricts`.`name` FROM `store`.`mad_districts` AS `MadDistricts` WHERE `mad_divisions_id` IS NULL       

在选择类别之后,查询中没有任何变化。我测试了ajax代码,发送值没有问题。

更具体地说,这是添加操作代码

public function add() {

        if(isset($this->request->query['list'])){
            $search = $this->request->query['list'];
            echo $search;
        }
        else{
            $search = '';
        }
        if ($this->request->is('post')) {
            $this->UserStoreSelection->create();
            if ($this->UserStoreSelection->save($this->request->data)) {
                $this->Session->setFlash(__('The user store selection has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user store selection could not be saved. Please, try again.'));
            }
        }
        $madDivisions = $this->UserStoreSelection->MadDivisions->find('list');

            $madDistricts = $this->UserStoreSelection->MadDistricts->find('list',array(
            'conditions'=>array('mad_divisions_id'=>"$list")
         ));
        $madAreas = $this->UserStoreSelection->MadAreas->find('list');
        $users = $this->UserStoreSelection->Users->find('list',array('fields' => array('id','username')));
        $madStores = $this->UserStoreSelection->MadStores->find('list',array('fields' => array('id','store_name')));
        $this->set(compact('madDivisions', 'madDistricts', 'madAreas', 'users','madStores'));
    } 

1 个答案:

答案 0 :(得分:0)

您需要更改add.ctp文件代码,如:

$('document').ready(function () {
$("#division").change(function () {
    var value = $(this).val();
    $.ajax({
        url: '<?php echo Router::url(' / ',true);?>userStoreSelections/subcategories',
        type: "POST",
        dataType: 'json',
        data: 'category=' + id,
        success: function (res) {
            var html = '<option>Sub Category</option>';
            if (res.flage == true) {
                 html = res.data;
            }
            $('.districts').html(html);
        }
    });
});

});

在userStoreSelections中创建一个函数,以获取子类别

public function subcategories() {

    $response = array('flage' => false);

    $madDistricts = $this->UserStoreSelection->MadDistricts->find('list', array(
        'conditions' => array('mad_divisions_id' => $this->request->data['category'])
    ));

    $options = "<option>Subcategories</option>";
    if ($states) {
        $response['flage'] = true;
        foreach ($madDistricts as $id => $name) {
            $options .= "<option value='" . $id . "'>" . $name . "</option>";
        }
        $response['data'] = $options;
    }
    echo json_encode($response);
    exit;
}