Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-table filter with nested properties

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>
like image 213
Piotr Stapp Avatar asked Nov 01 '22 14:11

Piotr Stapp


1 Answers

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,
like image 110
Kostia Mololkin Avatar answered Nov 11 '22 05:11

Kostia Mololkin