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

View file

@ -19,7 +19,15 @@
if (selected === null)
hint.innerText = ""
else {
hint.innerHTML = selected.innerHTML
const linkPrefix = input.list.dataset.linkPrefix;
if (linkPrefix === undefined) {
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 => {