[英]Aurelia: Binding textContent of a custom element?
我正在Aurelia寫一個非常簡單的數據網格自定義元素,部分用於我需要的功能,部分用於學習Aurelia。 它進展順利,但我仍然堅持如何獲取自定義組件中元素的內容。
這是我的代碼,問題的其余部分如下:
數據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);
}
}
數據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>
數據並網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'
},
];
}
}
數據網的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
的字段。 與標題,屬性和寬度屬性類似,都可以正常工作,我想要元素的文本內容。
似乎我需要在類上定義一些事情來實現這一點,但我無法弄清楚要定義什么或在哪里看。
提前致謝,
賈森
您需要獲取對列的DOM元素的引用,以便您可以獲取textContent。 以下是如何做到這一點,以及其他一些調整:
import {processContent, noView} from 'aurelia-framework';
@noView() // this element doesn't need a view, it's essentially declarative data for the parent element
@processContent(false) // the templating engine doesn't need to process the content of this element, we're going to do it
@inject(DataGridCustomElement, Element) // inject the parent, and the DOM element that represents this DataGridColumn element so we can read it's textContent
export class DataGridColumnCustomElement {
@bindable title = '';
@bindable width = '';
@bindable property = '';
textContent = '';
constructor(dataGrid, element) {
this.textContent = element.textContent; // grab the textContent and store it in a class property
dataGrid.columns.push(this);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.