1

Below is the data I'm trying to display in a datatable. As you can see permitbrands is a json array and i want to display it in one column. If it was json object it would have been easier but this is json array.

 {
 id: 1,
 total_cases: 13,
 permitbrands: [
       {
         id: 1,
         br_name: "Apple",
         br_no: "12",
         permit_id: 1,
       },
       {
         id: 2,
         br_name: "Mango",
         br_no: "36", 
         permit_id: 1,
        }
  ],
}

Below is my code for datatable column:

$(document).ready( function () {
    table = $('.table').DataTable({
        processing: true,
        serverSide: true,
        paging:true,
        ajax : '/permits/search',
        columns: [
               { data: 'total_cases', name: 'total_cases'},
               {
                data: 'permitbrands[,].br_name',
               },
        ],
    });

});

But The result shown is Apple,Mango.

I want to show the value in column as Apple 12 , Mango 36.

How can this be done? Can anyone please suggest me a solution.?

I tried looping it but it gets looped twice.

   {
    data: 'permitbrands[]',
          render: function ( data , row ) {
                  var output='';
                   $.each(data, function(index,item) {
                    alert(index);
                    output+= data[index].br_no+' '+data[index].br_name;
                  });
                 return output;
               }
    },

The result i get is like this:

12 12 Apple , 36 36 Mango.

I dont know why this happens. But looping shows me alert 4 times instead of 2.

5
  • 2
    Your code works fine here ... Commented Jan 8, 2019 at 6:01
  • But idk why but... the alert is shown 4 times .. when it should be just called 2 times. Commented Jan 8, 2019 at 7:48
  • Look at the type param, that is why the render callback is called 4 times. Commented Jan 8, 2019 at 7:50
  • could you please show me a sample? Commented Jan 8, 2019 at 7:59
  • render: function ( data ,type, row ){ return type; } , I passed type like this and it returns "display" as the value. What should i do? Commented Jan 8, 2019 at 8:09

1 Answer 1

1

Maybe you should try checking your JSON data that is being looped in the function. There is no other reason for your code not to work. Please check your data carefully maybe "12 Apple" or "36 Mango" maybe coming from one field of the table.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.