+ Start a Discussion
anil Kumaranil Kumar 

How to show and hide columns and based other column value in the CSV file

Hi All,

We have implemented download all the Assest object records in the CSV file based on the button click in the lightning compnent. Now i need show and hide columns and based other column value in the CSV file.

For example if the column A value equals source i need to hide column E and F in the CSV file and column A value not equals source i need to show column E and F in the CSV.

Below is the code which we have used in component JS to dlownload all the records.



  downloadCsvrecords : function(component,event,helper){
        var lstPositions = component.get("v.fullResults");
        console.log('lstPositions=== : '+ lstPositions.length);
        var data = [];
        var headerArray = [];
        var csvContentArray = [];        
        //Fill out the Header of CSV
        //headerArray.push('Installed Product');
        headerArray.push('Product Line');
        headerArray.push('Serial Number');
        headerArray.push('System ID / Asset#'); 
        headerArray.push('Eligibility');
        headerArray.push('Remote Access');
        headerArray.push('Location');
          
        data.push(headerArray);
        
        for(var i=0;i<lstPositions.length;i++){
            
            //Check for records selected by the user
            if(lstPositions[i]){
                console.log('lstPositions.length : '+ lstPositions.length);
                //Initialize the temperory array
                var tempArray = [];
                //use parseInt to perform math operation
                /*tempArray.push('"'+lstPositions[i].Name+'"');*/
                if(lstPositions[i].Product_Line__c === undefined || lstPositions[i].Product_Line__c == null){
                    tempArray.push('"'+''+'"');
                }else{tempArray.push('"'+lstPositions[i].Product_Line__c+'"');}
                if(lstPositions[i].Serial_Lot_Number__c === undefined || lstPositions[i].Serial_Lot_Number__c == null){
                    tempArray.push('"'+''+'"');
                }else{tempArray.push('"'+lstPositions[i].SVMXC__Serial_Lot_Number__c+'"');}
                if(lstPositions[i].Asset_Tag__c === undefined || lstPositions[i].Asset_Tag__c == null){
                    tempArray.push('"'+''+'"');
                }else{tempArray.push('"'+lstPositions[i].SVMXC__Asset_Tag__c+'"');}
                if(lstPositions[i].SM_Eligibility_Status__c === undefined || lstPositions[i].SM_Eligibility_Status__c ==  null){tempArray.push('"'+''+'"');
                }else{tempArray.push('"'+lstPositions[i].BH_US_SM_Eligibility_Status__c+'"');}
                if(lstPositions[i].SM_VirtualCare_Enabled__c === undefined || lstPositions[i].SM_VirtualCare_Enabled__c == null){tempArray.push('"'+''+'"');
                }else{tempArray.push('"'+lstPositions[i].SM_VirtualCare_Enabled__c+'"');}    
                if(lstPositions[i].Site__c === undefined || lstPositions[i].Site__c == null){
                    tempArray.push('"'+''+'"');
                }else{tempArray.push('"'+lstPositions[i].Site__r.Name+'"');}
                             
         
                data.push(tempArray);
            }
            
        }
        console.log('data.length : '+ data.length);
        for(var j=0;j<data.length;j++){
            var dataString = data[j].join(",");
            csvContentArray.push(dataString);
        }
        var csvContent = csvContentArray.join("\n");
        
        
       
        var fileName = "My Assets"; //Generate a file name
        fileName += ".csv";            //this will remove the blank-spaces from the title and replace it with an underscore 
     
        
        if (navigator.msSaveBlob) { // IE 10+
            console.log('----------------if-----------');
            var blob = new Blob([csvContent],{type: "text/csv;charset=utf-8;"});
            console.log('----------------if-----------'+blob);
            navigator.msSaveBlob(blob, fileName);
        }
        else{ 
          var myBlob =  new Blob( [csvContent] , {type: 'text/html'});
         var url = window.URL.createObjectURL(myBlob);
         var a = document.createElement("a");
         document.body.appendChild(a);
         a.href = url;
         a.download = "My Assets.csv";
         a.click();
        //adding some delay in removing the dynamically created link solved the problem in FireFox
         setTimeout(function() {window.URL.revokeObjectURL(url);},0);
            component.set("v.isDownloadcsv",false);
            
        }
 
    }

Thanks,
Anil Kumar