如何在Tailwind CSS和Alpine.js中在搜索栏中添加“未找到”选项

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

How to add a not found option in the search bar in Tailwind css and Alpine js

问题

我正在使用tailwindcss和alpine js开发一个项目。我想在其中创建一个搜索栏。我已经创建了搜索栏,但是“未找到”功能没有形成。我尝试了多种方法,但仍然没有得到结果。请帮我在我的代码中添加一个未找到的功能。提前感谢。

这是我的代码:

<ul class="max-h-72 scroll-py-2 overflow-y-auto text-sm text-gray-800" id="options" role="listbox">
    <template x-for="(item, index) in searchResults">
        <li class="cursor-default select-none px-4 py-2" role="option" x-text="item" tabindex="-1"></li>
    </template>
</ul>

即时div的x-data部分是:

x-data="{ 
    isNone:true,
    search: '',
    items: ['Apple', 'Banana', 'Guava', 'Ape', 'Bands'],
    get searchResults () {
        let datalist = this.items.filter(
            i => i.startsWith(this.search)
        );
        isNone = datalist.length == 0;
        return datalist;
    }  
}"

再次提前感谢。

英文:

I am working on a project in tailwindcss and alpine js. I want to make a search bar in it. I have made the search bar but the "Not found" feature is not getting formed. I tried multiple ways still not got the result. Please help me to put a not found feature in my code. Thanks is advance.

Here's my code:

&lt;ul class=&quot;max-h-72 scroll-py-2 overflow-y-auto text-sm text-gray-800&quot; id=&quot;options&quot; role=&quot;listbox&quot;&gt;
     &lt;template x-for=&quot;(item, index) in searchResults&quot;&gt;
          &lt;li class=&quot;cursor-default select-none px-4 py-2&quot; role=&quot;option&quot; x-text=&quot;item&quot; tabindex=&quot;-1&quot;&gt;&lt;/li&gt;
     &lt;/template&gt;
 &lt;/ul&gt;

the x-data part of the immediate div is :

x-data=&quot;{ 
    isNone:true,
    search: &#39;&#39;,
   items: [&#39;Apple&#39;, &#39;Banana&#39;, &#39;Guava&#39;, &#39;Ape&#39;, &#39;Bands&#39;],
    get searchResults () {
      let datalist= this.items.filter(
          i =&gt; i.startsWith(this.search)
      );
      isNone = datalist.length == 0;
      return datalist;
    }  
  }&quot;

Again thanks in advance.

答案1

得分: 0

你需要在组件定义中的每个数据属性前加上 this.,所以你需要在 getter 方法中使用 this.isNone

<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>

<div x-data="{isNone:true,
              search: '',
              items: ['Apple', 'Banana', 'Guava', 'Ape', 'Bands'],
              get searchResults () {
                let datalist = this.items.filter(
                    i => i.startsWith(this.search)
                );
                this.isNone = datalist.length == 0;
                return datalist;
              }}">
  <input type="text" x-model="search" />
  <ul class="max-h-72 scroll-py-2 overflow-y-auto text-sm text-gray-800" id="options" role="listbox">
    <template x-for="(item, index) in searchResults">
      <li class="cursor-default select-none px-4 py-2" role="option" x-text="item" tabindex="-1"></li>
    </template>
  </ul>

  <div x-show="isNone">Not found</div>
</div>
英文:

You need to prefix each data property with this. in the component definition, so you have to use this.isNone in the getter method:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;script defer src=&quot;https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js&quot;&gt;&lt;/script&gt;


&lt;div x-data=&quot;{isNone:true,
              search: &#39;&#39;,
              items: [&#39;Apple&#39;, &#39;Banana&#39;, &#39;Guava&#39;, &#39;Ape&#39;, &#39;Bands&#39;],
              get searchResults () {
                let datalist= this.items.filter(
                    i =&gt; i.startsWith(this.search)
                );
                this.isNone = datalist.length == 0;
                return datalist;
              }}&quot;&gt;
  &lt;input type=&quot;text&quot; x-model=&quot;search&quot; /&gt;
  &lt;ul class=&quot;max-h-72 scroll-py-2 overflow-y-auto text-sm text-gray-800&quot; id=&quot;options&quot; role=&quot;listbox&quot;&gt;
    &lt;template x-for=&quot;(item, index) in searchResults&quot;&gt;
      &lt;li class=&quot;cursor-default select-none px-4 py-2&quot; role=&quot;option&quot; x-text=&quot;item&quot; tabindex=&quot;-1&quot;&gt;&lt;/li&gt;
    &lt;/template&gt;
  &lt;/ul&gt;

  &lt;div x-show=&quot;isNone&quot;&gt;Not found&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月31日 16:31:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896419.html
匿名

发表评论

匿名网友

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

确定