How to Export Checked Row (I m using checkbox to select rows)?

How to Export Checked Row (I m using checkbox to select rows)?

Sanit KaleSanit Kale Posts: 23Questions: 9Answers: 0
edited March 2018 in Free community support

1.Server side code

   while( $row=mysqli_fetch_array($queryRecords) ) 
        {  
                     $nestedData=array();
                      $nestedData[] = "<input type='checkbox' onclick='checkboxfn($(this));' id='singleBox'  value='". $index."'  />" ;
                      $nestedData[]  = "<i class='mdi mdi-format-list-bulleted'></i>";
                      $nestedData[]  = $index;
                      $nestedData[] = $row["Designation"];
                      $nestedData[] = $row["CompanyName"];
                      $nestedData[] = $row["PersonalEmail"];
                      $nestedData[] = $row["PersonalPhone"];
                      $nestedData[] = $row["PersonalMobile"];
                     $data[] = $nestedData;
                  $index++;
         }  

$json_data = array(
                          "draw"                => 1,   
                          "recordsTotal"    => intval( $totalRecords ),  
                            "recordsFiltered" => intval($totalRecords),
                           "data"            => $data   // total data array
                        );
   echo json_encode($json_data);  // send data as json format
  1. My Button Code
       dom: 'lBfrtip',
       buttons: [
            {
                extend: 'copyHtml5',
                
                exportOptions: {
                    columns: [6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21 ]
                }
            },
            {
                extend: 'excelHtml5',
                text: 'XLS',
                exportOptions: {
                    columns: [6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
                }
            },
            {
            extend:  'csvHtml5',
            exportOptions: 
            {
                columns: [6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
            }
            },
            {
                extend: 'pdfHtml5',
                orientation: 'landscape',//landscape give you more space
            pageSize: 'A0',//A0 is the largest A5 smallest(A0,A1,A2,A3,legal,A4,A5,letter))
                exportOptions: {
                    columns: [7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
                }
            },
            
       ], 
                 columnDefs: [{
                "targets": 5,
                "orderable": true
            }]

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi Sanit,

    This example may be useful - it explains about exporting selected rows.

    Hope that helps,

    Cheers,

    Colin

  • Sanit KaleSanit Kale Posts: 23Questions: 9Answers: 0

    Thank You, Sir, For Your Response Its Very kind of you.
    But My Problem Is I am Trying to Select Rows Using Custome Checkboxes and I am Pushing respected Row's ID to Export

  • Sanit KaleSanit Kale Posts: 23Questions: 9Answers: 0
    edited March 2018

    I am getting " Uncaught TypeError: Cannot read property '0' of null" this Error
    Actually, My Excel Export Is working Fine but CSV and Copy Is not working

    1. This is my Excel Export Code.

                  for (var f = 0, h = b.body.length; f < h; f++)
      
                if(rowIds.length != 0)
                   {
                  var index = rowIds.indexOf(b.body[f][0]);       
                  console.log("Excel "+b.body[f][0]);
                          if (index < 0) 
                          {
                              //alert("dont export");
                          }
                          else
                          {
                              b.body[f].splice(0, 1);
                              a += e(b.body[f]);
                          }   
              }
              else
              {
                  b.body[f].splice(0, 1);
                  a += e(b.body[f]);
              }
      
                    }
      
    2. This Is My Copy and CSV Code

                            for(i=0;i<l.length;i++)
                  {
      
                      if(rowIds.length != 0)
                      {
                               // I AM GETTING ERROR ON BELOW LINE
                      var index = rowIds.indexOf(l[i].match(/\d+/)[0]);
                          console.log("Copy and CSV "+l[i].match(/\d+/)[0]);
                          console.log("Index "+index);
      
                          if (index < 0) 
                          {
                              //alert("dont export");
                          }
                          else
                          {
      
                              l[i]=l[i].substr(l[i].indexOf(b.fieldSeparator) + 1);
                              temp.push(l[i]);
                          }
      
                      }
                      else
                      {
                          //removing the row id from the contact before export.
                          l[i]=l[i].substr(l[i].indexOf(b.fieldSeparator) + 1);
                          temp.push(l[i]);
                      }
      
                  }
      
    3. my rowID Pushing Code

                     function checkboxfn(t)  
              {   
            if (t.is(':checked')) 
                   {
                      var index = rowIds.indexOf(t[0]['value']);  
                          if (index < 0) 
                          {
                              rowIds.push(t[0]['value']);
                          }   
      
                   }else
                   {  
                   //when checkbox is unchecked remove id from rowIds
                        var index = rowIds.indexOf(t[0]['value']);
                          if (index > -1) {
                              rowIds.splice(index, 1);
                          }            
                  }
      
          alert(rowIds);
      
      
      
              }
      
  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    As you pointed out that this line is giving an error

    rowIds.indexOf(l[i].match(/\d+/)[0])

    I would suggest debugging around l[i].match(/\d+/) - I suspect that's returning null, which you're then accessing with [0]

  • Sanit KaleSanit Kale Posts: 23Questions: 9Answers: 0

    Thanks, Sir, Now Its working Perfectly Fine

This discussion has been closed.