Rodrigo Valente
Rodrigo Valente

Reputation: 319

Smart Table st-search inside nested objects

Is there any way to search inside nested elements in smart-table? I feed the table with data from a REST Api that consists of the following form:

{
    "id": 1,
    "small_name": "Foo",
    "large_name": "Bar Foo",
    "variants": [{"value": "0"}, {"value": "1"}]
}

What I want to achieve is the possibility to filter the data through the value property of the objects inside the variants.

Upvotes: 0

Views: 1551

Answers (2)

Rodrigo Valente
Rodrigo Valente

Reputation: 319

I'll post the solution for my problem, maybe it can help someone.

angular.module('YourModule').filter('CustomFilter', [
    '$parse',
    function ($parse) {
        return function(items, filters) {
            console.log(items, filters);

            var itemsLeft = items.slice();

            Object.keys(filters).forEach(function (model) {
                var value = filters[model],
                    getter = $parse(model);

                itemsLeft = itemsLeft.filter(function (item) {
                    if (model === 'value') {
                        var variants = item.variants.filter(function (variant) {
                           return getter(variant).match(value);
                        });

                        return variants.length;
                    } else {
                        return getter(item).match(value);
                    }
                });
            });

            return itemsLeft;
        }
    }
])

Upvotes: 0

JC Ford
JC Ford

Reputation: 7066

From the Smart Table documentation:

"The stSetFilter replaces the filter used when searching through Smart Table. When the default behavior for stSearch does not meet your demands, like in a select where one entry is a substring of another, use a custom filter to achieve your goals." http://lorenzofox3.github.io/smart-table-website/

There is also an example available at that site.

Upvotes: 1

Related Questions