Created
June 24, 2014 11:09
-
-
Save cergey-obr/d480c183a521432d92a8 to your computer and use it in GitHub Desktop.
Селектор из базы данных
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.ajaxSelector = function(offset, limit, callback) { | |
var page_id = this.data('page-id'); | |
var template = this.data('template'); | |
var data = this.find(":input[value != '']").serialize(); | |
var params = $.param({ | |
id: page_id, | |
offset: offset, | |
limit: limit, | |
template: template | |
}); | |
$.ajax({ | |
url: '/selector.ajax?' + params, | |
type: 'POST', | |
async: false, | |
data: data, | |
success: function(html) { | |
callback(html); | |
} | |
}); | |
}; | |
$('form').ajaxSelector(0, 20, function(html) { | |
$('#results').append(html); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*----- Получение отфильтрованного списка элементов | |
---------------------------------------------------- | |
------- $parent_id - id родительского элемента | |
------- $offset - начальная позиция | |
------- $limit - кол-во элементов на одной странице | |
--------------------------------------------------*/ | |
public function ajaxSelector($parent_id, $offset = 0, $limit = 20) { | |
$hierarchy = umiHierarchy::getInstance(); | |
$parent = $hierarchy->getElement($parent_id); | |
$total = 0; | |
if ($parent instanceof umiHierarchyElement) { | |
$typesCollection = umiObjectTypesCollection::getInstance(); | |
$type_id = $hierarchy->getDominantTypeId($parent_id); | |
$type = $typesCollection->getType($type_id); | |
if ($type instanceof umiObjectType) { | |
$selector = new selector('pages'); | |
$selector->types('object-type')->id($type_id); | |
$selector->where('hierarchy')->page($parent_id)->childs(1); | |
if ((isset($_REQUEST['filter'])) and (!empty($_REQUEST['filter']))) { | |
foreach ($_REQUEST['filter'] as $field => $value) { | |
switch (gettype($value)) { | |
case 'array': { | |
$selector->option('or-mode')->field($field); | |
$selector->where($field)->equals($value); | |
} break; | |
case 'string': { | |
$selector->where($field)->like('%'.$value.'%'); | |
} break; | |
case 'integer': { | |
$selector->where($field)->equals($value); | |
} break; | |
} | |
} | |
} | |
$selector->limit($offset, $limit); | |
$items = array(); | |
foreach ($selector as $item) { | |
$items[] = def_module::parseTemplate(null, array( | |
'attribute:id' => $item->id, | |
'attribute:link' => $item->link, | |
'node:value' => $item->name | |
)); | |
} | |
return def_module::parseTemplate(null, array( | |
'nodes:item' => $items, | |
'current_count' => count($selector->result), | |
'total' => $selector->length | |
)); | |
} | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<sitemap> | |
<match pattern="selector.ajax"> | |
<generate src="udata://content/ajaxSelector/{id}/{offset}/{limit}" /> | |
<transform src="./templates/<template-name>/xslt/ajax/{template}.xsl" /> | |
<serialize type="html" /> | |
</match> | |
</sitemap> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment