Data Tables Custom View then export to csv - problem

Data Tables Custom View then export to csv - problem

yooeltonyooelton Posts: 4Questions: 0Answers: 0

Hello and thanks for interesting in my problem.

I've got implemented database with datatables for equipment in company.

The problem is when I export my data to csv. Always every row is included into the csv file(I open it with excel), even when i custom view only a few of columns.

$( "#custom_print_view" ).dialog({
                   autoOpen: true,
                   modal: true,
                   height:400,
                   width:350,
                    buttons: {
                    "Show": function() {

                            $("#empty").dialog("close");
                            toShow.elements.push("inTime");
                            toShow.data = $("#custom_print_date").val();
                        if(toShow.elements.length >= 1){
                            $.post( baseUrl+"CustomPrint/download",{toShow: JSON.stringify(toShow)}).done(function(data){
                            data = JSON.parse(data);
                            columns = elementsToTable(toShow.elements);
                            update(data,columns);
                            //data = JSON.parse(data);

                            $('#exportLink').css('display', 'inline-block'); //show button when ready 
                            var filename = 'hardware.csv'; //export .csv file           
                            var blobby = new Blob([makeCSV(data)], {type: 'text/csv;charset=utf-8'}); //make blob object

                                    $(exportLink).attr({
                                                'download' : filename,
                                                'href': window.URL.createObjectURL(blobby),
                                                'target': '_blank'
                                                });

                            })
                    }
                    else{
                            $("#empty").dialog("open");

                        }

                            $(this).dialog("close");
                        }
                    }
            });

        function update(data,columns){
                    var result='';
                    table = $('#Date').DataTable({

                    "pagingType": "simple",
                    "lengthMenu": [[50, 100, 150,200, -1], [50, 100, 150,200, "All"]],
                    "data": data,
                    "destroy": true,
                    "order": [[ 1, "asc" ]],
                    "scrollY": ($(window).innerHeight()-300)+"px",
                    "scrollCollapse": true,
                    "paging":         true,
                      "columns": columns,
                      "columnDefs": [ {


                            "targets": 0,

                            "render": function ( data, type, full, meta ) {
                                if(data!=null)
                                    {
                                       return data.replace('"',"'");
                                    }
                                else{
                                    return data;
                                    }

                            }
                          } ]

                    })  

        }

Where is the problem? I found on examples that it can work, and export only things that i searched through the search box or only the custom view with defined columns.

Please help me or ask me to copy other lines of code.

Cheers

Replies

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin

    I don't actually see where you are defining the CSV button for the Buttons extension. Is that is separate code?

    Allan

  • yooeltonyooelton Posts: 4Questions: 0Answers: 0
    function makeCSV(arrObj) {
            var jsonArray = typeof arrObj != 'object' ? JSON.parse(arrObj) : arrObj;
            var fullStr = '';
            var partialStr='';
            fullStr='\ufeff';
              for (var i = 0; i < jsonArray.length; i++) {
                    var oneLine = '"';
                       for (var j in jsonArray[i]) {
                          if (oneLine != '"') oneLine += '";"'
                             if(jsonArray[i][j]!=null){
                               partialStr = jsonArray[i][j].replace('"',"'");
                             }                               
                                oneLine += partialStr;
    
                           }
    
                       fullStr += oneLine + '"\r\n';
                  }
         return fullStr;
        }
    
    
            $(document).ready(function() {
                var toShow = {"data":"","elements":[]};
    
                $( ".checkboxs" ).on('click',function(){
                    toShow.elements.push(this.name);
                })
    
                $('#custom_print_date').datepicker({ 
                    dateFormat: 'yy-mm-dd' 
                });
    
                $("#empty").dialog({
                       height:200,
                       width:250,
                       buttons: {
                           "Ok": function(){
                               location.reload();
                           }
    
                       }
                });
    
    
                $( "#custom_print_view" ).dialog({
                       autoOpen: true,
                       modal: true,
                       height:400,
                       width:350,
                        buttons: {
                        "Show": function() {
    
                                $("#empty").dialog("close");
                                toShow.elements.push("inTime");
                                toShow.data = $("#custom_print_date").val();
                            if(toShow.elements.length >= 1){
                                $.post( baseUrl+"CustomPrint/download",{toShow: JSON.stringify(toShow)}).done(function(data){
                                data = JSON.parse(data);
                                columns = elementsToTable(toShow.elements);
                                update(data,columns);
                                //data = JSON.parse(data);
    
                                $('#exportLink').css('display', 'inline-block'); //show button when ready 
                                var filename = 'hardware.csv'; //export .csv file           
                                var blobby = new Blob([makeCSV(data)], {type: 'text/csv;charset=utf-8'}); //make blob object
    
                                        $(exportLink).attr({
                                                    'download' : filename,
                                                    'href': window.URL.createObjectURL(blobby),
                                                    'target': '_blank'
                                                    });
    
                                })
                        }
                        else{
                                $("#empty").dialog("open");
    
                            }
    
                                $(this).dialog("close");
                            }
                        }
                });
    
            function update(data,columns){
                        var result='';
                        table = $('#Date').DataTable({
    
                        "pagingType": "simple",
                        "lengthMenu": [[50, 100, 150,200, -1], [50, 100, 150,200, "All"]],
                        "data": data,
                        "destroy": true,
                        "order": [[ 1, "asc" ]],
                        "scrollY": ($(window).innerHeight()-300)+"px",
                        "scrollCollapse": true,
                        "paging":         true,
                          "columns": columns,
                          "columnDefs": [ {
    
    
                                "targets": 0,
    
                                "render": function ( data, type, full, meta ) {
                                    if(data!=null)
                                        {
                                           return data.replace('"',"'");
                                        }
                                    else{
                                        return data;
                                        }
    
                                }
                              } ]
    
                        })  
    
            }
    
            function elementsToTable(elements){
                var columns = [];
                for(i in elements){
                    if(elements[i]=="Users"){
                        columns.push({ "title": "Name", "class": "center"  });
                        columns.push({ "title": "SurName", "class": "center"  });
                    }
                    if(elements[i]=="Typ"){
                        columns.push({ "title": "Typ", "class": "center"  });
                    }
                    if(elements[i]=="Mark"){
                        columns.push({ "title": "Mark", "class": "center"  });
                    }
                    if(elements[i]=="Model"){
                        columns.push({ "title": "Model", "class": "center"  });
                    }
                    if(elements[i]=="Serial"){
                        columns.push({ "title": "Serial", "class": "center"  });
                    }
                    if(elements[i]=="Inventory"){
                        columns.push({ "title": "Inventory", "class": "center"  });
                    }
                    if(elements[i]=="Finance"){
                        columns.push({ "title": "finance", "class": "center"  });
                    }
                    if(elements[i]=="inTime"){
                        columns.push({ "title": "Got Time", "class": "center"  });
                    }
                }
                return columns;
            }
    
                function downloadView(){
                    $(document).ready(function() {
                        $('#Date').DataTable( {
                            dom: 'Bfrtip',
                            buttons: [
                                'copy', 'csv', 'excel', 'pdf', 'print'
                            ]
                        } );
                    } );
                }
            });
    
  • yooeltonyooelton Posts: 4Questions: 0Answers: 0

    and in another files .php there is:
    <a id="exportLink">Export to CSV</a>

  • yooeltonyooelton Posts: 4Questions: 0Answers: 0

    i've got it physically, problem is its data is not updating due to searching datables by search box

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin

    It looks to me like you are making a request to the server to get the data to export, so I don't really see how DataTables' filtering would come into play. You'd need the server to filter the data if you are getting it from there.

    Allan

This discussion has been closed.