【问题标题】:Comma separated keyword search逗号分隔的关键字搜索
【发布时间】:2018-12-18 04:08:56
【问题描述】:

我正在使用过滤条件过滤表中的行。

<tr *ngFor="let result  of mf.data | filter:filter; let i = index"> 

我们使用 ng2-search-filter

目前,它的过滤基于关键字,但我想在输入逗号分隔格式的关键字时过滤行。

如何更改上面的行以使其正常工作?

全桌

<table id="groups" class="table table-bordered table-hover dataTable" [mfData]="resultData" #mf="mfDataTable" [mfRowsOnPage]="rowsOnPage"
                        [(mfSortBy)]="sortBy" [(mfSortOrder)]="sortOrder">
                      <thead>
                      <tr role="row">

                        <th>  <mfDefaultSorter by="HostName">Host Name</mfDefaultSorter></th>
                        <!-- <th><mfDefaultSorter by="SupportDL">Support DL</mfDefaultSorter></th>  -->
                        <th><mfDefaultSorter by="Connectivity">Connectivity</mfDefaultSorter></th>
                        <th><mfDefaultSorter by="RDPStatus">RDP Status</mfDefaultSorter></th>
                        <th><mfDefaultSorter by="MemoryStatus">Memory Status</mfDefaultSorter></th>
                        <th><mfDefaultSorter by="CPUStatus">CPU Status</mfDefaultSorter></th>
                        <th><mfDefaultSorter by="ServiceStatus">Service Status</mfDefaultSorter></th>
                        <th><mfDefaultSorter by="ServiceLogStatus">Service Log Status</mfDefaultSorter></th>
                        <th><mfDefaultSorter by="DiskStatus">Disk Status</mfDefaultSorter></th>
                        <th><mfDefaultSorter by="LogTime">Log Time</mfDefaultSorter></th>


                        <!-- <th>Actions</th> -->
                      </tr>
                      </thead>
                      <tbody *ngIf="resultData.length>0">
                      <tr *ngFor="let result  of mf.data | filter:filter; let i = index">

                        <td>{{result.HostName}}</td>
                        <!-- <td>{{result.SupportDL}}</td> -->
                        <td>{{result.Connectivity}}</td>
                        <td>{{result.RDPStatus}}</td>
                        <td>{{result.MemoryStatus}}</td>
                        <td>{{result.CPUStatus}}</td>
                        <td><pre>{{result.ServiceStatus}}</pre></td>
                        <td>{{result.ServiceLogStatus}}</td>
                        <td><pre>{{result.DiskStatus}}</pre></td>
                        <td>{{result.LogTime}}</td>

                      </tr>
                    </tbody>
                    <tr *ngIf="resultData.length <=0"><td colspan="10"> No Data Found</td></tr>
                    <tfoot>
                        <tr>
                            <td colspan="8">
                                <mfBootstrapPaginator></mfBootstrapPaginator>
                            </td>
                        </tr>
                        </tfoot>
                    </table>

【问题讨论】:

    标签: angular pipe angular2-directives


    【解决方案1】:

    如果过滤器是一个简单的输入:

    <input type="query" [(ngModel)]="filter">
    

    那么你就不用改变你的 HTML 了。

    由于您没有发布您的实际过滤器(并且没有将您之前的问题标记为已解决,顺便说一句,谢谢您),我将使用我给您的过滤器并稍作更改:

    import { Pipe, PipeTransform } from '@angular/core';
    
    @Pipe({
      name: 'filter',
      pure: true
    })
    export class FilterPipe implements PipeTransform {
      transform(items: Object[], args: string): any {
        console.log(args);
    
        if (!items || !items.length) { return []; }
    
        if (!args) { return items; }
    
        return items
          .filter(item => Object.keys(item)
            .some(key => args.split(',').some(arg => item[key].toLowerCase().includes(arg.toLowerCase())))
          );
      }
    }
    

    还有here is a working stackblitz,以及它适用于图像的证明:

    【讨论】:

    • 我们使用 ng2-search-filter
    • 是的,也许你应该在你原来的问题中说明这一点......正如你所看到的,你不需要它。
    • 我不知道你在说什么,两行都在显示。我使用您自己的过滤器创建了这个过滤器,它不接受空格。
    • 正如我所说,它不接受空格作为您的原始过滤器。删除空格,它将起作用。
    • 我在用手机,它就像一个魅力,你可以在我的编辑中看到。你至少得为此付出一些努力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 2016-04-24
    • 2013-06-21
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多