string joining

string joining

Henry RiekeHenry Rieke Posts: 3Questions: 1Answers: 0

Hello,

Can't seem to hack this out, so I am a wee bit embarrassed to inquire what I could be doing wrong.
I have a bash script to generate data in a json file that I wish to deliver to users via a web page.

I want to join some data elements together since we don't need to have twenty columns of data scrolling across the screen, but I still want to keep the data normalized for future proofing the data.
For the record, I have followed the examples given on:

https://www.datatables.net/examples/advanced_init/column_render.html
https://datatables.net/manual/data/renderers#Functions

Some work :smiley: , others don't :frowning:

This is what I have:

$(document).ready(function() {
    $('#Report_table').DataTable( {
        "ajax": {
            "url": "http://__URL__/__PATH__/__FILE__.json",
            "dataSrc": ""
        },
        "columnDefs": [
            {  "targets": 0, "render": function ( data, type, full, meta ) { return type === 'display' && data.length > 40 ? '<span title="'+data+'">'+data.substr(0,38)+'...</span>' : data; } },
            {  "targets": 1,  "render": function ( data, type, full, meta ) { return '<a href="http://www.google.com/webhp?q='+data+'" target="_blank">'+data+'</a>'; } },
            {  "targets": 2,  "render": function ( data, type, row ) { return data + '<br>' + data[3]; }, },

        ],
        "columns": [
            { "data": "Column_A" },
            { "data": "Column_B" },
            { "data": "Column_C" },
            { "data": "Column_D" },
        ]
    } );
} );
// $.fn.dataTable.ext.errMode = 'none';

Results:

Column_A  |  Column_B  |  Column_C  |  Column_D
Data         Data         Data         Data
                          undefined   <----- this should show Column_D's data

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,871Questions: 1Answers: 10,526 Site admin

    I'd suggest you merge your columnDefs and columns arrays together - I think its just an extra level of indirection splitting them up and only using a single target for each def. e.g.:

    { data: "Column_A", render: function ... }
    

    Having said that, you are telling column index 2 to get the data from Column_C and then using: data[3] - that would be the same as Column_C[3].

    Is Column_C always an array, and do you actually want the fourth element from it every time?

    Allan

  • Henry RiekeHenry Rieke Posts: 3Questions: 1Answers: 0

    Thank you Allan for your response.

    For now, as I am still learning how to use Datatables, my plan is to keep things separated until I understand Datatables better.

    if I am understanding your question at the end, in my json file, each row of data is an array.

    [{
      "Column_A": "Value 1",
      "Column_B": "Value 2",
      "Column_C": "Value 3",
      "Column_D": "Value 4"
    }]
    

    The way that I am thinking of this problem is akin to Excel. I want to take the data from C1, D1 and join them in cell C1, and then hide cell D1.
    Using my example json data, my output should be:

    Column_A  |  Column_B  |  Column_C
    Value 1      Value 2      Value 3         
                              Value 4   <----- this should show Column_D's data
    
    
  • allanallan Posts: 63,871Questions: 1Answers: 10,526 Site admin
    Answer ✓
    {
      data: 'Column_C',
      render: function ( data, type, row ) {
        return data+'<br>'+row.ColumnD;
      }
    }
    

    which is basically the same as:

    {
      data: 'Column_C',
      render: function ( data, type, row ) {
        return row.Column_C+'<br>'+row.ColumnD;
      }
    }
    

    since data points to the Column_C property.

    Allan

  • Henry RiekeHenry Rieke Posts: 3Questions: 1Answers: 0

    Ding! Ding! Ding!
    We have a winner. Thank you Allan.

    Now, my final question to you is this:
    How can I help you improve on your documentation to assist others?

  • allanallan Posts: 63,871Questions: 1Answers: 10,526 Site admin

    Tell me where you think it needs to be improved! The rendering page covers this, but is there anything missing?

    Allan

This discussion has been closed.