ng-table : filter on column

余生长醉 提交于 2019-12-13 07:14:09

问题


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.



来源:https://stackoverflow.com/questions/36333148/ng-table-filter-on-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!