【问题标题】:Aurelia: How do I inject a component template in existing html?Aurelia:如何在现有 html 中注入组件模板?
【发布时间】:2017-09-08 15:30:56
【问题描述】:

正如您在下图中看到的那样,它不适合我。模板 html 出现在表格上方,而不是我预期的标题下方。

我想要做的是创建一个可选的过滤器组件来处理表格数据。添加组件时,它应该在标题列下方显示一个额外的行,并带有输入/按钮,允许您在特定列上放置过滤器。

所以很明显我做错了什么,或者做我不应该做的事情。如果是这样,如何正确注入filter-table组件的模板?

我的app.html

   <table class="table table-bordered table-striped">
        <thead>
            <tr>
                <th></th>
                <th>Name</th>
                <th>City</th>
                <th>Username</th>
                <th>Email</th>
                <th>Nationality</th>
            </tr>
            <table-filter containerless></table-filter>
        </thead>
        <tbody>
            <tr>
                <td>??</td>
                <td>Name</td>
                <td>City</td>
                <td>Username</td>
                <td>Email</td>
                <td>Nationality</td>
            </tr>
        </tbody>
    <table>

我的table-filter 组件。

// Viewmodel
@customElement('table-filter')
export class TableFilterComponent {
    // Nothing here    
}

// Template
<template>
    <tr>
        <th>Filter header 1</th>
        <th>Filter header 2</th>
        <th>Filter header 3</th>
        <th>Filter header 4</th>
        <th>Filter header 5</th>
        <th>Filter header 6</th>
    </tr>
</template>

【问题讨论】:

  • 这真的很奇怪。你试图做的事情对我来说很有意义。我重现了这个问题。我本来希望将&lt;tr&gt; 直接注入标题行下方。有兴趣了解建议的解决方案

标签: aurelia aurelia-templating


【解决方案1】:

我敢打赌这是因为&lt;table-filter /&gt; 不是&lt;thead /&gt; 元素内的有效元素。

试试这个,看看它是否有效:

<thead>
    <tr>
        <th></th>
        <th>Name</th>
        <th>City</th>
        <th>Username</th>
        <th>Email</th>
        <th>Nationality</th>
    </tr>
    <tr as-element="table-filter" containerless></table-filter>
</thead>

然后只需删除自定义元素模板中的 &lt;tr /&gt; 元素。

<template>
   <th>Filter header 1</th>
   <th>Filter header 2</th>
   <th>Filter header 3</th>
   <th>Filter header 4</th>
   <th>Filter header 5</th>
   <th>Filter header 6</th>
</template>

最后,在这个 Github issue 中有一些很好的提示:https://github.com/aurelia/binding/issues/409

【讨论】:

  • 很好,很好的建议:D。 as-element 是一个好主意(尽管它生成的行没有包装 tr 元素并且只是直接注入 th 项目)。我很好奇为什么使用添加containerless 并不能避免这个问题。我原以为它会直接注入tr
  • 为此添加了一个 Gistrun gist.run/…,尽管删除 containerless 并使用 as-element 会得到所需的结果:D
  • 是的,取出containerless,浏览器会忽略它。
  • 太棒了。这就像一个魅力。谢谢阿什利!如果日期没有改变,会在阿姆斯特丹 Aurelia 活动中见到你;)
猜你喜欢
  • 2019-04-15
  • 1970-01-01
  • 2015-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多