| 1 |
{{! |
| 2 |
https://github.com/miguelcobain/ember-yeti-table/raw/a6642a4f16b6786a0f72d1caaa3270d0cb194d05/ember-yeti-table/src/components/yeti-table/body.gjs |
| 3 |
}} |
| 4 |
import { action } from '@ember/object'; |
| 5 |
|
| 6 |
import Component from '@glimmer/component'; |
| 7 |
|
| 8 |
/** |
| 9 |
Renders a `<tbody>` element and yields the row component, row data and index. |
| 10 |
```hbs |
| 11 |
<table.body as |body person index|> |
| 12 |
<body.row as |row|> |
| 13 |
<row.cell> |
| 14 |
{{person.firstName}} #{{index}} |
| 15 |
</row.cell> |
| 16 |
<row.cell> |
| 17 |
{{person.lastName}} |
| 18 |
</row.cell> |
| 19 |
<row.cell> |
| 20 |
{{person.points}} |
| 21 |
</row.cell> |
| 22 |
</body.row> |
| 23 |
</table.body> |
| 24 |
``` |
| 25 |
It can also be used as a blockless component to let yeti table automatically |
| 26 |
unroll thee rows for you, based on the `@prop` arguments you passed in to the |
| 27 |
column definition components. |
| 28 |
```hbs |
| 29 |
<table.body/> |
| 30 |
``` |
| 31 |
Remember that this component's block will be rendered once per each item in the `@data` array. |
| 32 |
|
| 33 |
@class Body |
| 34 |
@yield {object} body |
| 35 |
@yield {Component} body.row - the row component |
| 36 |
@yield {Object} rowData - one item in the data array |
| 37 |
@yield {number} index |
| 38 |
*/ |
| 39 |
|
| 40 |
// template imports |
| 41 |
import { fn, get, hash } from '@ember/helper'; |
| 42 |
import TBodyRow from './tbody/row.gjs'; |
| 43 |
|
| 44 |
export default class Body extends Component { |
| 45 |
<template> |
| 46 |
<tbody class={{@theme.tbody}} ...attributes> |
| 47 |
{{#if (has-block)}} |
| 48 |
|
| 49 |
{{#each @data as |rowData index|}} |
| 50 |
{{yield (hash row=(component TBodyRow theme=@theme onClick=@onRowClick columns=@columns)) rowData index}} |
| 51 |
{{/each}} |
| 52 |
|
| 53 |
{{else}} |
| 54 |
|
| 55 |
{{#each @data as |rowData|}} |
| 56 |
<TBodyRow |
| 57 |
@theme={{@theme}} |
| 58 |
@onClick={{if @onRowClick (fn this.handleRowClick rowData)}} |
| 59 |
@columns={{@columns}} |
| 60 |
as |row| |
| 61 |
> |
| 62 |
|
| 63 |
{{#each @columns as |column|}} |
| 64 |
<row.cell @class={{column.columnClass}}> |
| 65 |
{{#if column.prop}} |
| 66 |
{{get rowData column.prop}} |
| 67 |
{{else}} |
| 68 |
{{rowData}} |
| 69 |
{{/if}} |
| 70 |
</row.cell> |
| 71 |
{{/each}} |
| 72 |
</TBodyRow> |
| 73 |
{{/each}} |
| 74 |
{{/if}} |
| 75 |
</tbody> |
| 76 |
</template> |
| 77 |
/** |
| 78 |
* Adds a click action to each row, called with the clicked row's data as an argument. |
| 79 |
* Can be used with both the blockless and block invocations. |
| 80 |
* |
| 81 |
* @argument onRowClick |
| 82 |
* @type Function |
| 83 |
*/ |
| 84 |
|
| 85 |
@action |
| 86 |
handleRowClick(rowData) { |
| 87 |
this.args.onRowClick?.(rowData); |
| 88 |
} |
| 89 |
} |