Defining columns with data-data ?

Defining columns with data-data ?

fischerfischer Posts: 10Questions: 6Answers: 0

Hi.

We have a part in our backend that is very prone to change, so I thought, I might as well sever-side render the html and have Datatables read the column definitions from data-* attributes. Especially, I added data-data attributes to each column, conforming to the structure of the requested json.

This is the resulting html.

<table class="table table-centered table-hover mb-0 dataTable no-footer" id="model-table">
     <thead>
         <tr class="header-row" role="row">
             <th tabindex="0" class="sorting align-middle" rowspan="1" colspan="1" data-data="owner">Auswahl</th>
             <th tabindex="0" data-data="model.modelName" class="sorting align-middle" rowspan="1" colspan="1">Modell</th>
             <th tabindex="0" data-data="model.url" class="sorting align-middle" rowspan="1" colspan="1">URL</th>
             <th tabindex="0" class="sorting align-middle" rowspan="1" colspan="1" data-data="model.COUNT">Anzahl</th>
             <th tabindex="0" class="sorting align-middle" rowspan="1" colspan="1" data-data="model.NUM_PRICE">Preis</th>
             <th tabindex="0" class="sorting align-middle" rowspan="1" colspan="1" data-data="model.TXT_PRICE">Preistext</th>
             <th tabindex="0" class="sorting align-middle" rowspan="1" colspan="1" data-data="model.DISCOUNT">Rabatt</th>
             <th tabindex="0" class="sorting align-middle" rowspan="1" colspan="1" data-data="model.NUM_FINANCING">Finanzierung</th>
             <th tabindex="0" class="sorting align-middle" rowspan="1" colspan="1" data-data="model.NUM_LEASING">Leasingrate</th>
          </tr>
      </thead>
      <tbody>
      </tbody>
</table>

This is the datatables configuration object.

   {
       ajax : {
            url : '/someRoute',
            data : function( request ){
                request.config = $('#some-form :input:not([type=hidden])');
                request.selection = $('#selection').val();
            }
         }
  }

This results in

TypeError: **aoColumnsInit is undefined** jquery.dataTables.js:1150:15
jQuery 5
addDataTable http://localhost:8080/assets/js/datatable.js:7
modelTable http://localhost:8080/assets/js/projects/adparam.js:20
<anonym> http://localhost:8080/assets/js/projects/adparam.js:15
jQuery 2

Is this approach possible in general? Am I doing anything wrong?

This question has an accepted answers - jump to answer

Answers

This discussion has been closed.