| Новости | FAQ | Авторы | Документация | В действии | Библиотека |
| Инструменты | Полезные ссылки | Хостинги | Скачать | Примеры | Форум |
BeaVe 21.05.2006 13:55 / 21.05.2006 13:56
Попробовал написать мелкий класс для построения списка алфавитного указателя. В классе имеется таблица с цифрами и буквами (русского и английского алфавита).@CLASS
aindex
@init[tItems;sItemsColumnName][tblItemsColumns;strActiveLetter]
^if(def $tItems){
^if(!$tItems is 'table'){
^throw[aindex;Items is not table.]
}
$_prepareActiveLetter{
$strActiveLetter[^strActiveLetter.trim[]]
$strActiveLetter[^strActiveLetter.left(1)]
$strActiveLetter[^strActiveLetter.upper[]]
}
$_appendActiveLetter{
$_prepareActiveLetter
^tblActiveLetters.append{$strActiveLetter}
}
$tblItemsColumns[^tItems.columns[]]
$tblActiveLetters[^table::create{letter}]
^switch(^tblItemsColumns.count[]){
^case(0){
^try{
^tItems.menu{
$strActiveLetter[$tItems.0]
$_appendActiveLetter
}
}{
^if($exception.comment eq 'column not found'){
$exception.handled(1)
}
# ЕСЛИ ТАБЛИЦА НЕ NAMELESS, ТО ПОКА ТОЧИМ ВАТУ
}
}
^case(1){
^tItems.menu{
$strActiveLetter[$tItems.[$tblItemsColumns.column]]
$_appendActiveLetter
}
}
^case[DEFAULT]{
^if(!def $sItemsColumnName){
^throw[aindex;Items column name must be defined.]
}
^if(def $sItemsColumnName && !($sItemsColumnName is 'string')){
^throw[aindex;Items column name is not string.]
}
^tItems.menu{
^try{
$strActiveLetter[$tItems.[$sItemsColumnName]]
$_appendActiveLetter
}{
^if($exception.comment eq 'column not found'){
$exception.handled(1)
^throw[aindex;Items column name is wrong.]
}
}
}
}
}
^if($tblActiveLetters){
$tblShareLetters[$tblLetters]
$hshActiveLetters[^tblActiveLetters.hash[letter][$.distinct(1)]]
$tblLetters[^table::create{letter group is_active}]
^tblShareLetters.menu{
^tblLetters.append{$tblShareLetters.letter $tblShareLetters.group ^if($hshActiveLetters.[$tblShareLetters.letter]){1}{0}}
}
}
}
#end @init[]
@print[params]
^if(def $params.tag_name){
<$params.tag_name^if(def $params.tag_attr){$params.tag_attr}>
}
$strTargetURL[^if(def $params.target_url){$params.target_url}{./}]
$strTargetURLSeparator[^if(^params.target_url.pos[?]>=0){^taint[&]}{?}]
$strFormName[^if(def $params.form_name){$params.form_name}{letter}]
^if($params.mode eq 'html'){
^_printHTML[$params]
}{
^_printXML[$params]
}
^if(def $params.tag_name){
</$params.tag_name>
}
#end @print[]
@_printHTML[params][_params]
$_params[^hash::create[$params]]
^if(def $_params.title){
$_params.title
}
^tblLetters.menu{
^if($form:[$strFormName] eq $tblLetters.letter){
$tblLetters.letter
}{
<a href="${strTargetURL}${strTargetURLSeparator}${strFormName}=^taint[uri][$tblLetters.letter]">$tblLetters.letter</a>
}
}
#end @_printHTML[]
@_printXML[params][_params][strLetterType]
$_params[^hash::create[$params]]
^if(def $_params.title){
<title>$_params.title</title>
}
^tblLetters.menu{
^if($form:[$strFormName] eq $tblLetters.letter){$strLetterType[current]}
<aindex
^if($strLetterType ne 'current'){
href="${strTargetURL}${strTargetURLSeparator}${strFormName}=^taint[uri][$tblLetters.letter]"
}
^if(def $hshGroups.[$tblLetters.group].type){group="$hshGroups.[$tblLetters.group].type"}
^if(def $hshGroups.[$tblLetters.group].lang){lang="$hshGroups.[$tblLetters.group].lang"}
^if(def $strLetterType){type="$strLetterType"}
^try{
is_active="^if($tblLetters.is_active){1}{0}"
}{
$exception.handled(1)
}
/>
$strLetterType[]
}
#end @_printXML[]
@auto[]
$tblGroups[^table::create{id type lang
1 numbers
2 letters en
3 letters ru
}]
$tblLetters[^table::create{letter group
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
A 2
B 2
C 2
D 2
E 2
F 2
G 2
H 2
I 2
J 2
K 2
L 2
M 2
N 2
O 2
P 2
Q 2
R 2
S 2
T 2
U 2
V 2
W 2
X 2
Y 2
Z 2
А 3
Б 3
В 3
Г 3
Д 3
Е 3
Ё 3
Ж 3
З 3
И 3
Й 3
К 3
Л 3
М 3
Н 3
О 3
П 3
Р 3
С 3
Т 3
У 3
Ф 3
Х 3
Ц 3
Ч 3
Ш 3
Щ 3
Ъ 3
Ы 3
Ь 3
Э 3
Ю 3
Я 3
}]
$hshGroups[^tblGroups.hash[id]]
#end @auto[]Примеры использования.$items[^table::sql{
SELECT
item_id,
title,
details
FROM
items
}]
$aindex[^aindex::init[$items;title]]
^aindex.print[
$.mode[html]
$.target_url[/items/]
]Один из самых неправильных вызовов, т.к. таблица с items передаётся в класс, но в некоторых случаях он пойдёт.$items_title[^table::create{title
...
}]
$aindex[^aindex::init[$items_title]]
^aindex.print[
$.tag_name[alfa-index]
$.target_url[/items/]
]Впринципе самый нормальный способ использования, т.к. передаются только названия.$items_title[^table::create[nameless]{
...
}]
$aindex[^aindex::init[$items_title]]
...Может когда-то пригодиться кому-нибудь такой способ... Правда я в этом совмневаюсь.@init[ТАБЛИЦА;НАЗВАНИЕ СТОЛБЦА С ДАННОЙ ТАБЛИЦЫ]Оба поля являются необязательными... В некоторых случаях можно не использовать этот метод, тогда поидеи будет использоваться внутренняя таблица символов.
@print[ $.mode[STRING] $.tag_name[STRING] $.tag_attr[STRING] $.form_name[STRING] $.target_url[STRING] ]$.mode[XML/HTML]