Jquery 搜索 Ajax 分页
这是我的第一篇文章,我一直在开发这个组件,用于根据索引页面表单中输入的数据构建动态分页条件。
该组件,创建文件 build_conditions.php
<?php
/**
*
* Build conditions for search
* @author Geneller Naranjo
* @version 1.0
* @license This content is released under the MIT License (http://www.opensource.org/licenses/mit-license.php)
*
*/
class BuildConditionsComponent extends Object {
function initialize(&$controller) {
$this->controller =& $controller;
}
function startup(&$controller) { }
/**
*
* Funcion que arma las condiciones para los buscadores.
* Tener cuidado, solo usar para listados que usan el like como comparador.
* @param Strin $modelName Modelo con las condiciones.
*/
function buildConditions($modelName) {
$conditions = array();
foreach ($this->controller->data['Search'] as $field => $value) {
if(!empty($value)) {
if(substr($field, -3, 3) == '_id') {
$conditions["$modelName.$field"] = $value;
} else {
$conditions[] = "$modelName.$field like '$value%'";
}
}
}
return $conditions;
}
/**
*
* Funcion que arma las condiciones para los buscadores.
* Tener cuidado, solo usar para listados que usan el like como comparador.
* Arma las condiciones dependiendo de los campos del formulario.
*/
function buildComplexConditions() {
$conditions = array();
foreach ($this->controller->data['Search'] as $model => $fields) {
foreach ($fields as $field => $value) {
if(!empty($value)) {
if(substr($field, -3, 3) == '_id') {
$conditions["$model.$field"] = $value;
} else {
$conditions[] = "$model.$field like '$value%'";
}
}
}
}
return $conditions;
}
}
?>
在 webroot/js/ 下创建一个文件 jquery.paginate_form.js。
/**
*
* Triggers search
* @author Geneller Naranjo
* @version 1.0
* @license This content is released under the MIT License (http://www.opensource.org/licenses/mit-license.php)
*
*/
// Teclas que no deben disparar la busqueda.
var unavailableKeyCodes = [9, 13, 16, 17, 18, 19, 20, 27, 35, 36, 37, 38, 39, 40, 45, 93, 106, 107, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 144, 145, 186, 187, 189, 190, 191, 192, 219, 220, 221, 222];
// Variables para abortar peticiones ajax.
var requesting = true;
var request = new $.ajax();
var focused = '';
function paginator(url, domId, targetDomId) {
var ajaxDelay = 8;
var params = {};
function paginate(params) {
if(requesting == true) {
request.abort();
}
if(typeof(params.url) != 'undefined') {
url = params.url;
}
request = $.ajax({
data: params.data, type: 'post', async: true, url: url,
beforeSend: function() {requesting = true; },
complete: function(data) {
$('#' + targetDomId).html(data.responseText);
requesting = false;
}
});
}
setInterval(function() {
ajaxDelay++;
if(ajaxDelay == 8) {
params.data = $('#' + domId).serialize();
paginate(params);
}}, 100);
$('#' + domId + ' div input').keyup(function(e) {
if(requesting == true) {
request.abort();
}
});
$('#' + domId + ' div input').keyup(function(e) {
delete params.url;
if(unavailableKeyCodes.indexOf(e.keyCode) == -1) {
if(typeof(e.currentTarget.attributes[1].nodeValue) != 'undefined') {
focused = e.currentTarget.attributes[1].nodeValue;
}
ajaxDelay = 1;
}
});
$('#' + domId + ' div select').change(function(e) {
params.data = $('#' + domId).serialize();
paginate(params);
});
$('#' + domId + ' div input:checkbox').change(function(e) {
params.data = $('#' + domId).serialize();
paginate(params);
});
$('#' + domId).submit(function() {
delete params.url;
params.data = $('#' + domId).serialize();
paginate(params);
return false;
});
$('#' + targetDomId + ' .paging a').live('click', function(e) {
e.preventDefault();
delete params.url;
params.data = $('#' + domId).serialize();
var href = $(this).attr('href');
params.url = href.replace('index/', 'index_ajax/');
paginate(params);
});
$('#' + targetDomId + ' table th a').live('click', function(e) {
e.preventDefault();
delete params.url;
params.data = $('#' + domId).serialize();
var href = $(this).attr('href');
params.url = href.replace('index/', 'index_ajax/');
paginate(params);
});
}
在您的视图中(我假设是索引),放置以下代码。
<?php echo $javascript->link('jquery.paginate_form'); ?>
<script type="text/javascript">
$(function() {
paginator('<?php echo $this->base . '/<controller>/<action>'?>', 'SearchIndexForm', 'indexAjax');
});
</script>
这是索引的 html 示例代码。
<div class="header">
<h2>Carrocerías</h2>
</div>
<div id="hideableSearch">
<div id="showSearchImages">
<?php
echo $html->image('Search-index.png', array('style'=>'height: 35px;'));
?>
</div>
<div id="searchFormDiv">
<fieldset class="fieldset-search-index">
<?php
echo $form->create('Search', array('id'=>'SearchIndexForm', 'encoding'=>'UTF-8'));
echo $form->input('nombre', array('type'=>'text', 'size'=>10));
echo $form->input('codigo', array('type'=>'text', 'size'=>10));
echo $form->input('descripcion', array('type'=>'text', 'size'=>20));
echo $form->end();
?>
</fieldset>
</div>
</div>
<div id="indexAjax">
<table cellpadding="0" cellspacing="0">
<tr>
<th>#</th>
<th><?php echo $this->Paginator->sort('Nombre','nombre', array('url'=>$url));?></th>
<th><?php echo $this->Paginator->sort('Codigo','codigo', array('url'=>$url));?></th>
<th><?php echo $this->Paginator->sort('Descripcion','descripcion', array('url'=>$url));?></th>
<th class="actions">Acciones</th>
</tr>
<?php
$i = 0;
foreach ($carrocerias as $item):
$class = null;
if ($i++ % 2 == 0) $class = ' class="altrow"';
?>
<tr<?php echo $class; ?> id="<?php echo $item['Carroceria']['id']?>">
<td><?php echo $i; ?> </td>
<td><?php echo $item['Carroceria']['nombre']; ?> </td>
<td><?php echo $item['Carroceria']['codigo']; ?> </td>
<td><?php echo $item['Carroceria']['descripcion']; ?> </td>
<td class="actions">
<?php echo $this->Html->link($html->image('Edit.png'), array('action' => 'edit', $item['Carroceria']['id']), array('escape'=>false, 'class'=>'index-actions')); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<p>
<?php
echo $this->Paginator->counter(array(
'format' => 'Pag %page% de %pages%, %current% registros de %count% en total, desde %start% hasta %end%'
));
?></p>
<div class="paging">
<?php
$paginator->options(array('url'=> $url));
echo $this->Paginator->prev('<< '.'Prev', array(), null, array('class'=>'disabled')).' | ';
echo $this->Paginator->numbers() .' | ';
echo $this->Paginator->next('Sig'.' >>', array(), null, array('class' => 'disabled'));
?></div>
</div>
视图中的代码应该是您希望在 #indexAjax 中看到的答案。
您的控制器中应该类似于以下内容
var $components = array('BuildConditions');
以及您在请求 url 中提到的函数,对于此示例,“carrocerias/index_ajax”。
function index_ajax() {
$this->Carroceria->recursive = 0;
$this->layout = 'ajax';
$conditions = $this->BuildConditions->buildConditions($this->modelClass);
$this->set('carrocerias', $this->paginate(null, $conditions));
}
对于简单的条件,如果您要为不同的模型创建条件,则应使用“buildComplexConditions”而不是“buildConditions”。
希望我解释清楚了,请评论并让我知道任何问题或如何改进此组件。
希望我解释清楚了,请评论并让我知道如何修复任何问题,例如第一个 u