How to concat two or more columns of same datatabase table to show in a single datatable column

How to concat two or more columns of same datatabase table to show in a single datatable column

usha2003usha2003 Posts: 5Questions: 3Answers: 0

I wanted multiple values form different database columns to show in a single datatable(Editor) column with comma separated values

This question has accepted answers - jump to:

Answers

  • emilemil Posts: 7Questions: 1Answers: 1
    Answer ✓

    You can use data from any cell in the same row. You can also use HTML code to format each field.

    {
                    render: function ( data, type, row ) {
                       return row[0] + " " + RemoveAccents(row[1]) + " " + RemoveAccents(row[2]) + " " + RemoveSpaces(row[3]) + " " + RemoveSpaces(row[4]) + " " + RemoveAccents(row[5]) + " " + RemoveAccents(row[6]) + " k" + row[7] + " " + RemoveSpaces(row[9]);
                    },
                    targets: 8,
                    visible: true,
                    searchable: true 
                },
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    This is correct - use the columns.render option to render data to suit your own needs.

    See also the renderers manual page.

    Allan

  • usha2003usha2003 Posts: 5Questions: 3Answers: 0

    Thanks for the help. But I achieved it in a similar manner.
    I will show how I solved my case thinking it might help others.
    All I wanted was to Concat Different Cols from Database Table to show in Datatable but with single column name hence I used it like this:

    "columnDefs": [ {
                                    "targets": [12],
                                    "data": "purchaseorders.inst1InvoiceNo",
                                    "render": function ( data, type, full, meta ) {
                                        var invNo = full.purchaseorders;
                                        var conCat = '';
                                        if(invNo.inst1InvoiceNo)
                                            conCat += invNo.inst1InvoiceNo + ', ';
                                        if(invNo.inst2InvoiceNo)
                                            conCat += invNo.inst2InvoiceNo + ', ';
                                        if(invNo.inst3InvoiceNo)
                                            conCat += invNo.inst3InvoiceNo + ', ';
                                        if(invNo.inst4InvoiceNo)
                                            conCat += invNo.inst4InvoiceNo + ', ';
                                        if(invNo.inst5InvoiceNo)
                                            conCat += invNo.inst5InvoiceNo + ', ';
                                        if(invNo.inst6InvoiceNo)
                                            conCat += invNo.inst6InvoiceNo + ', ';
                                        conCat = conCat.substr(0,conCat.length-2);
                                        return conCat;
                                    }
                                } ];
    

    In this case I wanted the values of different columns to be seperated with comma .And I am making use of 'full' argument to get values of all fields fetched from database and combining required fields in specific format.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Thanks for posting back - using columns.render in that way looks like a good option.

    Allan

This discussion has been closed.