【问题标题】:Search on Collapsible contents not working in angular搜索无法以角度工作的可折叠内容
【发布时间】:2019-06-21 08:32:15
【问题描述】:

我已经在我的 Angular 应用程序中实现了可折叠。但是那些可折叠的内容来自数据库服务,我在 ngFor as- 的帮助下设置了这些可折叠的内容-

 <input type="text" name="search" class="form-control" id="myInput" onkeyup="myFAQsSearchFun()" placeholder="Enter your query " >


 <div *ngFor="let item of faqs;let i = index;" id="div2">
     <button class="accordion" (click)="toggleAccordian($event, i)">
         <a style="text-decoration:none;" > {{item.question}}</a> 
     </button>

     <div class="panel"  hide="!item.isActive">
         <p><br> {{item.answer}} </p>
     </div>
     <br>
 </div>

Collapsible 工作正常,但问题是我想根据我在搜索栏中输入的内容来搜索这些内容。为此,我实现了以下代码-

  function myFAQsSearchFun() {
                var input, filter, ul, li, a, i, txtValue;
                input = document.getElementById("myInput");
                filter = input.value.toUpperCase();
                ul = document.getElementById("div2");
                li = ul.getElementsByTagName("button");
                window.alert(li.length);
                for (i = 0; i < li.length; i++) {
                    a = li[i].getElementsByTagName("a")[0];
                    txtValue = a.textContent || a.innerText;
                    if (txtValue.toUpperCase().indexOf(filter) > -1) {
                        li[i].style.display = "";
                    } else {
                        li[i].style.display = "none";
                    }
                }
            }

Window.alert 输出为“1”。但是 ngFor 循环了 3 次,因为我可以看到 3 个可折叠项。

我做错了什么。请帮忙。

提前致谢!

【问题讨论】:

    标签: javascript html css angular typescript


    【解决方案1】:
    • 不要使用 document.getElementById("myInput"),而是使用 Angular 表单来获取输入。
    • 您将在控制器中获得以 HTML 格式显示的数据,因此无需循环通过 DOM,您只需过滤控制器本身中的数组即可。
    • 过滤后,只需为常见问题中的每个项目添加一个标志即可隐藏或显示它们。
    • 根据上述标志在 HTML 中添加 ngIF。

    HTML:

    <input [(ngModel)]="model.search" >
    <div *ngFor="let item of faqs;let i = index;" >
      <div *ngIf="!item.hidden">
         <button class="accordion" (click)="toggleAccordian($event, i)">
            <a style="text-decoration:none;" > {{item.question}}</a> 
         </button>
    
         <div class="panel"  hide="!item.isActive">
            <p><br> {{item.answer}} </p>
         </div>
         <br>
       </div>
     </div>
    

    JS:

           model = {search: ''};
           faqs = [{question:'asf',answer:'asd'}, {question:'asf',answer:'asd'}]
           myFAQsSearchFun() {
                var input, filter, ul, li, a, i, txtValue;
                input = model.search;
                filter = input.toUpperCase();
                faqs.forEach((faq) => {
                   if (faq.question.toUpperCase().indexOf(filter) > -1) {
                      faq.hidden = false;
                   } else {
                      faq.hidden = true;
                   }
                });
            }
    

    【讨论】:

    • 你能举个例子/代码你想说的话。我没有得到这个
    猜你喜欢
    • 2014-12-19
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 2012-12-01
    相关资源
    最近更新 更多