How to apply foreach statements to columns option

How to apply foreach statements to columns option

jhjhjhjh Posts: 10Questions: 5Answers: 0

If the data value using ajax is flexible (Depending on the search results, the number of data may be three or N.),
can the foreach statement be applied in columns in the option of DataTable.js?

$.DataTable({ ajax: {}, 
                      columns:[ data: , "render": function(){ //... }  ],      //I want to apply a loop here.
                      etc..
})

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    No, but if the data is an array, you don't need to specify the source as it takes the place in the array by default - this example here may help : http://live.datatables.net/qakopare/10/edit

    Colin

  • jhjhjhjh Posts: 10Questions: 5Answers: 0

    What should I do if I want to express only some of the values in the array?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Then you would need to explicitly reference those columns, in either columns or columnDefs. It might be useful if you backtrack and explain what you're trying to do, with a test case, then we may be able to offer an alternative approach. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • jhjhjhjh Posts: 10Questions: 5Answers: 0
    edited July 2021

    http://live.datatables.net/jolaxave/1/edit?html,css,js,console,output

    Thanks! This is json from ajax.

    I would like to express the 2016-2021 value in List2 in data. (Except UNIT_VAL, UNIT_CD, ssr_id)

    But the year could be 2022 later, so columns want to change automatically. Will it be possible?
    (The value in HTML changes automatically.)

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    I'm sorry, I'm not following that at all! Can you give an example of where the columns are laid out as you'd want them, please.

    Colin

Sign In or Register to comment.