Allow displaying datalist hint as link

This commit is contained in:
Simon Bruder 2024-07-19 00:03:37 +02:00
parent 8aff438f98
commit 8e61f34b3f
Signed by: simon
GPG key ID: 347FF8699CDA0776
2 changed files with 13 additions and 2 deletions

View file

@ -11,6 +11,7 @@ use crate::manage;
pub struct Datalist { pub struct Datalist {
name: String, name: String,
options: Vec<DatalistOption>, options: Vec<DatalistOption>,
link_prefix: Option<String>,
} }
impl Datalist { impl Datalist {
@ -22,7 +23,7 @@ impl Datalist {
impl Render for Datalist { impl Render for Datalist {
fn render(&self) -> Markup { fn render(&self) -> Markup {
html! { html! {
datalist #{ (self.name) "-datalist" } { datalist #{ (self.name) "-datalist" } data-link-prefix=[&self.link_prefix] {
@for option in &self.options { @for option in &self.options {
(option) (option)
} }
@ -49,6 +50,7 @@ pub async fn items(pool: &PgPool) -> Result<Datalist, sqlx::Error> {
Ok(Datalist { Ok(Datalist {
name: "items".to_string(), name: "items".to_string(),
link_prefix: Some("/item/".to_string()),
options: items options: items
.iter() .iter()
.map(|i| DatalistOption { .map(|i| DatalistOption {
@ -62,6 +64,7 @@ pub async fn items(pool: &PgPool) -> Result<Datalist, sqlx::Error> {
pub async fn item_classes(pool: &PgPool) -> Result<Datalist, sqlx::Error> { pub async fn item_classes(pool: &PgPool) -> Result<Datalist, sqlx::Error> {
Ok(Datalist { Ok(Datalist {
name: "item-classes".to_string(), name: "item-classes".to_string(),
link_prefix: Some("/item-class/".to_string()),
options: manage::item_class::get_all(pool) options: manage::item_class::get_all(pool)
.await? .await?
.into_iter() .into_iter()

View file

@ -19,7 +19,15 @@
if (selected === null) if (selected === null)
hint.innerText = "" hint.innerText = ""
else { else {
const linkPrefix = input.list.dataset.linkPrefix;
if (linkPrefix === undefined) {
hint.innerHTML = selected.innerHTML hint.innerHTML = selected.innerHTML
} else {
let link = document.createElement("a")
link.href = `${linkPrefix}${input.value}`
link.innerHTML = selected.innerHTML
hint.appendChild(link)
}
} }
} }
Array.from(document.getElementsByClassName("datalist-hint")).forEach(hint => { Array.from(document.getElementsByClassName("datalist-hint")).forEach(hint => {