带空格键的手风琴Bootstrap表

时间:2019-03-13 12:50:13

标签: mongodb meteor bootstrap-4 spacebars

我看到了与手风琴有关的问题,但并不完全符合我的特殊需求。我的表格是使用空格键填充的,更具体的说是每个循环嵌套如下:

<tbody>
{{#each piece in pieces}}
    <tr id="{{piece._id}}" class="itemList table-warning">
        <th class="name tText">{{piece.name}} {{piece._id}}</th>
        <td class="pdf tText" style="text-align: center"><a class ="pdf" href="{{piece.pdf}}" target="_blank"><i class="fa fa-file-text-o" aria-hidden="true"></i></a></td>
        <td class="audio tText" style="text-align: center"><a class="audio" href="{{piece.audio}}" target="_blank"><i class="fa fa-volume-up" aria-hidden="true"></i></a></td>
        <td class="format tText">{{piece.instrumentation}}</td>
        <th class="price tText" >${{piece.price}}</th>
        <td><input class ="qty" type ="number" name ="quantity" value="0" min="0"></td>
    </tr>
<!-- Row that is being clicked-->
    <tr class="partsList">
        <td colspan="3"></td>
        <th class="partName tText">{{piece.name}} Parts</th>
        <td colspan="2"></td>
    </tr>

    {{#each part in piece.parts}}
<!-- Rows that should accordion -->
<!-- Currently ALL rows accordion on click. Need to accordion based on _id-->
        <tr class="partList">
            <td colspan="3"></td>
            <td class="pname tText">{{piece.name}}: {{part.pname}}</td>
            <td class="price tText">${{part.pprice}}</td>
            <td><input class="qty" type="number" name="quantity" value="0" min="0"></td>
        </tr>
    {{/each}}
{{/each}}
</tbody>

我具有如下点击功能:

'click .partsList': function(e){
    e.preventDefault();
    $('.partList').nextUntil('tr.itemList').toggle();
}

手风琴功能有效,但是它适用于每个循环的每个实例。也就是说,每个tr class ="partsList"都会在点击时同时显示手风琴。

据我所知,可以使用{{piece._id}}访问文档的_id。如果我将表的行ID设置为等于该行的ID,则它只会读取集合中FIRST文档的_id

我需要单击<tr class="partList">以使基于_id的手风琴出现。也许您会采用与引导表不同的方式进行处理?

请让我知道我的问题是否需要澄清。

1 个答案:

答案 0 :(得分:1)

您可以使用.partslist属性过滤单击的data-*。这导致jQuery仅选择此特定项目。请注意,您需要将data-*属性附加到单击的行和应折叠的行:

<tbody>
{{#each piece in pieces}}
    ...
    <!-- Row that is being clicked-->
    <!-- use the _id value of the piece context as data attribute -->
    <tr class="partsList" data-id="{{piece._id}}"> 
        <td colspan="3"></td>
        <th class="partName tText">{{piece.name}} Parts</th>
        <td colspan="2"></td>
    </tr>

    {{#each part in piece.parts}}
    <!-- Rows that should accordion -->
    <!-- Currently ALL rows accordion on click. Need to accordion based on _id-->
    <!-- use the _id value of the piece context as data attribute -->
    <tr class="partList" data-target="{{piece._id}}">
    ...
    </tr>
    {{/each}}
{{/each}}
</tbody>
  'click .partsList': function(e, templateInstance){
    e.preventDefault();
    // get the data-id attribute of the clicked row
    const targetId = templateInstance.$(e.currentTarget).data('id')
    // skip if this row is not intended to toggle
    if (!targetId) return
    // toggle based on data-target attribute
    templateInstance.$(`.partList[data-target="${targetId}"]`).nextUntil('tr.itemList').toggle();
  }