Using a function to generate child row data

Using a function to generate child row data

NeighlydNeighlyd Posts: 10Questions: 4Answers: 0

My child rows have access to objects that I want to iterate through and concatenate into a field using a javascript function. However, when I call the function in the format function for my child rows, they only display "undefined."

My format row is:

function format(d) {
    return "<table class='table'>" +
               "<tr>"+
               "<td>Litigants:</td>" +
               "<td>" + list_litigants(data.litigant_list) + "</td>"
               "</tr>" +
               "</table>"
     }

And the function for list_litigants is:

function list_litigants(data){
       data.forEach(function (obj){
              concat='Name: ' + obj.name + ', Role: ' + obj.role + '; ';
              console.log(concat);
              return concat;
       })
};

The console.log is showing that the function is triggering just fine. However, the string isn't getting passed through to Datatables. Is there an extra step I need to take for it to render properly?

This question has an accepted answers - jump to answer

Answers

  • crwdzrcrwdzr Posts: 31Questions: 5Answers: 6

    try this

    function format(data) {
        return "<table class='table'>" +
                   "<tr>"+
                   "<td>Litigants:</td>" +
                   "<td>" + list_litigants(data.litigant_list) + "</td>"
                   "</tr>" +
                   "</table>"
         }
    

    format() had an argument of d therefor anything in that data object being passed would never be defined :)

  • NeighlydNeighlyd Posts: 10Questions: 4Answers: 0

    Thank you for your quick response.

    I apologize, that was a typo on my part when I transcribed my code into the browser window.

    The format() function was using the same variable as the call to list_litigants.

    so:

    function format(data){
        return "<table class='table'>"+
            "<tr>"+
            "<td>Litigants:</td>"+
            "<td>" + list_litigants(data.litigant_list) + "</td>" +
            "</tr>" +
            "</table>"
    }
    

    The console is spitting out the error: Uncaught TypeError: Cannot read property 'forEach' of undefined at list_litigants

  • crwdzrcrwdzr Posts: 31Questions: 5Answers: 6

    with the code you've shown, assuming that data is structured properly, there should be no errors, this:

    function format(data) {
        return "<table class='table'>" +
            "<tr>"+
            "<td>Litigants:</td>" +
            "<td>" + list_litigants(data.litigant_list) + "</td>"
            "</tr>" +
            "</table>"
    }
    function list_litigants(data){
        data.forEach(function (obj){
            concat='Name: ' + obj.name + ', Role: ' + obj.role + '; ';
            console.log(concat);
            return concat;
        })
    };
    
    let data = {
        litigant_list: [
            {name: 'foo', role: 'bar'},
            {name: 'baz', role: 'qux'},
        ],
    }
    format(data);
    

    outputs:
    Name: foo, Role: bar;
    Name: baz, Role: qux;

    I would have to say either the error is in the structuring of the data variable or somewhere else in the process

  • NeighlydNeighlyd Posts: 10Questions: 4Answers: 0

    Hello,

    Thank you for replying.

    The console.log(concat) is indeed showing the appropriate results (the concat results). So I think the data is structured appropriately:

            "litigant_list": [
                {
                    "name": "Foo",
                    "role": "bar"
                },
                {
                    "name": "baz",
                    "role": "qux"
                }
            ],
    

    However, the table cell is still showing undefined.

    When I use your formulation (not pulling data from ajax, but using a let function), I get the same undefined cell with the appropriate concat output to console.log as well.

  • allanallan Posts: 63,468Questions: 1Answers: 10,466 Site admin

    At the top of your format function could you do console.log( JSON.stringify( data ) ) and show us the result from the console?

    Thanks,
    Allan

  • NeighlydNeighlyd Posts: 10Questions: 4Answers: 0

    I have pasted the results of the console.log( JSON.stringify (data)) below. As you can see, the litigant list is nested within case. I have called list_litigants(data.case.litigants) and the cell still reads as undefined, even though the console.log(concat) in the list_litigants function works fine.

    The litigant_list_concat is the programmatically created version that I have my django model creating right now as a stop gap until I get the Datatables version working.

    Also, even though this is court data, it is 700 years old so I don't need to obfuscate the litigants' names or details.

    {
    "id":3466,
    "amercement":null,
    "damage":null,
    "damage_notes":"",
    "ad_proximum":null,
    "role": {
          "id":37,
          "role":"Mentioned"
          },
    "impercamentum_notes":null,
    "chevage_id":null,
    "heriot_assessment_id":null,
    "fine_id":null,
    "chevage":null,
    "heriot_animal":null,
    "impercamentum_animal_id":null,
    "impercamentum_animal":null,
    "habet_terram":null,
    "person_id":1350,
    "heriot_assessment":null,
    "attached":null,
    "distrained":null,
    "heriot_animal_id":null,
    "bail":null,
    "heriot_quantity":null,
    "case": {
            "id":1775,
            "of_interest":false,
            "ad_legem":false,
            "litigant_list_concat":"<div class=d-flex justify-content-end><div class='mr-auto p-0'><u>Name:</u> Walterus Bailly</div> <div class='p-0'><u>Role:</u> Defendant</div></div><hr><div class=d-flex justify-content-end><div class='mr-auto p-0'><u>Name:</u> Abbot of Tewkesbury</div> <div class='p-0'><u>Role:</u> Mentioned</div></div><hr>",
            "case_type": {
                       "id":66,
                       "case_type":"Does Not Reside"
                       },
            "court_type":"Hallmoot",
            "verdict_id":3,
            "summary":"Amercement in lieu of forefeiture. Claims that Walter lives on land of the Abbot of Tewkesbury.",
            "active_sale":false,
            "villeinage_mention":false,
            "session_id":55,
            "litigant_list": [
                            {
                            "name":"Walterus Bailly",
                            "role":"Defendant"
                            },
                            {
                            "name":"Abbot of Tewkesbury",
                            "role":"Mentioned"
                            }
                            ],
            "verdict": {
                     "id":3,
                     "verdict":"Amercements"
                     },
            "case_type_id":66,
            "incidental_land":false,
            "litigant_count":2,
            "session": {
                     "id":55,
                     "date":"1370-11-05",
                     "notes":"Tuesday after All Saints - Nov 1",
                     "record_id":14,
                     "law_term":"Michaelmas",
                     "year":1370,
                     "village_id":48,
                     "folio":"30v & 31"
                     }
            },
    "impercamentum_amercement_id":null,
    "chevage_notes":null,
    "impercamentum_amercement":null,
    "role_id":37,
    "amercement_id":null,
    "crossed":null,
    "damage_id":null,
    "recessit":null,
    "impercamentum_quantity":null,
    "case_id":1775,
    "fine":null
    }
    
  • allanallan Posts: 63,468Questions: 1Answers: 10,466 Site admin

    It looks like you would want to use:

    list_litigants(data.case.litigant_list)
    

    Allan

  • NeighlydNeighlyd Posts: 10Questions: 4Answers: 0

    I figured out the problem... it was a simple and very stupid mistake on my part. I wasn't returning the concat data properly.

    My function was:

    function list_litigants(data){
        data.forEach(function (obj){
            concat='Name: ' + obj.name + ', Role: ' + obj.role + '; ';
            return concat;
        })
    };
    

    When it should have been:

    function list_litigants(data){
        var concat = '';
        data.forEach(function (obj){
            concat= concat + 'Name: ' + obj.name + ', Role: ' + obj.role + '; ';
            return concat;
        })
      return concat;
    };
    

    Without that extra return concat, the concat variable had no way to return itself from the function.

    A completely rookie mistake and I feel like an idiot now...

    Thank you everyone for your help!

  • allanallan Posts: 63,468Questions: 1Answers: 10,466 Site admin
    Answer ✓

    We all do it, all the time, no matter how long we've been programming!

    Good to hear you've got it working now.

    Allan

This discussion has been closed.