0

I have a message object which I want to display in a table, I don't want to display all properties of the message so I set some of them false and I want to filter the properties that are set true and display them

Message Object:

message0 = {
    id: {
        wert: 0,
        status: pr_active.id
    },
    category: {
        wert: 'General',
        status: pr_active.category
    },
    icon_id: {
        wert: '1',
        status: pr_active.id.icon_id
    },
    text: {
        wert: 'Success',
        status: pr_active.id.text
    }
};

    scope.messages = [message0, message1, message2];

ngRepeat:

        <tr ng-repeat="message in messages | filter: {category : category}">
            <td ng-repeat="property in message | filter: {status:true}">
                {{property.wert}}
            </td>
        </tr>

I'm using a status-Object which contains the status for every possible property, so that I can connect it to the header of the table

        pr_active = {
        id: 'false',
        category: 'false',
        icon_id: 'true',
        text: 'true',
    }

The problem is, that every property is displayed, even the one's that should be false.

Also the properties are displayed in the wrong order (they are ordered alphabetically), how can I use the default order that I defined

John Smith
  • 615
  • 4
  • 15

2 Answers2

0

You don't define an order on an object in JavaScript (see Does JavaScript Guarantee Object Property Order?). Thus you'd have to define an order for you properties manually. But this might already solve your problem:

Suppose you must define the order of the properties you want to display, then you might as well just define those properties you want to see:

$scope.properties = [
  // 'id', // just use a comment if you don't want to see them
  // 'category',
  'icon_id',
  'text'
]

Then you simply have to iterate over the properties array in your inner ng-repeat:

<tr ng-repeat="message in messages | filter: {category : category}">
  <td ng-repeat="property in properties">
    {{message[property].wert}}
  </td>
</tr>
Community
  • 1
  • 1
Tharabas
  • 3,402
  • 1
  • 30
  • 29
  • I have to use the properties I don't want to display for other things (e.g. the category for checking if the message belongs to the current category-tab), so commenting them out is not an option. The reason why I've chosen an object for every property is that I want to save the display-status so I can't just define the properties in an array but thanks for the array-idea I'll try to work with it – John Smith Jan 17 '14 at 14:38
0

Have you tried?:

pr_active = {
        id: false,
        category: false,
        icon_id: true,
        text: true
    }
Alexander Nenkov
  • 2,910
  • 20
  • 28