columndef vs columns

columndef vs columns

montoyammontoyam Posts: 568Questions: 136Answers: 5

I saw a post where someone was using both columndef AND columns:
https://datatables.net/forums/discussion/71535/how-to-pass-record-from-table-to-partial-view#latest

Is there an advantage to doing it this way? Or would it be better to use only one of these?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,261Questions: 26Answers: 4,934
    Answer ✓

    The answer is it depends :smile:

    The columns option is generally used when defining options for each column, like columns.data. In this case the advantage of columnDefs is to apply the same config options to multiple columns. The code in the thread you linked could be reduced to this:

            columnDefs: [
                {
                    targets: [0],
                    visible: true,
                    searchable: true,
                    orderable: true
                },
                {
                    targets: [1,2,3,4,5,6,7,8],
                    visible: false,
                    searchable: false,
                    orderable: false
                },
                {
                    targets: [9],
                    visible: true,
                    searchable: false,
                    orderable: false
                },
           ],
           columns: [ .... ],
    

    The first one, using columnDefs.targets [0], is not needed as those are all default values.

    columnDefs is also useful when you aren't using columns to define all the columns. Otherwise you will need to do something like the columns example.

    You will want to understand the columnDefs conflict resolution rules.

    HTH,
    Kevin

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    ah, i see. so even if you are using columns.data to define the columns, it could be a timesaver to not define other properties there if they are shared by other columns. you would use columnDefs to group columns with the same properties. so that is a case where you would use both methods, like in your example.

    very interesting. time to go through all my projects and do some code cleanup.

  • kthorngrenkthorngren Posts: 21,261Questions: 26Answers: 4,934

    If it ain't broke - don't fix it :smile:

    Kevin

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    :D

Sign In or Register to comment.