Datatables merging Columns while exporting to Excel

Datatables merging Columns while exporting to Excel

Atif RAzzaqAtif RAzzaq Posts: 1Questions: 1Answers: 0
edited September 2023 in Free community support

In Image on last column have td inside the td the problem is that when I am exporting it to excel via data tables then last two columns are not exporting and other columns are showing perfectly, how can I merge these columns via data table to excels.
Table structure as follows

<table class="table table-bordered " id="example">
                        <thead>
                                <tr>
                                        <th>Branch ID</th>
                                        <th>Branch Name</th>
                                        <th>Date</th>
                                        <th>S.time</th>
                                        <th >E.time</th>
                                        <th>Lines</th>
                                        <th>
                                            <table>
                                                   <tr><th>Item</th></tr>
                                            
                                            </table>
                                        </th> <th>
                                            <table>
                                                    <tr><th>time</th></tr>
                                            </table>
                                        </th>

                                </tr>
                            </thead>       
                                <tbody>
                                                                                        <tr>
                                                   <td>1</td>
                                                   <td>HEAD OFFICE</td> 
                                                   <td id="tdtdate1"><input type="date" name="tdate" id="tdate1" class="tdate" value="2023-09-08"></td> 
                                                   <td class="td1"><input type="time" id="stime1" ></td>
                                                   <td><input type="time" id="etime1"></td>
                                                    <td><input type="number" name="number" id="number1" class="number" autocomplete="off" ></td>
                                                   <td >
                                                            <table><tr> <td> <input type="text" class="form-control item" id="item1"> </td></tr><tr> <td> <input type="text" class="form-control item" id="item1"> </td></tr><tr> <td> <input type="text" class="form-control item" id="item1"> </td></tr> </table></td>                               
                                                   <td ><table> <tr><td> <input type="text" class="form-control time" id="time10"> </td></tr> <tr><td> <input type="text" class="form-control time" id="time11"> </td></tr> <tr><td> <input type="text" class="form-control time" id="time12"> </td></tr> </table></td>                                                  
                                               </tr>        
                                                                                            <tr>
                                                   <td>2</td>
                                                   <td>ONLINE STORE</td> 
                                                   <td id="tdtdate1"><input type="date" name="tdate" id="tdate1" class="tdate" value="2023-09-08"></td> 
                                                   <td class="td1"><input type="time" id="stime1" ></td>
                                                   <td><input type="time" id="etime1"></td>
                                                    <td><input type="number" name="number" id="number1" class="number" autocomplete="off" ></td>
                                                   <td >
                                                            <table><tr> <td> <input type="text" class="form-control item" id="item1"> </td></tr><tr> <td> <input type="text" class="form-control item" id="item1"> </td></tr><tr> <td> <input type="text" class="form-control item" id="item1"> </td></tr> </table></td>                               
                                                   <td ><table> <tr><td> <input type="text" class="form-control time" id="time10"> </td></tr> <tr><td> <input type="text" class="form-control time" id="time11"> </td></tr> <tr><td> <input type="text" class="form-control time" id="time12"> </td></tr> </table></td>                                                  
                                               </tr>                                      
                                               </tr>        
                                                      
                                                
                                </tbody>

                     
                    </table>

Data Tables jquery as bellow

$(document).ready(function() {
    $('#example').DataTable( {
        info: false,
        ordering: false,
        paging: false
        dom: 'Bfrtip',
        buttons: [
        {
          extend: 'excel',
          exportOptions: {

            format: {
              body: function ( inner, rowidx, colidx, node ) {
                if ($(node).children("input").length > 0) {
                  return $(node).children("input").first().val();
                } else {
                  return inner;
                }
              }
            }
          }
        }
      ]
    } );
} );

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • allanallan Posts: 63,602Questions: 1Answers: 10,486 Site admin

    You are using $(node).children("input").first() so you are only getting the first input element's value. Do you want to get all of the input element's values and join them space separated or something? If so, Use $().map() to loop over the elements, return an array of values and then .join(' ') them.

    Allan

Sign In or Register to comment.