Angular ng-table dynamic headers doesn't work inside

回眸只為那壹抹淺笑 提交于 2019-11-30 10:23:46

Define a template for a header, and set a template-header attribute of ng-table.

You can see the code http://plnkr.co/edit/EXVkjabwfjCreNZAY1c2?p=preview

Define template header

<script id="sample_ng_header" type="text/ng-template">
  <tr>
      <th ng-repeat="column in columns" ng-show="column.visible"
          class="text-center sortable" ng-class="{
              'sort-asc': tableParams.isSortBy(column.field, 'asc'),
              'sort-desc': tableParams.isSortBy(column.field, 'desc')
            }"
          ng-click="tableParams.sorting(column.field, tableParams.isSortBy(column.field, 'asc') ? 'desc' : 'asc')">
          {{column.title}}
      </th>
      <!--<th>Columns:{{columns.length}}</th>-->
  </tr>
</script>

Set template-header attribute of ng-table with your template

<table ng-table="tableParams" template-header="sample_ng_header" show-filter="true" class="table">
  <tbody>
  <tr ng-repeat="user in $data">
      <td ng-repeat="column in columns" ng-show="column.visible" sortable="column.field">
          {{user[column.field]}}
      </td>
  </tr>
  </tbody>
</table>

There is a newer way to do this since Feb 2015. I found the following originally linked from this issue on github, but the demo written has been updated since the original.

It makes use of the new ngTableDynamic directive.

The demo is at http://ng-table.com/#/formatting/demo-dynamic-js-values.

I think the main code to pay attention to, assuming you're familiar enough with the rest of ng-table, is

<table ng-table-dynamic="tableParams with cols" show-filter="true" class="table table-bordered table-striped">
    <tr ng-repeat="user in $data">
        <td ng-repeat="col in $columns">{{user[col.field]}}</td>
    </tr>
</table>

The tableParams with cols part will be parsed, split around the with to get the table configuration and the column definitions (e.g. $scope.tableParams and $scope.cols). You can see the column definitions with code like this:

var usernameCol = {
    title: 'Full Name',
    titleAlt: 'Name',
    sortable: 'name',
    filter: { name: 'select' },
    filterData: names,
    show: true,
    field: 'name'
};
var ageCol = { ... }

$scope.cols = [usernameCol, ageCol];

Here is my solution: The above solution with template-header did not work for me. The trick is to add the columns in the header and do the same in the data cells

<table ng-table="tableParams" class="table">
        <tr>
            <th ng-repeat="col in tableCols"
                class="text-left sortable header observation-header" ng-class="{
                        'sort-asc': tableParams.isSortBy(col.field, 'asc'),
                        'sort-desc': tableParams.isSortBy(col.field, 'desc')
                      }"
                ng-click="tableParams.sorting(col.field, tableParams.isSortBy(col.field, 'desc') ? 'asc' : 'desc')">
                <div>{{col.title}}</div>
            </th>
        </tr>
        <tbody>
            <tr ng-repeat="dataObj in $data">
                <td ng-repeat="col in tableCols" data-title="col.title" sortable="col.field">{{dataObj[col.field]}}</td>
            </tr>
        ></tbody>
    </table>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!