Columns need to be referred to by an alias

Columns need to be referred to by an alias

princeofnaxosprinceofnaxos Posts: 26Questions: 9Answers: 0

In options like columDefs, the targets contains the column number. This is very unhandy when inserting a column. It means that we then have to change all the targets. A lot of work, especially when there are many columns.

Why not give the columns an alias so that we can refer to them like this: "targets: ['id', 'name']". Or even the name used in columns.data could be used to refer to. This way, we can insert or remove columns wherever we want without the hassle of changing the targets all the time.

Replies

  • mainternetmainternet Posts: 2Questions: 0Answers: 0
    edited February 2017

    Here, here! - surely we can refer to columns by their name...?

  • mainternetmainternet Posts: 2Questions: 0Answers: 0
    edited February 2017

    Found out how :-

    As part of a columns[...] definition add any required field aliases - like so :-

    var table = $("#datagrid").DataTable({
    
    columns[
          {
            data: "<field name>",
            name: "<column name>",
            orderable: true,
    ...
          },
    ...
    ] 
    
    }
    
    table.column("<column name>:name").visible(false);
    
  • princeofnaxosprinceofnaxos Posts: 26Questions: 9Answers: 0

    @mainternet Your solution is nice, but doesn't work for the options. E.g. order: [ [0,'desc'] ] cannot be solved this way.

  • rf1234rf1234 Posts: 2,982Questions: 87Answers: 421

    You can assign an id to your column in HTML. Save the index in a variable and use that. If you hide columns later on you need to save the index prior to hiding them.
    Here is an example:

    var ixUpdateTime = $('#updateTime').index();
    

    and later on in your data table definition:

    // sequence: update time desc, 
        order: [[ixUpdateTime, 'desc']],
    
  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Yes, not being able to use the defined column name in the order configuration is a known limitation and something I want to address in future. At the moment you would need to use the column() selector to get the column by the given name and then use column().index() to get the index.

    Allan

This discussion has been closed.