render array with two fields

render array with two fields

montoyammontoyam Posts: 568Questions: 136Answers: 5

I have an mjoin with a one to many situation. I want to return a comma seperated list of First and Last Name. So, basically I am needing to merge these two:

            ,{
                data: "CaseDefendants",
                render:  {
                    _: '[, ].DefendantFirstName'
                }
            }

and

            {
                data: null,
                title: "Defendants",
                render: function (data, type, row) {
                    return row.DefendantFirstName+ ' ' + row.DefendantLastName;
                }
            },

but i can't figure out the correct syntax.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,180Questions: 1Answers: 10,411 Site admin
    Answer ✓

    You’d need to write a custom renderer for it:

    data: ‘myArray’,
    render: function (data) {
      return data.map( function (o) {
        return o.DefendantFirstName + ‘ ‘ + o.DefendantLastName;
      } );
    }
    

    Allan

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    thanks. i don't know a lot of javascript/jquery functions. .map did the trick. thanks. at first i was wondering where the comma separator was going to come from when looking at your code, but in reading about .map i see that it is returning an array, which is by nature separated by commas??

  • allanallan Posts: 63,180Questions: 1Answers: 10,411 Site admin

    Oops - I should have added .join(', '); there! What is probably happening at the moment is that Javascript will be taking the toString() of the array return which is basically the values comma separated. I think if you look closely at them, you might find that they don't have spaces at the moment. Adding the join will fix that.

    Allan

This discussion has been closed.