Search code examples
htmlangularjsangularjs-ng-repeatsingle-page-application

How do I run a ng-repeat loop inside a table in html?


                <thead>
                    <tr>

                        <th ng-click="sortData('firstname')">
                            Firstname <div ng-class="getSortClass('firstname')"></div>
                        </th>

                        <th ng-click="sortData('lastname')">
                            Lastname <div ng-class="getSortClass('lastname')"></div>
                        </th>


                        <th ng-click="sortData('email')">
                            Email <div ng-class="getSortClass('email')"></div>
                        </th>


                        <th ng-click="sortData('university')">
                            University <div ng-class="getSortClass('university')"></div>
                        </th>

                        <th ng-click="sortData('githublink')">
                            Github Link <div ng-class="getSortClass('githublink')"></div>
                        </th>

                        <th ng-click="sortData('bamboolink')">
                            Bamboo <div ng-class="getSortClass('bamboolink')"></div>
                        </th>

                        <th ng-click="sortData('backendnote')">
                            Backend <div ng-class="getSortClass('backendnote')"></div>
                        </th>

                        <th ng-click="sortData('frontend')">
                            Frontend <div ng-class="getSortClass('frontend')"></div>
                        </th>

                        <th ng-click="sortData('algorithms')">
                            Algorithms <div ng-class="getSortClass('algorithms')"></div>
                        </th>

                        <th ng-click="sortData('specialnote')">
                            Special notes <div ng-class="getSortClass('specialnote')"></div>
                        </th>

                        <th>
                            Extra column1
                        </th>

                        <th>
                            Extra column2
                        </th>

                        <th>
                            Extra column3
                        </th>

                        <th>
                            Delete
                        </th>

                        <th>
                            Edit
                        </th>

                    </tr>

                </thead>

I want to replace these extra column 1, 2 and 3 with ng-repeat since the numbers of columns will change eventually. When I tried this piece of code below

                        <div ng-repeat="a in emps">
                            <th>
                                {{a.intervieweeExtra[$index].columnname}}
                            </th>
                        </div>

it printed nothing. How do I do this in angularjs 1.x? I am new in html and angularjs. I could not find a working solution yet so I am asking this here. Thanks


Solution

  • There is no reason for the <div> element. You should put the ng-repeat directly on the <th> element, i.e.:

    <table>
        <thead>
            <tr>
                <!-- Permanent columns -->
                <th></th>
                <th></th>
    
                <!-- Variable columns -->
                <th ng-repeat="a in emps">{{a.intervieweeExtra[$index].columnname}}</th>
    
                <!-- Permanent columns -->
                <th></th>
                <th></th>
            </tr>
        </thead>
    
        <tbody>
            <tr>
                <!-- Permanent columns -->
                <td></td>
                <td></td>
    
                <!-- Conditional columns -->
                <td ng-repeat="a in emps"></td>
    
                <!-- Permanent columns -->
                <td></td>
                <td></td>
            </tr>
        </tbody>
    </table>
    

    An identical ng-repeat is added to the table-row in the <tbody> element to ensure that the rows in the table-header and table-body contain the same number of columns.

    I think that you also just want to reference {{a.intervieweeExtra.columnname}}, but without knowing your data-structure it's hard to know for sure.