Javascript Aurelia:绑定自定义元素的文本内容?

Javascript Aurelia:绑定自定义元素的文本内容?,javascript,aurelia,Javascript,Aurelia,我正在Aurelia中编写一个非常简单的数据网格自定义元素,部分是为了我需要的功能,部分是为了学习Aurelia。进展顺利,但我一直在研究如何在自定义组件中获取元素的内容 这是我的代码,还有下面问题的剩余部分: data-grid.js import {inject, bindable} from 'aurelia-framework'; import _ from 'underscore'; export class DataGridCustomElement { @bindable d

我正在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.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>

文本内容
文本内容
文本内容
在这段代码中,我想做的是将
元素的
文本内容
绑定到
datagrid.js#DataGridColumnCustomElement
中的一个字段。与title、property和width属性类似,我需要元素的文本内容

似乎我需要在类中定义一些东西来实现这一点,但我无法确定要定义什么或在哪里查找

提前感谢,


Jason

您需要获取对列的DOM元素的引用,以便获取文本内容。以下是如何做到这一点,以及其他一些调整:

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);
  }
}

谢谢你的回答!这解决了眼前的问题,但还有一个问题,就是希望能够在文本内容中使用模板。不过,我会先讨论一下,如果我搞不懂的话,再回来。
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);
  }
}