0

I need to dynamically display a table in Angular, depending on the info coming from the DB

So far, I have this Info

[ { "BET": 57630343, "CUSTOMER": 181645, "XX_FILL OPEN": true },
  { "BET": 57633044, "CUSTOMER": 181645, "XX_FILL OPEN": true },
  { "BET": 57633047, "CUSTOMER": 181645, "XX_FILL OPEN": true },
  { "BET": 57635034, "CUSTOMER": 181645, "XX_FILL OPEN": true } ]

everything with XX... should be a button

and in the HTML

      <table>
        <thead>
          <tr>
            <th>Bet</th>
            <th>Customer</th>
            <th>Fill Open Bet</th>
          </tr>
        </thead>
        <tbody>
          <tr ng-repeat="pendingBet in pendingBets">
            <td> {{::pendingBet.BET}}</td>
            <td> {{::pendingBet.CUSTOMER}}</td>
            <td><button class="btn btn-danger">Fill</button></td> <!--{{pendingBet.XX_FILL_OPEN}}-->
          </tr>
        </tbody>
      </table>

the issue I have here is: above I have something static like

            <th>Bet</th>
            <th>Customer</th>
            <th>Fill Open Bet</th>

but sometimes <th>Bet</th> which is <td> {{::pendingBet.BET}}</td> is not coming from the DB, so I don't have to display it, so I want to know what should I do to display it dynamically

what should I do in this case?

EDIT

Let explain myself better:

according to the table, <td> {{::pendingBet.BET}}</td> belongs to <th>Bet</th>, but sometimes pendingBet.BET is null so I don't have to display all that column <th>Bet</th> and <td> {{::pendingBet.BET}}</td>, the same with Customer and Fill Open.

7
  • 1
    Not sure what you are asking. Are you wanting to remove the whole column if none of the rows have pendingBet.BET? Commented Sep 9, 2015 at 22:42
  • @charlietfl yes. I BET, or CUSTOMER or the other are not coming from the DB, then I should not display that row neither the header. Commented Sep 9, 2015 at 22:43
  • Oh... just row...can use ng-if on the <tr> for that. Or filter the data first in controller Commented Sep 9, 2015 at 22:43
  • @charlietfl why on the <tr> ? is doesn't make any sense, if I apply to the whole <tr>, then when BET isn't coming, nothing will show up. Commented Sep 9, 2015 at 22:46
  • 1
    Then it is not clear what you are expecting Commented Sep 9, 2015 at 22:47

2 Answers 2

1

You can use ng-if on the <th> and <td>:

<table>
    <thead>
      <tr>
        <th ng-if="hasBetColumn">Bet</th>
        <th>Customer</th>
        <th>Fill Open Bet</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="pendingBet in pendingBets">
        <td ng-if="hasBetColumn"> {{::pendingBet.BET}}</td>
        <td> {{::pendingBet.CUSTOMER}}</td>
        <td><button class="btn btn-danger">Fill</button></td> <!--{{pendingBet.XX_FILL_OPEN}}-->
      </tr>
    </tbody>
  </table>

It's up to you to determine how you want to set hasBetColumn, either in your controller or as part of the returned data from your API call.

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

Comments

1

If all the fields are dynamic:

In you ctrl, you need a array to store the fields you want to display, if you assign it with Object.keys(data[0]) every time you initialize the ctrl, you may not see the same table because properties order in objects are not guaranteed in js.

in ctrl

//Leat's say you have lodash (or underscore, if you dont you should...)
$scope.fields = _.intersection( ["BET", "CUSTOMER", "whatever"],
  _.keys(data ? data[0] : {})
)

in html

<tbody>  
  <!-- ng-repeat on fields in `thead ` -->
  <tr ng-repeat="pendingBet in pendingBets">
      <td ng-repeat="field in fields" ng-bind="::pendingBet[field]"></td>
  </tr>
</tbody>

2 Comments

the <th> part is also dynamic.
Yes. just ng-repeat on $scope.fields because $scope.fields will be dynamic based on your data's keys.

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.