ng-table filter with nested properties

北战南征 提交于 2019-12-07 05:42:13

问题


I have following JSON:

  [{
    "Id": "1",
    "Data": {"Str1": "Ann", "Str2": "Xenna"}
  },{
    "Id": "2",
    "Data": {"Str1": "Bob","Str2": "Bobby"},
  }]

And I created ng-table to display it. I tried to add filter. When I filter by Id everything works as expected (filter is { "Id": "2" }). But I cannot create proper filter do Str1 and Str2 fields. I already tried:

  1. { "Str1": "A" }
  2. { "Data.Str1": "A" }
  3. { "Data['Str1']": "A" }

but above options does not work.

Example of my work is here: http://plnkr.co/edit/MyJCqTlgvKLtSP63FYQY?p=preview

Update

Thanks to @Blackhole I founded that filter {Data: {Str1: 'A'}} works. But I can only delcare this in code. When I try to put something like this in HTML it doesn't even show filter:

  <td data-title="'Str1'" filter="{Data:{Str1: 'text'}}">
    {{ user.Data.Str1 }}
  </td>

回答1:


When you try to use filter="{Data:{Str1: 'text'}}" in html,input doesn't showing cause of template in header,have a look in source code.

<div ng-repeat="(name, filter) in column.filter"> //!!!! right here it's not supported
        <div ng-if="column.filterTemplateURL" ng-show="column.filterTemplateURL">
            <div ng-include="column.filterTemplateURL"></div>
        </div>
        <div ng-if="!column.filterTemplateURL" ng-show="!column.filterTemplateURL">
            <div ng-include="'ng-table/filters/' + filter + '.html'"></div>
        </div>
    </div>

right here <div ng-repeat="(name, filter) in column.filter"> it's not dig into nested objects

Ngtable not support nested filter in default template,so you can create your own template,which gonna support it.Have a look to example of header template.

Note

This how column.filter is initializing,it's parsing from filter attribute on td tag,source

var parsedAttribute = function (attr, defaultValue) {
                        return function (scope) {
                            return $parse(el.attr('x-data-' + attr) || 
                                          el.attr('data-' + attr) || 
                                          el.attr(attr))
                                (scope, {
                                $columns: columns
                            }) || defaultValue;
                        };
                    };

                    var parsedTitle = parsedAttribute('title', ' '),
                        headerTemplateURL = parsedAttribute('header', false),
                        // here
                        filter = parsedAttribute('filter', false)(),
                        filterTemplateURL = false,
                        filterName = false;

                    ...
                    columns.push({
                        ....
                        filter: filter,


来源:https://stackoverflow.com/questions/26730608/ng-table-filter-with-nested-properties

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