3
votes
{% raw %}

<table class="table table-striped table-bordered row" id="compare-table" style = "table-layout: fixed; margin-right: auto; margin-left: auto">
    <thead>
        <th class = "col-md-3 tableHeading">Configuration name</th>
        <th class = "col-md-3 tableHeading">Property Name</th>
        <th class = "col-md-3 tableHeading">Value 1</th>
        <th class = "col-md-3 tableHeading">Value 2 </th>
    </thead>
    <tbody>
    {{#each tableRows }}
        {{#each values}}
            <tr>
                {{#if @first}}
                <th  class="breakWord inlineHeading" rowspan={{../length}}>{{ ../pid }}</th>
                {{/if}}

                <td class="breakWord">{{ propName }}</td>
                <td class="breakWord">{{ propValueA }}</td>
                <td class="breakWord">{{ propValueB }}</td>
            </tr>
        {{/each}}
    {{/each}}
    </tbody>
</table>
{% endraw %}

I have to render a table dynamically after ajax request and want to group rows by cofiguration-name. When I use rowspan in Data Table only simple table is rendered and there is a console error:

jquery.dataTables.min.js:24 Uncaught TypeError: Cannot set property '_DT_CellIndex' of undefined

I am using handlebars.js for populating Table-Template

DATATABLE: https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"> https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap.min.js">

Is there a way to generate table in this format while keeping Datatable features.

enter image description here

1
Where is the javascript in this? - SamHoque
<th rowspan="2">Name</th> Try - Sumesh TG
@SumeshTG i am giving rowspan dynamically through handlebars but it is throwing console error and a simple html table is rendered without pagination, search and sorting features - shubham dua
@SamzSakerz I have directly used cdn links to import Datatable and want to know the solution for using rowspan for which js will be used. I am new to js and wanted help for this issue. Please suggest if any edits should be made to the post. Thanks - shubham dua

1 Answers

1
votes

$(document).ready(function() {
   var table = $('#example').DataTable({
      'ajax': 'https://api.myjson.com/bins/qgcu',
      'rowsGroup': [2]
   });   
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://cdn.datatables.net/v/dt/dt-1.10.12/datatables.min.js"></script>
<script src="https://cdn.rawgit.com/ashl1/datatables-rowsgroup/fbd569b8768155c7a9a62568e66a64115887d7d0/dataTables.rowsGroup.js"></script>
<link href="https://cdn.datatables.net/v/dt/dt-1.10.12/datatables.min.css" rel="stylesheet"/>
<h3>jQuery DataTables - ROWSPAN in table body TBODY</h3>

<hr><br>
    
<table id="example" class="display" cellspacing="0" width="100%">
   <thead>
      <tr>
         <th>Name</th>
         <th>Position</th>
         <th>Office</th>
         <th>Extn.</th>
         <th>Start date</th>
         <th>Salary</th>
      </tr>
   </thead>
   <tfoot>
      <tr>
         <th>Name</th>
         <th>Position</th>
         <th>Office</th>
         <th>Extn.</th>
         <th>Start date</th>
         <th>Salary</th>
      </tr>
   </tfoot>
</table>

Use https://cdn.rawgit.com/ashl1/datatables-rowsgroup/fbd569b8768155c7a9a62568e66a64115887d7d0/dataTables.rowsGroup.js external library and apply 'rowsGroup': [index_of_column] in datatable config. I hope this helps you.