Search code examples
javascriptjquerytabulator

tabulator - how to extract just the data and save as json object


is there a way to save all data inside a tabulator table into a variable in jquery/javascript? I noticed that they have a

table.downloadToTab("json");

method which is cool but instead of opening up into new browser, I just want to save to a variable.

I know I can write my own javascript / jquery code to loop through the rows and cells but it looks like they don't use a traditional table with <tr> and <td> but everything is a with specific class names.

For example this is all the HTML that's created for just one row of data in a tabulator table:

<div class="tabulator-row tabulator-selectable tabulator-row-odd" role="row" style="padding-left: 30px;">
   <div class="tabulator-cell tabulator-row-handle tabulator-frozen tabulator-frozen-left" role="gridcell" title="" style="width: 30px; position: absolute; left: 0px; height: 29px;">
      <div class="tabulator-row-handle-box">
         <div class="tabulator-row-handle-bar"></div>
         <div class="tabulator-row-handle-bar"></div>
         <div class="tabulator-row-handle-bar"></div>
      </div>
      <div class="tabulator-col-resize-handle"></div>
      <div class="tabulator-col-resize-handle prev"></div>
   </div>
   <div class="tabulator-cell" role="gridcell" tabulator-field="id" title="" style="width: 150px; height: 29px;">
      2134561
      <div class="tabulator-col-resize-handle"></div>
      <div class="tabulator-col-resize-handle prev"></div>
   </div>
   <div class="tabulator-cell" role="gridcell" tabulator-field="first_name" tabindex="0" title="" style="width: 150px; height: 29px;">
      jane
      <div class="tabulator-col-resize-handle"></div>
      <div class="tabulator-col-resize-handle prev"></div>
   </div>
   <div class="tabulator-cell" role="gridcell" tabulator-field="last_name" tabindex="0" title="" style="width: 150px; height: 29px;">
      doe
      <div class="tabulator-col-resize-handle"></div>
      <div class="tabulator-col-resize-handle prev"></div>
   </div>
   <div class="tabulator-cell" role="gridcell" tabulator-field="company" tabindex="0" title="" style="width: 150px; height: 29px;">
      <div class="tabulator-col-resize-handle"></div>
      <div class="tabulator-col-resize-handle prev"></div>
   </div>
   <div class="tabulator-cell" role="gridcell" tabulator-field="email" tabindex="0" title="" style="width: 150px; height: 29px;">
      [email protected]
      <div class="tabulator-col-resize-handle"></div>
      <div class="tabulator-col-resize-handle prev"></div>
   </div>
   <div class="tabulator-cell" role="gridcell" tabulator-field="title" tabindex="0" title="" style="width: 150px; height: 29px;">
      &nbsp;
      <div class="tabulator-col-resize-handle"></div>
      <div class="tabulator-col-resize-handle prev"></div>
   </div>
   <div class="tabulator-cell" role="gridcell" tabulator-field="registered" title="" style="width: 150px; height: 29px;">
      &nbsp;
      <div class="tabulator-col-resize-handle"></div>
      <div class="tabulator-col-resize-handle prev"></div>
   </div>
   <div class="tabulator-cell" role="gridcell" tabulator-field="speak" aria-checked="false" title="" style="width: 167px; text-align: center; height: 29px;">
      <svg enable-background="new 0 0 24 24" height="14" width="14" viewBox="0 0 24 24" xml:space="preserve">
         <path fill="#CE1515" d="M22.245,4.015c0.313,0.313,0.313,0.826,0,1.139l-6.276,6.27c-0.313,0.312-0.313,0.826,0,1.14l6.273,6.272  c0.313,0.313,0.313,0.826,0,1.14l-2.285,2.277c-0.314,0.312-0.828,0.312-1.142,0l-6.271-6.271c-0.313-0.313-0.828-0.313-1.141,0  l-6.276,6.267c-0.313,0.313-0.828,0.313-1.141,0l-2.282-2.28c-0.313-0.313-0.313-0.826,0-1.14l6.278-6.269  c0.313-0.312,0.313-0.826,0-1.14L1.709,5.147c-0.314-0.313-0.314-0.827,0-1.14l2.284-2.278C4.308,1.417,4.821,1.417,5.135,1.73  L11.405,8c0.314,0.314,0.828,0.314,1.141,0.001l6.276-6.267c0.312-0.312,0.826-0.312,1.141,0L22.245,4.015z"></path>
      </svg>
      <div class="tabulator-col-resize-handle"></div>
      <div class="tabulator-col-resize-handle prev"></div>
   </div>
   <div class="tabulator-cell" role="gridcell" tabulator-field="paid" aria-checked="false" title="" style="width: 167px; text-align: center; height: 29px;">
      <svg enable-background="new 0 0 24 24" height="14" width="14" viewBox="0 0 24 24" xml:space="preserve">
         <path fill="#CE1515" d="M22.245,4.015c0.313,0.313,0.313,0.826,0,1.139l-6.276,6.27c-0.313,0.312-0.313,0.826,0,1.14l6.273,6.272  c0.313,0.313,0.313,0.826,0,1.14l-2.285,2.277c-0.314,0.312-0.828,0.312-1.142,0l-6.271-6.271c-0.313-0.313-0.828-0.313-1.141,0  l-6.276,6.267c-0.313,0.313-0.828,0.313-1.141,0l-2.282-2.28c-0.313-0.313-0.313-0.826,0-1.14l6.278-6.269  c0.313-0.312,0.313-0.826,0-1.14L1.709,5.147c-0.314-0.313-0.314-0.827,0-1.14l2.284-2.278C4.308,1.417,4.821,1.417,5.135,1.73  L11.405,8c0.314,0.314,0.828,0.314,1.141,0.001l6.276-6.267c0.312-0.312,0.826-0.312,1.141,0L22.245,4.015z"></path>
      </svg>
      <div class="tabulator-col-resize-handle"></div>
      <div class="tabulator-col-resize-handle prev"></div>
   </div>
   <div class="tabulator-cell" role="gridcell" tabulator-field="example" title="" style="width: 167px; height: 29px;">
      <a href="mailto://email" target="_blank">[email protected]</a>
      <div class="tabulator-col-resize-handle"></div>
      <div class="tabulator-col-resize-handle prev"></div>
   </div>
</div>

A lot of html! but I can write the code... i just want to know if there's a better way.


Solution

  • You can use Tabulator's getData() method:

    var array = table.getData();
    

    This will return all the table's data as a 2D array. If you want to only get the data that is currently filtered (visible to the user), then pass true as argument:

    var array = table.getData(true);
    

    And you can use the native JSON.stringify() function to produce JSON in case you need to transfer/persist this data somehow:

    var json = JSON.stringify(array);