【发布时间】:2016-01-27 02:23:24
【问题描述】:
我正在 Aurelia 中编写一个非常简单的数据网格自定义元素,部分用于我需要的功能,部分用于学习 Aurelia。进展顺利,但我一直坚持如何在自定义组件中获取元素的内容。
这是我的代码,剩下的问题如下:
data-grid.js
import {inject, bindable} from 'aurelia-framework';
import _ from 'underscore';
export class DataGridCustomElement {
@bindable data = [];
@bindable height = '';
columns = [];
bind() {
this.sort(this.columns[0].property);
}
sort(property) {
if (property == this.sortProperty) {
this.sortDirection = !this.sortDirection;
}
else {
this.sortProperty = property;
this.sortDirection = true;
}
let data = _.sortBy(this.data, this.sortProperty);
if (!this.sortDirection) {
data = data.reverse()
}
this.data = data;
}
}
@inject(DataGridCustomElement)
export class DataGridColumnCustomElement {
@bindable title = '';
@bindable width = '';
@bindable property = '';
constructor(dataGrid) {
dataGrid.columns.push(this);
}
}
data-grid.html
<template>
<table class="table table-fixedheader table-condensed table-striped table-bordered">
<thead>
<tr>
<th repeat.for="column of columns" width="${column.width}"><a href="#" click.delegate="sort(column.property)">${column.title}</a></th>
</tr>
</thead>
<tbody css="height: ${height};">
<tr repeat.for="row of data">
<td repeat.for="column of columns" width="${column.width}"></td>
</tr>
</tbody>
</table>
</template>
data-grid-test.js
import {inject} from 'aurelia-framework';
export class DataGridTest {
constructor() {
this.animals = [
{
id : 3,
animal : 'Horse',
home : 'Stall'
},
{
id : 1,
animal : 'Monkey',
home : 'Tree'
},
{
id : 11,
animal : 'Dog',
home : 'House'
},
{
id : 2,
animal : 'Cat',
home : 'Internet'
},
{
id : 20,
animal : 'Hamster',
home : 'Cage'
},
];
}
}
data-grid-test.html
<template>
<require from="./data-grid"></require>
<data-grid data.bind="animals" height="300px">
<data-grid-column title="ID" property="id" width="34%">TEXT CONTENT</data-grid-column>
<data-grid-column title="Animal" property="animal" width="33%">TEXT CONTENT</data-grid-column>
<data-grid-column title="Home" property="home" width="33%">TEXT CONTENT</data-grid-column>
</data-grid>
</template>
在这段代码中,我想做的是将<data-grid-column> 元素的TEXT CONTENT 绑定到data-grid.js#DataGridColumnCustomElement 中的一个字段。与所有工作正常的标题、属性和宽度属性类似,我想要元素的文本内容。
似乎我需要在类上定义一些东西才能发生这种情况,但我不知道要定义什么或在哪里查看。
提前致谢,
杰森
【问题讨论】:
标签: javascript aurelia