-->

ng-table : filter on column

2019-09-11 14:18发布

问题:

I am using ng-table and I want to filter my columns. It works with a simple data.

<table ng-table="tableParams"  show-filter="true" class="table table-striped table-bordered table-hover" id="dynamic-table">
    <tr ng-repeat="consultant in $data">
        <td  class="text-center" data-title="'Ville résidence'" filter="{ 'villeResidence': 'text' }">{{consultant.villeResidence}}</td>
        <td  class="text-center" data-title="'Contrat'" filter="{ 'typeContrat.contrat': 'text' }" >
            <span ng-repeat="typeContrat in consultant.typeContrats">
                <a class="form-control-static" ui-sref="type-contrat-detail({id: {{typeContrat.id}}})">{{typeContrat.contrat}}</a>{{$last ? '' : ', '}}
            <span>
        </td>
    </tr>
</table>

But with a more complexe column(here : "contrat") I don't know how to do that. If anyone knows...

[UPDATE]

<td class="text-center" data-title="'Contrat'" filter="{ 'typeContrat.contrat': 'text' }" data-title-text="Contrat">
        <!-- ngRepeat: typeContrat in consultant.typeContrats -->
    <span ng-repeat="typeContrat in consultant.typeContrats" class="ng-binding ng-scope">
        <a class="form-control-static ng-binding" ui-sref="type-contrat-detail({id: 1})" href="#/type-contrat/1">CDI</a>, 
        <span>    
        </span>
    </span>
    <!-- end ngRepeat: typeContrat in consultant.typeContrats -->
    <span ng-repeat="typeContrat in consultant.typeContrats" class="ng-binding ng-scope">
            <a class="form-control-static ng-binding" ui-sref="type-contrat-detail({id: 2})" href="#/type-contrat/2">Freelance</a>, 
        <span></span>
    </span>
    <!-- end ngRepeat: typeContrat in consultant.typeContrats -->
    <span ng-repeat="typeContrat in consultant.typeContrats" class="ng-binding ng-scope">
            <a class="form-control-static ng-binding" ui-sref="type-contrat-detail({id: 3})" href="#/type-contrat/3">Portage</a>
        <span></span>
    </span><!-- end ngRepeat: typeContrat in consultant.typeContrats -->
</td>

回答1:

As mentioned here and as I looked around in the docs, I concluded ng-table is not well suited for nested models.

A workaround could be to create a property based on your array data of contats and then filter by it. So, for each consultant, I added a property contracts which contains the concatenated string of its typeContrats.contats:

_.forEach(consultantData, function(value) {
    var contrats = _.map(value.typeContrats, 'contrat');
    value.contrats = _.join(contrats, ', ');
});

And after that in the HTML:

<td class="text-center" data-title="'Contrat'" filter="{ 'contrats': 'text' }">

Here is a fiddle with the example.

I hope this is what you were trying to achieve.