0

I've got tabular data loaded in AngularJS app. I want to display them in a table. But I also want to have detail view for some rows. Something like:

<table>
    <tr ng-repeat="datum in data">
        <!-- if datum.showDetail => detail view -->

            <td colspan="N">
                <h1>{{ datum.field1 }} <small>{{ datum.field2 }}</small></h1>
                ...
                <p>{{ datum.fieldN }}</p>
            </td>

        <!-- else => row view -->

            <td>{{ datum.field1 }}</td>
            <td>{{ datum.field2 }}</td>
            ...
            <td>{{ datum.fieldN }}</td>
    </tr>
</table>

What's the Angular way of doing this?

1 Answer 1

1

Why not just adding ng-show and ng-hide directives to the td's?

<table>
    <tr ng-repeat="datum in data" >
        <!-- if datum.showDetail => detail view -->

            <td colspan="N" ng-show="datum.showDetail">
                <h1>{{ datum.field1 }} <small>{{ datum.field2 }}</small></h1>
                ...
                <p>{{ datum.fieldN }}</p>
            </td>

        <!-- else => row view -->

            <td ng-hide="datum.showDetail">{{ datum.field1 }}</td>
            <td ng-hide="datum.showDetail">{{ datum.field2 }}</td>
            ...
            <td ng-hide="datum.showDetail">{{ datum.fieldN }}</td>
    </tr>
</table>

Also note that, in case you're using the Angular UI extensions, the ui-if directive would probably be a better fit, since it would actually remove the unused td's from the table, instead of just hiding them, like ng-hide.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for pointing out ui-if directive. That is exactly what I need.
as of Angular 1.1.4 ui-if is now included in the core as ng-if

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.