我希望有一个带有一个文本框,2个下拉菜单和一个单选按钮(在cakephp中)的搜索表单。但这要用分页来完成。搜索和分页工作分开但不在一起。当我选择过滤条件并单击搜索按钮时,结果在第1页上显示正常,但如果我单击任何分页链接,过滤条件将丢失,并且分页数据将显示,而不显示任何过滤器。我该如何解决这个问题? 控制器代码:
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'));
}
答案 0 :(得分: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'));
}
它应该包含在组件中。在我的一个项目中,我有一个自动执行此操作的组件,并替换控制器数据以实现更自动化的过程。
答案 1 :(得分: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'
));
解决了这个问题。
答案 2 :(得分:0)
在2.x中,您可以将表单设置为使用GET而不是POST。
将表单类型从文件更改为GET(BTW,为什么使用文件?)
echo $this->Form->create('Search', array(
'type' => 'get',
当然,不是使用Request
中的数据数组$this->request->data['Search']['name']
您将使用如下查询数组:
if (!empty($this->request->query['name'])) {