Accordion Bootstrap table with spacebars

98 Views Asked by At

I have seen questions pertaining to accordion but not entirely to my specific need. My table is populated using spacebars, more specific a nested each loop like this:

<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>

I have a click function like so:

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

The accordion function works, however it works with every instance of the each loop. i.e. every tr class ="partsList" will accordion at the same time on click.

To my understanding of the each loop, I can access the _id of a document using {{piece._id}}. If I set the table row id to equal that however, it only reads the _id of the FIRST document in the collection.

What I need is on click for the <tr class="partList"> to accordion based on _id. Or perhaps you would go about this a different way than bootstrap tables?

Please let me know if my question needs clarification.

1

There are 1 best solutions below

7
Jankapunkt On

You could filter the clicked .partslist using a data-* attribute. This causes jQuery to select only this specific items. Note that you need to attach the data-* attribute to the row that is clicked and to the rows that should collapse:

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