Why does column.table() call change the column context?

Why does column.table() call change the column context?

amalnevamalnev Posts: 3Questions: 1Answers: 0
edited July 16 in Free community support

When you have a column stored in a variable like:

const column = api.column(1)

and then call #table method on it like:

column.table()

your column context is altered

https://live.datatables.net/yabiropo/1

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 64,749Questions: 1Answers: 10,713 Site admin
    Answer ✓

    table() is a top level method - it is not a child of column(), so this is fully expected and correct in how the DataTables API works. When you call a top level method will to change the context to that method's return object.

    Allan

  • amalnevamalnev Posts: 3Questions: 1Answers: 0
    edited July 17

    @allan so what's a better way to get the table if I have a column? Without altering the column context for sure.

  • allanallan Posts: 64,749Questions: 1Answers: 10,713 Site admin

    Can you explain what your goal is? I will hopefully be able to help come up with an answer.

    To answer your specific question, you can't get the table() and retain the column context - that isn't how the API works. However, I'm sure there will be a way to do whatever you ultimately need to do, but I don't know what your end goal is?

    Allan

  • amalnevamalnev Posts: 3Questions: 1Answers: 0

    @allan My function only receives a column object - it doesn't know anything about the parent table. But I need to access a custom API that's attached to the table, like Api.register("table().customMethod()"). I assumed I could get the table context from the column to reach this API.

    I came up with the only workaround:
    const table = column.column(column.index()).table();

    The custom api is related to a table, not to a column. Should I register the same method for the column anyway instead?

  • allanallan Posts: 64,749Questions: 1Answers: 10,713 Site admin

    If the custom API doesn't need any column information, what is the problem with the context changing to the table?

    with that API method you would be able to do api.column(1).table().customMethod(), but the column information is discarded as soon as you call table().

    Allan

Sign In or Register to comment.