Skip to content

Instantly share code, notes, and snippets.

@amine2233
Created March 12, 2014 07:14
Show Gist options
  • Save amine2233/a27fccba3a4eec72b0a7 to your computer and use it in GitHub Desktop.
Save amine2233/a27fccba3a4eec72b0a7 to your computer and use it in GitHub Desktop.
CakePhp
public function autocomplete() {
$this->request->params['named']['filter'] = $this->request->data[$this->modelClass]['filter'];
if (!empty($this->request->params['named']['filter']) || $this->request->params['named']['filter'] !== '') {
$this->Prg->commonProcess();
$this->Paginator->settings = array(
'conditions' => $this->Location->parseCriteria($this->Prg->parsedParams()),
);
$this->set('locations', $this->Paginator->paginate());
}
$this->render('autocomplete', 'ajax');
}
<div class="row">
<div class="col-lg-offset-3 col-lg-6">
<?php
echo $this->Form->create('Location', array(
'url' => array('controller' => 'locations', 'action' => 'find'),
'inputDefaults' => array(
'div' => false,
)
)
);
?>
<div class="input-group">
<?php
echo $this->Form->input('filter', array('label' => '', 'type' => 'text', 'class' => "form-control",
'placeholder' => 'Recherche', 'id' => 'InputRecherche'), array('escape' => false));
?>
<span class="input-group-btn">
<?php
echo $this->Form->button('<span class="glyphicon glyphicon-search"/>', array(
'class' => 'btn btn-default',
));
?>
</span>
</div>
<?php echo $this->Form->end(); ?>
<?php
$update = "#Recherche";
$before = $this->Js->get("#loading")->effect('fadeIn', array('buffer' => false));
$complete = $this->Js->get('#loading')->effect('fadeOut', array('buffer' => false));
$data = $this->Js->get('#InputRecherche')->serializeForm(array('isForm' => true, 'inline' => true));
echo $this->Js->get("#InputRecherche")->event('keyup', $this->Js->request(
array('controller' => 'locations', 'action' => 'autocomplete'), array(
'async' => true,
'dataExpression' => true,
'method' => 'post',
'data' => $data,
'update' => $update,
'before' => $before,
'complete' => $complete,
)
));
?>
</div>
</div>
<div id="loading" class="center-block"><?php $this->Html->image('load.gif'); ?></div>
<div id="Recherche"></div>
/**
* rating method
*
* @param string $id
* @return void
*
*/
public function rating($id) {
$this->Location->id = $id;
if (!$this->Location->exists()) {
throw new NotFoundException(__('Invalid location'));
}
if ($this->request->is(array('post', 'put', 'ajax'))) {
$this->Location->save(array('rate' => $this->request['data']['value']));
}
if ($this->request->is('ajax')) {
$this->render('ajax');
} else {
return $this->redirect($this->referer());
}
}
<?php
$data = "{field: 'rate', value: $('#RatingLocation').val()}";
$this->Js->get("#RatingLocation")->event(
'change', $this->Js->request(
array('action' => 'rating', $location['Location']['id']), array(
'update' => '#View',
'data' => $data,
'async' => true,
'dataExpression' => true,
'method' => 'POST'
)
)
);
?>
<?php
echo $this->Form->input('rate', array(
'type' => "number",
'id' => "some_id",
'data-max' => "5",
'data-min' => "1",
'id' => 'RatingLocation',
'class' => "rating",
'value' => $location['Location']['rate']
));
?>
<div class="pagination pagination-large">
<ul class="pagination">
<?php
//echo $this->Paginator->first('First', array('escape' => false, 'tag' => 'li'), null, array('escape' => false, 'tag' => 'li', 'class' => 'disabled', 'disabledTag' => 'a'));
echo $this->Paginator->prev(
'&laquo;',
array('escape' => false, 'tag' => 'li'),
null,
array(
'escape' => false,
'tag' => 'li',
'class' => 'disabled',
'disabledTag' => 'a'));
echo $this->Paginator->numbers(
array(
'separator' => '',
'currentTag' => 'a',
'currentClass' => 'active',
'tag' => 'li',
'first' => 1));
echo $this->Paginator->next(
'&raquo;',
array(
'escape' => false,
'tag' => 'li',
'currentClass' => 'disabled'),
null,
array(
'escape' => false,
'tag' => 'li',
'class' => 'disabled',
'disabledTag' => 'a'));
//echo $this->Paginator->last('Last',array('escape' => false, 'tag' => 'li', 'currentClass' => 'disabled'), null, array('escape' => false, 'tag' => 'li', 'class' => 'disabled', 'disabledTag' => 'a'));
?>
</ul>
</div>
<div class="pagination pagination-large">
<ul class="pagination">
<?php
$this->Paginator->options(array(
'update' => '#View',
'evalScript' => true,
'before' => $this->Js->get("#loading")->effect('fadeIn', array('buffer' => false)),
'complete' => $this->Js->get('#loading')->effect('fadeOut', array('buffer' => false)),
));
//echo $this->Paginator->first('First', array('escape' => false, 'tag' => 'li'), null, array('escape' => false, 'tag' => 'li', 'class' => 'disabled', 'disabledTag' => 'a'));
echo $this->Paginator->prev(
'&laquo;',
array('escape' => false, 'tag' => 'li'),
null,
array(
'escape' => false,
'tag' => 'li',
'class' => 'disabled',
'disabledTag' => 'a'));
echo $this->Paginator->numbers(
array(
'separator' => '',
'currentTag' => 'a',
'currentClass' => 'active',
'tag' => 'li',
'first' => 1));
echo $this->Paginator->next(
'&raquo;',
array(
'escape' => false,
'tag' => 'li',
'currentClass' => 'disabled'),
null,
array(
'escape' => false,
'tag' => 'li',
'class' => 'disabled',
'disabledTag' => 'a'));
//echo $this->Paginator->last('Last',array('escape' => false, 'tag' => 'li', 'currentClass' => 'disabled'), null, array('escape' => false, 'tag' => 'li', 'class' => 'disabled', 'disabledTag' => 'a'));
?>
</ul>
</div>
<?php
$data = "{field: 'rate', value: $('#RatingLocation').val()}";
$this->Js->get("#RatingLocation")->event(
'change', $this->Js->request(
array('action' => 'rating', $location['Location']['id']), array(
'update' => '#View',
'data' => $data,
'async' => true,
'dataExpression' => true,
'method' => 'POST'
)
)
);
?>
<?php
echo $this->Form->input('rate', array(
'type' => "number",
'id' => "some_id",
'data-max' => "5",
'data-min' => "1",
'id' => 'RatingLocation',
'class' => "rating",
'value' => $location['Location']['rate']
));
?>
<?php echo $this->Html->script('bootstrap-rating-input', array('inline' => false)); ?>
public function validate_message() {
if ($this->request->is('ajax')) {
$location = $this->Location->create();
$location['Location'][$this->request['data']['field']] = $this->request['data']['value'];
$this->Location->set($location);
$error = $this->validateErrors($this->Location);
if ($this->Location->validates($location)) {
$this->autoRender = FALSE;
$this->autoLayout = '';
} else {
$errors = $this->validateErrors($this->Location);
foreach ($errors[$this->request['data']['field']] as $key => $value) {
$error = $value;
}
$this->set(compact('error'));
$this->render('validate_message', 'ajax');
}
}
}
<?php echo $this->Form->input('name', array('id' => 'Name')); ?>
<?php
$update = "#error";
$before = $this->Js->get("#loading")->effect('fadeIn', array('buffer' => false));
$complete = $this->Js->get('#loading')->effect('fadeOut', array('buffer' => false));
$data = "{field: 'name', value: $('#Name').val()}";
echo $this->Js->get("#Name")->event('blur', $this->Js->request(
array('controller' => 'locations', 'action' => 'validate_message'), array(
'async' => true,
'dataExpression' => true,
'method' => 'post',
'data' => $data,
'update' => $update,
'before' => $before,
'complete' => $complete,
)
)
);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment