Active Search

์ด ์˜ˆ์‹œ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•  ๋•Œ ์—ฐ๋ฝ์ฒ˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

๋จผ์ €, ๊ฒ€์ƒ‰ ์ž…๋ ฅ ํ•„๋“œ์™€ ๋นˆ ํ…Œ์ด๋ธ”์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค:

<h3> 
  Search Contacts 
  <span class="htmx-indicator"> 
    <img src="/img/bars.svg"/> Searching... 
   </span> 
</h3>
<input class="form-control" type="search" 
       name="search" placeholder="Begin Typing To Search Users..." 
       hx-post="/search" 
       hx-trigger="input changed delay:500ms, search" 
       hx-target="#search-results" 
       hx-indicator=".htmx-indicator">

<table class="table">
    <thead>
    <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Email</th>
    </tr>
    </thead>
    <tbody id="search-results">
    </tbody>
</table>

์ด ์ž…๋ ฅ ํ•„๋“œ๋Š” input ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ /search๋กœ POST ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ , ํ…Œ์ด๋ธ”์˜ ๋ณธ๋ฌธ์„ ๋ฐ˜ํ™˜๋œ ๋‚ด์šฉ์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. keyup ์ด๋ฒคํŠธ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์‚ฌ์šฉ์ž๊ฐ€ ๋งˆ์šฐ์Šค๋กœ ํ…์ŠคํŠธ๋ฅผ ๋ถ™์—ฌ๋„ฃ๋Š” ๋“ฑ์˜ ๋น„ํ‚ค๋ณด๋“œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•  ๋•Œ๋Š” ํŠธ๋ฆฌ๊ฑฐ๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๋Š” delay:500ms ์ˆ˜์ •์ž๋ฅผ ํŠธ๋ฆฌ๊ฑฐ์— ์ถ”๊ฐ€ํ•˜์—ฌ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅ์„ ๋ฉˆ์ถœ ๋•Œ๊นŒ์ง€ ์ฟผ๋ฆฌ ์ „์†ก์„ ์ง€์—ฐ์‹œํ‚ต๋‹ˆ๋‹ค. ๋˜ํ•œ, changed ์ˆ˜์ •์ž๋ฅผ ํŠธ๋ฆฌ๊ฑฐ์— ์ถ”๊ฐ€ํ•˜์—ฌ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅ ๊ฐ’์— ๋ณ€ํ™”๋ฅผ ์ฃผ์ง€ ์•Š์•˜์„ ๋•Œ(์˜ˆ: ํ™”์‚ดํ‘œ ํ‚ค๋ฅผ ๋ˆ„๋ฅด๊ฑฐ๋‚˜ ๋™์ผํ•œ ๊ฐ’์„ ๋ถ™์—ฌ๋„ฃ์—ˆ์„ ๋•Œ) ์ƒˆ๋กœ์šด ์ฟผ๋ฆฌ๊ฐ€ ์ „์†ก๋˜์ง€ ์•Š๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

search ํƒ€์ž…์˜ ์ž…๋ ฅ ํ•„๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ž…๋ ฅ ํ•„๋“œ์— ์ž…๋ ฅ์„ ์ง€์šธ ์ˆ˜ ์žˆ๋Š” x ๋ฒ„ํŠผ์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ์„ ์ง€์› ์„ ๋•Œ ์ƒˆ๋กœ์šด POST ์š”์ฒญ์ด ํŠธ๋ฆฌ๊ฑฐ๋˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค๋ฅธ ํŠธ๋ฆฌ๊ฑฐ๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํŠธ๋ฆฌ๊ฑฐ๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด ์‰ผํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ํŠธ๋ฆฌ๊ฑฐ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. search ํŠธ๋ฆฌ๊ฑฐ๋Š” ํ•„๋“œ๋ฅผ ์ง€์› ์„ ๋•Œ ์‹คํ–‰๋˜๋ฉฐ, ์—”ํ„ฐ ํ‚ค๋ฅผ ๋ˆŒ๋Ÿฌ 500ms์˜ input ์ด๋ฒคํŠธ ์ง€์—ฐ์„ ๋ฌด์‹œํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ, hx-indicator ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒ€์ƒ‰์ด ์ง„ํ–‰ ์ค‘์ผ ๋•Œ ์ธ๋””์ผ€์ดํ„ฐ๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

Server Requests ↑ Show

๐Ÿ”—Demo