0

我希望有一个带有一个文本框、2 个下拉菜单和一个单选按钮(在 cakephp 中)的搜索表单。但这要通过分页来完成。搜索和分页可以单独工作,但不能一起工作。当我选择过滤条件并单击搜索按钮时,结果会在第一页上很好地显示,但如果我单击任何分页链接,过滤条件将丢失,并且分页数据显示时没有任何过滤器。我该如何解决这个问题?控制器代码:

private function _index() {
    $this->genre_list();
    $this->language_list();
    $conditions = array();
    debug($this->postConditions($this->data)); die;
    if (!empty($this->request->data['Artist'])) {
        foreach ($this->request->data['Artist'] as $name => $record) {
            if (isset($record) && !empty($record)) {
                if ($name == 'name') {
                    $conditions = array(
                        $this->modelClass->User. 'User.name LIKE' => '%' . $record . '%',
                    );
                } else {
                    $conditions[$this->modelClass . '.' . $name] = $record;
                }
            }
        }
    };

    $this->paginate = array(
        'contain' => array (
            'User' => array(

                'City',// => array('name'),
                'State',// => array('name'),
                'Country',// => array('name'),
            ),
            'Genre',// => array('name'),
            'Language',// => array('language'),
        ),
        'limit' => 3,
        'conditions' => $conditions
    );


    $data = $this->paginate($this->modelClass);
    //~ debug($data);
    $this->set(compact('data'));
}

看法:

<?php
echo $this->Form->create('Search', array(
    'type' => 'file',
    'inputDefaults' => array(
        'format' => array(
            'label', 'between', 'input', 'error', 'after'
        )
    ),
    'class' => 'form-horizontal'
));
echo $this->Form->input('Artist.name', array(
    'div' => 'control-group',
    'label' => array(
        'class' => 'control-label',
        'text' => 'Artist Name'
    ),
    'between' => '<div class="controls">',
    'after' => '</div>',
    'placeholder' => 'Name',
    'error' => array(
        'attributes' => array(
            'wrap' => 'div',
            'style' => 'color:#B94A48'
        )
    )
));
echo $this->Form->input('Artist.genre_id', array(
    'div' => 'control-group',
    'empty' => 'All',
    'label' => array(
        'class' => 'control-label',
        'text' => 'Genre'
    ),
    'between' => '<div class="controls">',
    'after' => '</div>',
    'error' => array(
        'attributes' => array(
            'wrap' => 'div',
            'style' => 'color:#B94A48'
        )
    )
));

echo $this->Form->input('Artist.language_id', array(
    'div' => 'control-group',
    'empty' => 'All',
    'label' => array(
        'class' => 'control-label',
        'text' => 'Select Lanuage'
    ),
    'between' => '<div class="controls">',
    'after' => '</div>',
    'error' => array(
        'attributes' => array(
            'wrap' => 'div',
            'style' => 'color:#B94A48'


    )
        )
    ));
?>
<?php echo $this->element('pagination'); 

使用会话编辑的代码

private function _index() {
    $this->genre_list();
    $this->language_list();
    $conditions = array();

    //~ foreach($this->request->params['named'] as $key => $record) {
        //~ debug($this->request->params['named']);
            //~ $this->request->data['Search'][$key] = $record;
    //~ }

    if (!empty($this->request->params['named']['page'])) {
    // use session data for conditions
    $conditions = (array)$this->Session->read('_indexConditions');
    } else {
    // delete session data
    $this->Session->delete('_indexConditions');
    }

    $conditions = array();
    if (!empty($this->request->data)) {
    // new search! use the data to make conditions,
    // like you did, and save the conditions
    //~ if (!empty($this->request->data['Artist'])) {
        foreach ($this->request->data['Search'] as $name => $record) {
            if (isset($record) && !empty($record)) {
                if ($name == 'name') {
                    $conditions = array(
                        $this->modelClass->User. 'User.name LIKE' => '%' . $record . '%',
                    );
                } else {
                    $conditions[$this->modelClass . '.' . $name] = $record;
                }
            }
        }
    //~ }
    $this->Session->write('_indexConditions', $conditions);
    }

    $this->paginate = array(
        'contain' => array (
            'User' => array(

                'City',
                'State',
                'Country',
            ),
            'Genre',
            'Language',
        ),

        'limit' => 3,
        'conditions' => $conditions
    );


    $data = $this->paginate('Artist');
    $this->set(compact('data'));
}
4

3 回答 3

0

答案很简单:将搜索条件保存在会话或 cookie 中,如果未发送新条件,则使用这些条件。

为简单起见,我省略了您的大部分代码。像这样的东西应该工作。

private function _index() {
  // check if this is a pagination request without data
  if (!empty($this->request->params['named']['page']) {
    // use session data for conditions
    $conditions = (array)$this->Session->read('_indexConditions');
  } else {
    // delete session data
    $this->Session->delete('_indexConditions');
  }

  $conditions = array();
  if (!empty($this->request->data)) {
    // new search! use the data to make conditions,
    // like you did, and save the conditions
    $this->Session->write('_indexConditions', $conditions);
  }

  $this->paginate = array(
    'conditions' => $conditions
  );
  $data = $this->paginate($this->modelClass);
  $this->set(compact('data'));
}

它应该被包装在一个组件中。在我的一个项目中,我有一个组件可以自动执行此操作并替换控制器数据以实现更自动化的过程。

于 2012-11-06T15:12:54.630 回答
0

在不使用会话的情况下获得了解决方案

在控制器中:

if (!empty($this->request->data) || $this->request->params['named']) {
        foreach($this->request->params['named'] as $key => $record) {
            if($key!= 'page')
                $this->request->data['Search'][$key] = $record;
        }
        foreach ($this->request->data['Search'] as $name => $record) {
            if (isset($record) && !empty($record)) {
                $this->request->params['named'][$name] = $record;
                if ($name == 'name') {
                    $conditions[$this->{$this->modelClass}->User. 'User.name LIKE'] = '%' . $record . '%';
                } else {
                    $conditions[$this->modelClass . '.' . $name] = $record;
                }
            }
        }
    }

并在视图中明确提供控制器和操作:

echo $this->Form->create('Search', array(
    'type' => 'file',
    'inputDefaults' => array(
        'format' => array(
            'label', 'between', 'input', 'error', 'after'
        )
    ),
    'url' => array(
        'controller' => 'artists',
        'action' => 'index',
    ),
    'class' => 'form-horizontal'
));

解决问题。

于 2012-11-07T07:18:00.020 回答
0

在 2.x 中,您可以将表单设置为使用 GET 而不是 POST。

将表单的类型从文件更改为 GET(顺便说一句,为什么使用文件??)

echo $this->Form->create('Search', array(
'type' => 'get',

当然,而不是使用来自 Request 的数据数组

$this->request->data['Search']['name'] 

您将像这样使用查询数组:

if (!empty($this->request->query['name'])) {
于 2012-12-28T02:41:55.887 回答