不要在首次加载时加载HTML模板

huangapple go评论72阅读模式
英文:

Don't load HTML template on first load

问题

以下是您要翻译的内容:

HTML

<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <script src="script.js" defer></script>
    <title>Document</title>
  </head>

  <body>
    <div class="search-wrapper">
      <input type="search" id="search"  placeholder="Search the catalogue" data-search>
    </div>
    <div class="user-cards" data-user-cards-container></div>
    <template data-user-template>
      <div class="card">
        <a href="https://www.url.com/#">
          <div class="header" data-header></div>
        </a>
        <div class="body" data-body></div>
      </div>
    </template>
  </body>

JavaScript

const userCardTemplate = document.querySelector("[data-user-template]")
const userCardContainer = document.querySelector("[data-user-cards-container]")
const searchInput = document.querySelector("[data-search]")

let users = []

searchInput.addEventListener("input", e => {

  const value = e.target.value.toLowerCase()
  const xy = value.split(' ')

  users.forEach(user => {

    if (parseFloat(value.length) < 3) {
      user.element.classList.toggle("hide", true)
    } else {
      var distance = Math.sqrt(
        Math.pow(parseFloat(xy[0]) - parseFloat(user.x), 2) +
        Math.pow(parseFloat(xy[1]) - parseFloat(user.y), 2))
      const isVisible =
        user.name.toLowerCase().includes(value) || distance <= 10
      user.element.classList.toggle("hide", !isVisible)
    }
  })
})

fetch("https://ucc.ar/_clusters/test.json")
  .then(res => res.json())
  .then(data => {
    users = data.map(user => {
      const card = userCardTemplate.content.cloneNode(true).children[0]
      const header = card.querySelector("[data-header]")
      const body = card.querySelector("[data-body]")
      header.textContent = user.name
      body.textContent = user.company
      userCardContainer.append(card)
      return {
        name: user.name,
        x: user.x,
        y: user.y,
        element: card
      }
    })
  })

CSS

.search-wrapper {
  display: flex;
  flex-direction: column;
  gap: .25rem;
}

input {
  width: 80%;
  margin: 0 auto;
  display: block;
  padding: 12px 20px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
  font-size: 18px;
  background-color: #ffffff;
}

.user-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: .25rem;
  margin-top: 1rem;
}

.card {
  border: 1px solid black;
  background-color: white;
  padding: .5rem;
  text-align: center;
}

.card > .header {
  margin-bottom: .25rem;
}

.card > .body {
  font-size: .8rem;
  color: #777;
  font-weight: bold;
}

.hide {
  display: none;
}

请告诉我,您是否需要任何其他帮助。

英文:

I have a basic HTML template that is populated by cards through a search bar given some conditions. This works fine, the only problem is that on first load all the cards are shown and I want none shown until the conditions are met.

This is my code: https://jsfiddle.net/barkwegn/6/

HTML

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;styles.css&quot;&gt;
&lt;script src=&quot;script.js&quot; defer&gt;&lt;/script&gt;
&lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class=&quot;search-wrapper&quot;&gt;
&lt;input type=&quot;search&quot; id=&quot;search&quot;  placeholder=&quot;Search the catalogue&quot; data-search&gt;
&lt;/div&gt;
&lt;div class=&quot;user-cards&quot; data-user-cards-container&gt;&lt;/div&gt;
&lt;template data-user-template&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;a href=&quot;https://www.url.com/#&quot;&gt;
&lt;div class=&quot;header&quot; data-header&gt;&lt;/div&gt;
&lt;/a&gt;
&lt;div class=&quot;body&quot; data-body&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/template&gt;
&lt;/body&gt;

Javascript

const userCardTemplate = document.querySelector(&quot;[data-user-template]&quot;)
const userCardContainer = document.querySelector(&quot;[data-user-cards-container]&quot;)
const searchInput = document.querySelector(&quot;[data-search]&quot;)
let users = []
searchInput.addEventListener(&quot;input&quot;, e =&gt; {
const value = e.target.value.toLowerCase()
const xy = value.split(&#39; &#39;)
users.forEach(user =&gt; {
if (parseFloat(value.length) &lt; 3) {
user.element.classList.toggle(&quot;hide&quot;, true)
} else {
var distance = Math.sqrt(
Math.pow(parseFloat(xy[0]) - parseFloat(user.x), 2) +
Math.pow(parseFloat(xy[1]) - parseFloat(user.y), 2))
const isVisible =
user.name.toLowerCase().includes(value) || distance &lt;= 10
user.element.classList.toggle(&quot;hide&quot;, !isVisible)
}
})
})
fetch(&quot;https://ucc.ar/_clusters/test.json&quot;)
.then(res =&gt; res.json())
.then(data =&gt; {
users = data.map(user =&gt; {
const card = userCardTemplate.content.cloneNode(true).children[0]
const header = card.querySelector(&quot;[data-header]&quot;)
const body = card.querySelector(&quot;[data-body]&quot;)
header.textContent = user.name
body.textContent = user.company
userCardContainer.append(card)
return {
name: user.name,
x: user.x,
y: user.y,
element: card
}
})
})

CSS

.search-wrapper {
display: flex;
flex-direction: column;
gap: .25rem;
}
input {
width: 80%;
margin: 0 auto;
display: block;
padding: 12px 20px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
font-size: 18px;
background-color: #ffffff;
}
.user-cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
gap: .25rem;
margin-top: 1rem;
}
.card {
border: 1px solid black;
background-color: white;
padding: .5rem;
text-align: center;
}
.card &gt; .header {
margin-bottom: .25rem;
}
.card &gt; .body {
font-size: .8rem;
color: #777;
font-weight: bold;
}
.hide {
display: none;
}

答案1

得分: 1

在初始的fetch中创建元素时,将hide类添加到这些元素。

const card = userCardTemplate.content.cloneNode(true).children[0]
const header = card.querySelector("[data-header]")
const body = card.querySelector("[data-body]")
header.textContent = user.name
body.textContent = user.company
card.classList.add('hide');

你也可以直接在模板中将其添加到卡片。

英文:

Add the hide class to the elements when creating them in the initial fetch.

const card = userCardTemplate.content.cloneNode(true).children[0]
const header = card.querySelector(&quot;[data-header]&quot;)
const body = card.querySelector(&quot;[data-body]&quot;)
header.textContent = user.name
body.textContent = user.company
card.classList.add(&#39;hide&#39;);

You could also directly add it to the card in the template.

huangapple
  • 本文由 发表于 2023年4月11日 06:28:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定