fix(contacts): пагинация, по умолчанию буква «А», читаемые ширины колонок
Жалоба: при загрузке список широкий, затем узкий — текста не видно; после фильтра по алфавиту карточки всё равно узкие. - Пагинация: page/per_page (25/50/100/200, по умолчанию 50), «← Назад / Вперёд →», «стр. N из M», селектор размера страницы; page клампится. - По умолчанию открывается буква «А» (letter=А), чип «Все» снимает фильтр (letter=all), повторный клик по активной букве ведёт в «Все». - Ширины: .ctab min-width 860px + минимумы колонок 120/90/100/140/130/130/150, overflow:auto у списка вместо сжатия текста; c2 flex:2.6, карточка max-width 460px, у значений в ячейках title с полным текстом. - Страница: 872 КБ → 56 КБ (7 строк на букву А), 127 мс. - spec contacts-ui: добавлены требования «Пагинация и буква по умолчанию» и «Читаемая ширина колонок». Замерено headless-Chromium (метрики из DOM): 1600 px с закрытой ИИ-панелью — c2=938 px, таблица 860 помещается целиком; с открытой — c2=628 и скролл при сохранении минимальных ширин. tests/test_contacts_ui.py — 15/15; контейнер пересобран (healthy), живые запросы: letter=all&per_page=25 → 25 строк, «стр. 1 из 9», page=2 — другие 25.
This commit is contained in:
+30
-8
@@ -386,34 +386,56 @@ def _contact_ctx(contact: dict | None) -> dict:
|
||||
return {"current": contact, "mails": mails, "ctasks": ctasks}
|
||||
|
||||
|
||||
PER_PAGE_OPTIONS = (25, 50, 100, 200)
|
||||
DEFAULT_LETTER = "А"
|
||||
|
||||
|
||||
@app.get("/contacts", response_class=HTMLResponse)
|
||||
def contacts_page(request: Request, _: AuthDep, q: str = "", company: str = "",
|
||||
last_name: str = "", first_name: str = "", middle_name: str = "",
|
||||
phone: str = "", email: str = "", position: str = "",
|
||||
letter: str = "", sort: str = "last_name", dir: str = "asc",
|
||||
cid: str = ""):
|
||||
"""Страница контактов: фильтры + алфавит (c1), таблица (c2), карточка (c3)."""
|
||||
letter: str = DEFAULT_LETTER, page: int = 1, per_page: int = 50,
|
||||
sort: str = "last_name", dir: str = "asc", cid: str = ""):
|
||||
"""Страница контактов: фильтры + алфавит (c1), таблица с пагинацией (c2), карточка (c3).
|
||||
|
||||
По умолчанию открывается буква «А»; `letter=all` — весь список.
|
||||
"""
|
||||
import math
|
||||
db_all = contacts.load()
|
||||
everyone = contacts.visible(db_all)
|
||||
filters = {"q": q, "company": company, "last_name": last_name,
|
||||
"first_name": first_name, "middle_name": middle_name,
|
||||
"phone": phone, "email": email, "position": position}
|
||||
found = contacts.search(everyone, letter=letter, **filters)
|
||||
# "all"/"все" — без фильтра по букве; иначе буква из указателя
|
||||
letter_eff = "" if (letter or "").strip().lower() in ("all", "все") else (letter or "").strip()
|
||||
found = contacts.search(everyone, letter=letter_eff, **filters)
|
||||
found = contacts.sort_contacts(found, sort, reverse=(dir == "desc"))
|
||||
# алфавит считается без учёта запроса q и выбранной буквы — иначе «прыгает»
|
||||
alpha_source = contacts.search(everyone, **{**filters, "q": ""})
|
||||
params = {k: v for k, v in {**filters, "letter": letter}.items() if v}
|
||||
|
||||
per_page = per_page if per_page in PER_PAGE_OPTIONS else 50
|
||||
pages = max(1, math.ceil(len(found) / per_page))
|
||||
page = min(max(1, int(page or 1)), pages)
|
||||
page_items = found[(page - 1) * per_page: page * per_page]
|
||||
|
||||
letter_q = letter_eff or "all"
|
||||
params = {k: v for k, v in {**filters, "letter": letter_q}.items() if v}
|
||||
if sort != "last_name":
|
||||
params["sort"] = sort
|
||||
if dir != "asc":
|
||||
params["dir"] = dir
|
||||
if per_page != 50:
|
||||
params["per_page"] = per_page
|
||||
ctx = {
|
||||
"contacts": found, "total": len(everyone), "params": params,
|
||||
"contacts": page_items, "total": len(everyone), "found": len(found),
|
||||
"params": params, "page_no": page, "pages": pages, "per_page": per_page,
|
||||
"per_page_options": PER_PAGE_OPTIONS, "letter_param": letter_q,
|
||||
"params_json": {k: v for k, v in params.items() if k != "per_page"},
|
||||
"alphabet": contacts.alphabet(alpha_source),
|
||||
"companies": contacts.filter_options(everyone, "company"),
|
||||
"positions": contacts.filter_options(everyone, "position"),
|
||||
"sort": sort, "dir": dir, "letter": letter, "filters": filters,
|
||||
"page": "contacts",
|
||||
"sort": sort, "dir": dir, "letter": letter_eff, "filters": filters,
|
||||
"page": "contacts", # подсветка раздела в боковой панели
|
||||
}
|
||||
if cid:
|
||||
ctx.update(_contact_ctx(contacts.by_id(cid, db_all)))
|
||||
|
||||
@@ -344,13 +344,35 @@ body.f3 .c3{flex:1}
|
||||
.alpha .al:hover{background:var(--hover);color:var(--fg)}
|
||||
.alpha .al.act{background:var(--accent);color:var(--bg)}
|
||||
.alpha .al.off{color:var(--fg3);opacity:.35;cursor:default}
|
||||
.cwide{width:auto;flex:1;min-width:0;position:relative}
|
||||
.ctab{width:100%;border-collapse:collapse;font-size:12.5px}
|
||||
.alpha .al.all{width:auto;padding:2px 7px;font-weight:600;border:1px solid var(--border)}
|
||||
.alpha .al.all.act{border-color:var(--accent)}
|
||||
.cpager{display:flex;align-items:center;gap:8px;padding:6px 10px;border-top:1px solid var(--border);
|
||||
background:var(--bg2);font-size:11.5px;flex-wrap:wrap;flex-shrink:0}
|
||||
.cpager .btn{background:none;border:1px solid var(--border);border-radius:6px;padding:3px 9px;
|
||||
color:var(--fg);cursor:pointer;font-size:11.5px}
|
||||
.cpager .btn:hover:not([disabled]){border-color:var(--accent)}
|
||||
.cpager .btn[disabled]{opacity:.4;cursor:default}
|
||||
.cpager .pinfo{color:var(--fg2)}
|
||||
.cpager .pper{display:flex;align-items:center;gap:4px;color:var(--fg3);margin-left:auto}
|
||||
.cpager .pper select{padding:2px 4px;border:1px solid var(--border);border-radius:5px;
|
||||
background:var(--bg);color:var(--fg);font-size:11.5px}
|
||||
.cwide{flex:2.6;min-width:600px;position:relative;display:flex;flex-direction:column;min-height:0}
|
||||
.cwide .lw{overflow:auto}
|
||||
.cols > .c3{min-width:340px;max-width:460px}
|
||||
.ctab{width:100%;min-width:860px;border-collapse:collapse;font-size:12.5px;white-space:nowrap}
|
||||
/* минимальные ширины колонок: текст не сжимается в нечитаемое */
|
||||
.ctab th:nth-child(1),.ctab td:nth-child(1){min-width:120px}
|
||||
.ctab th:nth-child(2),.ctab td:nth-child(2){min-width:90px}
|
||||
.ctab th:nth-child(3),.ctab td:nth-child(3){min-width:100px}
|
||||
.ctab th:nth-child(4),.ctab td:nth-child(4){min-width:140px}
|
||||
.ctab th:nth-child(5),.ctab td:nth-child(5){min-width:130px}
|
||||
.ctab th:nth-child(6),.ctab td:nth-child(6){min-width:130px}
|
||||
.ctab th:nth-child(7),.ctab td:nth-child(7){min-width:150px}
|
||||
.ctab th{position:sticky;top:0;background:var(--bg2);text-align:left;font-weight:600;font-size:11.5px;
|
||||
color:var(--fg2);padding:6px 8px;border-bottom:1px solid var(--border);cursor:pointer;white-space:nowrap}
|
||||
.ctab th:hover{color:var(--fg)}
|
||||
.ctab .sarr{font-size:8px;margin-left:3px;color:var(--accent)}
|
||||
.ctab td{padding:4px 8px;border-bottom:1px solid var(--border);vertical-align:top;max-width:220px}
|
||||
.ctab td{padding:4px 8px;border-bottom:1px solid var(--border);vertical-align:middle}
|
||||
.ctab td .ed .cellv{display:block;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
||||
.ctab td .ed input{width:100%;min-width:80px;padding:1px 4px;font:inherit;font-size:12px;
|
||||
border:1px solid var(--accent);border-radius:4px;background:var(--bg);color:var(--fg)}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{% macro edit_value(contact, field) -%}
|
||||
<div class="ed" x-data="{edit:false, val:{{ (contact.get(field) or '')|tojson }}, err:''}">
|
||||
<span class="cellv" x-show="!edit" x-on:click.stop="edit=true"
|
||||
title="клик — редактировать">{{ contact.get(field) or '—' }}</span>
|
||||
title="{{ contact.get(field) or '—' }} — клик, чтобы изменить">{{ contact.get(field) or '—' }}</span>
|
||||
<template x-if="edit">
|
||||
<input x-model="val" x-on:click.stop x-on:keyup.enter="window.CTSave($data,'{{ contact.id }}','{{ field }}',val)"
|
||||
x-on:keyup.escape="edit=false" x-on:blur="edit=false"
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
<!-- колонка 1: фильтры + алфавитный указатель -->
|
||||
<div class="c1 cfit">
|
||||
<div class="ch"><span>👥 Фильтры</span><span class="cnt">{{ contacts|length }} из {{ total }}</span></div>
|
||||
<div class="ch"><span>👥 Фильтры</span><span class="cnt">{{ found }} из {{ total }}</span></div>
|
||||
<form class="cfilters" hx-get="/contacts" hx-target="#contacts-cols" hx-swap="outerHTML"
|
||||
hx-trigger="input changed delay:350ms, search, submit" hx-push-url="true"
|
||||
onsubmit="return false" autocomplete="off">
|
||||
@@ -36,6 +36,8 @@
|
||||
<button type="submit" class="btn">Найти</button>
|
||||
<a class="btn" href="/contacts">Сбросить</a>
|
||||
</div>
|
||||
<input type="hidden" name="letter" value="{{ letter_param }}">
|
||||
<input type="hidden" name="per_page" value="{{ per_page }}">
|
||||
{% if sort != 'last_name' %}<input type="hidden" name="sort" value="{{ sort }}">{% endif %}
|
||||
{% if dir != 'asc' %}<input type="hidden" name="dir" value="{{ dir }}">{% endif %}
|
||||
</form>
|
||||
@@ -48,10 +50,13 @@
|
||||
</datalist>
|
||||
|
||||
<div class="alpha">
|
||||
<a class="al all{% if not letter %} act{% endif %}" title="весь список"
|
||||
hx-get="{{ params_url('/contacts', params, letter='all', page=1) }}"
|
||||
hx-target="#contacts-cols" hx-swap="outerHTML" hx-push-url="true">Все</a>
|
||||
{% for a in alphabet %}
|
||||
{% if a.count %}
|
||||
<a class="al{% if letter == a.letter %} act{% endif %}" title="{{ a.count }} конт."
|
||||
hx-get="{{ params_url('/contacts', params, letter=('' if letter == a.letter else a.letter)) }}"
|
||||
hx-get="{{ params_url('/contacts', params, letter=('all' if letter == a.letter else a.letter), page=1) }}"
|
||||
hx-target="#contacts-cols" hx-swap="outerHTML" hx-push-url="true">{{ a.letter }}</a>
|
||||
{% else %}
|
||||
<span class="al off" title="нет контактов">{{ a.letter }}</span>
|
||||
@@ -70,6 +75,22 @@
|
||||
<div class="lw" id="clist">
|
||||
{% include "partials/contacts_table.html" %}
|
||||
</div>
|
||||
<div class="cpager">
|
||||
<button class="btn" {% if page_no > 1 %}
|
||||
hx-get="{{ params_url('/contacts', params, page=page_no - 1) }}"
|
||||
hx-target="#contacts-cols" hx-swap="outerHTML" hx-push-url="true"{% else %}disabled{% endif %}>← Назад</button>
|
||||
<span class="pinfo">стр. {{ page_no }} из {{ pages }}</span>
|
||||
<button class="btn" {% if page_no < pages %}
|
||||
hx-get="{{ params_url('/contacts', params, page=page_no + 1) }}"
|
||||
hx-target="#contacts-cols" hx-swap="outerHTML" hx-push-url="true"{% else %}disabled{% endif %}>Вперёд →</button>
|
||||
<label class="pper">на странице
|
||||
<select name="per_page" hx-get="/contacts" hx-include="this"
|
||||
hx-vals="{{ params_json|tojson|forceescape }}"
|
||||
hx-target="#contacts-cols" hx-swap="outerHTML" hx-push-url="true">
|
||||
{% for n in per_page_options %}<option value="{{ n }}"{% if n == per_page %} selected{% endif %}>{{ n }}</option>{% endfor %}
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
<button class="ctop" id="ctop" title="Наверх, к фильтрам"
|
||||
onclick="var l=document.getElementById('clist'); if(l) l.scrollTop=0; var q=document.getElementById('cq'); if(q) q.focus();">↑</button>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user