data.replace is not a function

data.replace is not a function

RappiRappi Posts: 82Questions: 18Answers: 1

Hello.

I have a problem with export.

extend: 'print',
            title: '',
            messageTop: '<center><strong>Bestandsbuch<br /><?php echo $verein;?></strong><br /><?php echo $von;?> bis <?php echo $bis;?></center>',
            text: 'Drucken',
            stripHtml: true,
            stripNewlines:true,
            exportOptions: {
                    columns: ':visible',
                    format: {
                        body: function (data, row, column, node) {
                            //return data.replace(/\n/ig, "<br/>");
                            //return column === 2 ? data.replace(/\n/ig, "<br/>") : data;
                        }
                    }
                },
            customize: function (win) {
                $(win.document.body).find('table').addClass('display').css('font-size', '8px');
                $(win.document.body).find('tr:nth-child(odd) td').each(function(index){
                    $(this).css('background-color','#D0D0D0');
                });
                $(win.document.body).find('h1').css('text-align','center');
            
            
            }
            },

if i use return data.replace(/\n/ig, "<br/>"); then above error appears.

If I use return column === 5 ? data.replace(/\n/ig, "<br/>") : data; no error appears but I want to render not only column 5 but all columns.

What can I do to render all columns?

Rappi

Answers

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949

    Your code snippet seems to work here:
    http://live.datatables.net/xovarumu/1/edit

    There must be something specific with your data causing the issue. Please provide a link to your page or update this test case to show the issue.

    Kevin

  • RappiRappi Posts: 82Questions: 18Answers: 1

    Thanks for the quick reply. The system is a closed system. So unfortunately I can not provide a link. Since it is data composed of several table columns, I don't know how to get the sample data into the test case....

    Here is an excerpt of how the data is put together.

            columns: [
                {
                    data: "tm_tiere.id"
                },
                
                {
                    data: "tm_tiere.lfdnr"
                },
                
                {
                    searchable: false,
                    orderable: false,
                    data: null,
                    render: function ( data, type, row ) {
                        
                        var name = row.tm_tiere.Name;
                        var rasse = row.tm_tiere.Rasse;
                        var art = row.tm_tierart.Art;
                        var transponder = row.tm_tiere.Chip1;
                        var geschlecht = row.tm_tiere.Geschlecht;
                        var age = row.tm_tiere.Age;
                        
                        var geburtsdatum = row.tm_tiere.Geburtsdatum;
                        
                          if (geburtsdatum == '0000-00-00') {
                            var gdat = '0000-01-01';
                            //var geburt = (moment(gdat).format("DD.MM.YYYY"));
                            var geburt = '';
                            if (age != ''){
                                var geburt = row.tm_tiere.Age;
                            }
                          } else {
                            var geburt = (moment(geburtsdatum).format("DD.MM.YYYY"));
                          }       
                          
                          return 'Name: '+name+'<br />Art: '+art+'<br />Rasse: '+rasse+'<br />Sex: '+geschlecht+'<br />Geburtsdatum: '+geburt; 
                    }
                },
                {
                    searchable: false,
                    orderable: false,
                    data: null,
                    render: function ( data, type, row ) {
                        
                        var merkmale = row.tm_tiere.Merkmale;
                        var kastriert = row.tm_tiere.Kastriert;
                        var transponder = row.tm_tiere.Chip1;
                        var tattoo = row.tm_tiere.Chip2;
                        var ringnummer = row.tm_tiere.Ringnummer;
                        var ausweisart = row.tm_tiere.Ausweis;
                        var ausweisnr = row.tm_tiere.Ausweisnr;
                        
                        if (merkmale == ''){
                            merkmale = 'Keine';
                        }
                        if (transponder == ''){
                            transponder = 'NV';
                        }
                        if (tattoo == ''){
                            tattoo = 'NV';
                        }
                        if (ringnummer == ''){
                            ringnummer = 'NV';
                        }
                        if(transponder != 'NV' && ringnummer == 'NV' && tattoo == 'NV'){
                            //transponder = 'Keiner vorh&&en';
                            return 'Besondere Merkmale: '+merkmale+'<br />Kastriert: '+kastriert+'<br />Transponder: '+transponder+'<br />Ausweisart: '+ausweisart+'<br />Auswweisnr.: '+ausweisnr;
                        } else if(tattoo != 'NV' && transponder == 'NV' && ringnummer == 'NV'){
                            return 'Besondere Merkmale: '+merkmale+'<br />Kastriert: '+kastriert+'<br />Tattoo: '+tattoo+'<br />Ausweisart: '+ausweisart+'<br />Auswweisnr.: '+ausweisnr;
                        } else if(ringnummer != 'NV' && transponder == 'NV' && tattoo == 'NV'){
                            return 'Besondere Merkmale: '+merkmale+'<br />Kastriert: '+kastriert+'<br />Ringnummer: '+ringnummer+'<br />Ausweisart: '+ausweisart+'<br />Auswweisnr.: '+ausweisnr;
                        } else if(transponder != 'NV' && ringnummer != 'NV' && tattoo == 'NV'){
                            return 'Besondere Merkmale: '+merkmale+'<br />Kastriert: '+kastriert+'<br />Transponder: '+transponder+'<br />Ringnummer: '+ringnummer+'<br />Ausweisart: '+ausweisart+'<br />Auswweisnr.: '+ausweisnr;
                        } else if(transponder != 'NV' && tattoo != 'NV' && ringnummer == 'NV'){
                            return 'Besondere Merkmale: '+merkmale+'<br />Kastriert: '+kastriert+'<br />Transponder: '+transponder+'<br />Tattoo: '+tattoo+'<br />Ausweisart: '+ausweisart+'<br />Auswweisnr.: '+ausweisnr;
                        } else if(transponder != 'NV' && tattoo != 'NV' && ringnummer != 'NV'){
                            return 'Besondere Merkmale: '+merkmale+'<br />Kastriert: '+kastriert+'<br />Transponder: '+transponder+'<br />Tattoo: '+tattoo+'<br />Ringnummer: '+ringnummer+'<br />Ausweisart: '+ausweisart+'<br />Auswweisnr.: '+ausweisnr;
                        } else if(tattoo != 'NV' && ringnummer != 'NV' && transponder == 'NV'){
                            return 'Besondere Merkmale: '+merkmale+'<br />Kastriert: '+kastriert+'<br />Tattoo: '+tattoo+'<br />Ringnummer: '+ringnummer+'<br />Ausweisart: '+ausweisart+'<br />Auswweisnr.: '+ausweisnr;
                        } else {
                            return 'Besondere Merkmale: '+merkmale+'<br />Kastriert: '+kastriert+'<br />Transponder: '+transponder+'<br />Tattoo: '+tattoo+'<br />Ringnummer: '+ringnummer+'<br />Ausweisart: '+ausweisart+'<br />Auswweisnr.: '+ausweisnr;
                        }
                    }
                    
                },
                
                {   
                    searchable: false,
                    orderable: false,
                    data: null,
                    render: function ( data, type, row ) {
                        
                          var rowvalueallauf = row.tm_tiere.Aufnahmedatum;
                            var grund = row.tm_tiere.Aufnahmegrund;
                            var fundort = row.tm_tiere.Fundort;
                            var rowgefundenam = row.tm_tiere.Fundam;
                            
                          if (rowvalueallauf == '0000-00-00') {
                            var gdat = '0000-01-01';
                            //return (moment(gdat).format("DD.MM.YYYY"));
                            var aufnahme = (moment(gdat).format("DD.MM.YYYY"));
                          } else {
                            //return (moment(data).format("DD.MM.YYYY"));
                            var aufnahme = (moment(rowvalueallauf).format("DD.MM.YYYY"));
                          }       
                          if (rowgefundenam == '0000-00-00') {
                            var gdat = aufnahme;
                            //return (moment(gdat).format("DD.MM.YYYY"));
                            var gefundenam = (moment(gdat).format("DD.MM.YYYY"));
                          } else {
                            //return (moment(data).format("DD.MM.YYYY"));
                            var gefundenam = (moment(rowgefundenam).format("DD.MM.YYYY"));
                          }
                          
                          if(grund == ''){
                              return 'Aufnahmedatum: '+aufnahme+'<br />Grund: Unbekannt'; 
                          } else if(fundort == ''){
                              return 'Aufnahmedatum: '+aufnahme+'<br />Grund: '+grund; 
                          } else {
                              return 'Aufnahmedatum: '+aufnahme+'<br />Grund: '+grund+'<br />Fundort: '+fundort+'<br />Gefunden am: '+gefundenam; 
                          }
                    }
                },
    
                
                {
                    searchable: false,
                    orderable: false,
                    data: null,
                    render: function ( data, type, row ) {
                        
                          var rowvalueallver = row.tm_tiere.Pflegestelle;
                            var name = row.tm_pflegestellen.Name+', '+row.tm_pflegestellen.Vorname;
                            var strasse = row.tm_pflegestellen.Strasse;
                            var ort = row.tm_pflegestellen.PLZ+' '+row.tm_pflegestellen.Ort;
                            var land = row.tm_land.land;
                        
                            if(land == null){
                                var land = '';
                            }
                            
                          if(rowvalueallver == '0'){
                            
                            return '';  
                          } else {
                            return 'Pflegestelle: '+name+'<br />'+strasse+'<br />'+ort+'<br />'+land; 
                          }
                          
                    }
                },
            
                
                
                {
                    searchable: false,
                    orderable: false,
                    data: null,
                    render: function ( data, type, row ) {
                        
                          var woher = row.tm_tiere.Woher;
                          var intra = row.tm_tiere.Intra;
                          
                          if(intra != ''){
                            return woher+'<br />Intra: '+intra; 
                          } else {
                            return woher;  
                          }
                    }
                },
                
                
                
                {
                    searchable: false,
                    orderable: false,
                    data: null,
                    render: function ( data, type, row ) {
                        
                          var rowvalueallver = row.tm_tiere.Vermitteltdatum;
                            var name = row.tm_adoptanten.Name+', '+row.tm_adoptanten.Vorname;
                            var strasse = row.tm_adoptanten.Strasse;
                            var ort = row.tm_adoptanten.PLZ+' '+row.tm_adoptanten.Ort;
                            var land = row.tm_land.land;
                        
                            if(land == null){
                                var land = '';
                            }
                            
                          if (rowvalueallver == '0000-00-00') {
                            var fdat = '0000-01-01';
                            var vermittelt = (moment(fdat).format("DD.MM.YYYY"));
                          } else {
                            var vermittelt = (moment(rowvalueallver).format("DD.MM.YYYY"));
                          }
                          if(vermittelt == '01.01.0000'){
                            
                            return 'Noch nicht vermittelt';  
                          } else {
                            return 'Abgabedatum: '+vermittelt+'<br />Besitzer: '+name+'<br />'+strasse+'<br />'+ort+'<br />'+land; 
                          }
                          
                    }
                },
                
                
            
                
                {
                    data: "tm_tiere.Geloescht",
                    render: function ( data, type, row ) {
                        if ( type === 'display' ) {             //if column data is 1 then set attr to checked, use row id as input id (plus prefix)
                            return '<input type="checkbox" ' + ((data == 1) ? 'checked' : '') + ' id="input' + row.id + '" class="filter-ck" />';
                        }
                        return data;
                      },
                      className: "dt-body-center"
                     },
                {
                    data: "tm_tiere.LGrund"
                },
                {
                    data: "Impfungen"
                }
        
            
            ],
    

    And this is my table:

    <table cellpadding="0" cellspacing="0" border="0" class="display nowrap dt-responsive table table-bordered" id="tm_sl_bestandsbuch" width="100%">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Lfd. Nr.</th>
                            <th>Tierdaten</th>
                            <th>Besondere Merkmale</th>
                            <th>Aufnahme</th>
                            <th>Unterbringung</th>
                            <th>Herkunft</th>
                            <th>Abgabe</th>
                            <th>Gelöscht</th>
                            <th>Löschgrund</th>
                            <th>Impfungen</th>
                        </tr>
                    </thead>
                </table>
    
  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949
    edited February 2021

    If your error is occurring with this code:

                        format: {
                            body: function (data, row, column, node) {
                                return data.replace(/\n/ig, "<br/>");
                            }
    

    Then you will need to debug the value of data to determine why this error occurs. Use console.log or the browser's debugger to monitor data and maybe the column number. It could be something with one of the rendered columns or maybe the value is a number. It will be difficult to debug with only looking at the code. Maybe look at some Stack Overflow Threads to get an idea of why you might get this error.

    Or you can add an if statement to check that data is a string and if so use the replace statement.

    Kevin

  • RappiRappi Posts: 82Questions: 18Answers: 1

    ok, I found the column that is responsible for the error and by an if query excluded this column from data.replace.
    Now it works.
    Thanks.

This discussion has been closed.