Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I override just one table header and not all? #562

Closed
andreicristianpetcu opened this issue Mar 3, 2015 · 6 comments
Closed

Can I override just one table header and not all? #562

andreicristianpetcu opened this issue Mar 3, 2015 · 6 comments

Comments

@andreicristianpetcu
Copy link

Hi,
Can I override just one table header and not all? I want a sortable table with a custom first column (a fancy checkbox first column). Right now all the logic in the sorting is in tbody > tr. I do not have thead > th and this is how my code looks like:

<tbody>
  <tr ng-repeat="foo in fooList">
    <td data-title="'Name'" class="" data-sortable="'name'">

And now my code looks like this

<thead>
<!--custom first column header-->
<!--boilerplate header-->
  <th ng-class="{
    'sort-asc': tableParams.isSortBy('name', 'asc'),
    'sort-desc': tableParams.isSortBy('name', 'desc')
    }"
    ng-click="tableParams.sorting({'name' : tableParams.isSortBy('name', 'asc') ? 'desc' : 'asc'})">
    <div>Name</div>
  </th>
<!--the rest of the column definitions which are similar to the previous one-->
</thead>
<tbody>
  <!--custom first column data-->
  <tr ng-repeat="foo in fooList">
    <td data-title="'Name'" class="" data-sortable="'name'">

I did so because I followed this example: http://bazalt-cms.com/ng-table/example/18
Is there any way I can have a simple default sortable column (like I used to have in my first example) and have custom stuff only in the first one? I have lots of columns and the boilerplate sortable stuff complicates my code a lot and it is not DRY.

A workaround is to make my own custom directive for the header but I was hoping there is a simpler solution.

Thank you!

@Jeremy-Walton
Copy link

Yes you can.

Create a simple custom template like so:

<script id="expandAllHeader" type="text/ng-template">
  <span class="expandable" ng-click="toggleExpandAll()">+ / -</span>
</script>

Then define your special column like this:

<td header="'expandAllHeader'">...</td>

... instead of this:

<td data-title="'whatever'">...</td>

@andreicristianpetcu
Copy link
Author

Thank you @Jeremy-Walton! I did not made a full implementation yet, just a small example and it seems to work :P I will take a look more at how ng-table is using it so I can make my own example for my specific case. https://github.com/esvit/ng-table/search?utf8=%E2%9C%93&q=type%3D%22text%2Fng-template%22

@andreicristianpetcu
Copy link
Author

How can I pass a variable inside? I have something like this:

<script id="expandAllHeader" type="text/ng-template">
  <div>{{this.title}}</div>
  <div>{{this.sorting}}</div>
</script>
<td data-title="'Name'" data-sortable="'name'" header="'expandAllHeader'"></td>

@Jeremy-Walton
Copy link

Anything you put in the header will be executed in the same scope as the rest of your table, outside of the body loop. See https://github.com/esvit/ng-table/blob/master/src/ng-table/header.html#L16 and https://docs.angularjs.org/api/ng/directive/ngInclude.

Instead of using this.whatever, you should use whatever.

@andreicristianpetcu
Copy link
Author

Thank you!
For posterity :P This worked for me:

<script id="expandAllHeader" type="text/ng-template">
  <div>{{column.title()}}</div>
  <div>{{column.sortable()}}</div>
</script>
<table ng-table="tableParams">
  <tbody>
    <td data-title="'First Name'" data-sortable="'firstname'" header="'expandAllHeader'"></td>
    <td data-title="'Last Name'" data-sortable="'lastname'" header="'expandAllHeader'"></td>

@vcmiranda
Copy link

Yes you can.

Create a simple custom template like so:

<script id="expandAllHeader" type="text/ng-template">
  <span class="expandable" ng-click="toggleExpandAll()">+ / -</span>
</script>

Then define your special column like this:

<td header="'expandAllHeader'">...</td>

... instead of this:

<td data-title="'whatever'">...</td>

Wonderful. Still useful!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants