Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!-- Подбор объекта -->
- <?php echo $form->typeAheadRow($model, 'object_id', array(
- 'source' => 'js:function (query, process) {
- clearTimeout(typeaheadtimer);
- if (query.length >= 2) {
- typeaheadtimer = setTimeout(function(){
- $.ajax({
- url: "/obj/typeahead",
- type: "POST",
- dataType: "JSON",
- data: "query=" + query,
- success: function(response) {
- var data = new Array();
- $.each(response, function(i, title){
- data.push(title);
- })
- return process(data);
- }
- });
- },300);
- }
- }',
- 'items' => 10,
- 'matcher' => "js:function(item) {
- return ~0;
- }",
- 'updater' => 'js:function(item) {
- var parts = item.split("(#");
- var object_id = parseInt(parts[1]);
- $("#request___create_form_object_id").val(object_id);
- return item;
- }'
- ), array(
- 'name' => 'object_typeahead',
- 'placeholder' => 'Название объекта (ID, адрес)...',
- 'value' => isset($model->object) ? $model->object->getName() : ""
- )); ?>
- // SERVER
- /**
- * Автоподбор объектов для плагина Typeahead
- */
- public function actionTypeahead()
- {
- //Поисковой запрос
- $query = $_POST['query'];
- $data = array();
- //Если это ИД объекта - просто отдадим сам объект
- // TODO: is_number(numeric)
- if ((int)$query > 0 && $query == (int)$query) {
- $data[] = Object::model()->findByPk($query)->getName();
- }
- else {
- // Разобъем строку на массив
- $query = str_replace(",", " ", $query);
- $search = explode(" ", $query);
- $criteria = new CDbCriteria();
- $criteria->with = 'city';
- if (count($search)) {
- foreach ($search as $word) {
- if (strlen($word) > 3) {
- $criteria->addCondition("concat_ws(' ',city.title,t.address,t.title) ilike '%{$word}%'");
- }
- }
- }
- $criteria->limit = 50;
- $objects = Object::model()->findAll($criteria);
- if (count($objects)) {
- foreach ($objects as $object) {
- $data[] = $object->getName();
- }
- }
- }
- if (!count($data)) {
- $data[] = "Ничего не найдено (#0)";
- }
- echo CJSON::encode($data);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement