sorting on column basis

sorting on column basis

arvindg82arvindg82 Posts: 21Questions: 4Answers: 0

how to sorting table on a column name by default decreasing order.

Replies

  • kthorngrenkthorngren Posts: 21,840Questions: 26Answers: 5,049

    I'm not sure what specifically your question is. The order option is used to set the initial table order. You can use the order API to change the prder programmatically. If this doesn't help please provide more details about the problem you are trying to solve.

    Kevin

  • burbur Posts: 30Questions: 2Answers: 2
    edited March 10

    I'm also not sure what you're asking exactly, maybe this helps:
    Order by column name on init:

    let table = new DataTable('#tableId', function() {
        // other options
        order: [{ name: 'column_name', dir: 'desc' }],
    });
    

    Order by column name using order API:

    table.order([{ name: 'column_name', dir: 'desc' }]);
    

    Note that there's currently a small bug with ordering by name using the API. For now you can work around it like this:

    table.order([[ table.column('column_name:name').index(), 'desc' ]]);
    
  • arvindg82arvindg82 Posts: 21Questions: 4Answers: 0
    edited March 10

    But column name is coming dynamically from multiple accounts in same table. Column name is fixed for each account.
    So, how can we use condition?

  • kthorngrenkthorngren Posts: 21,840Questions: 26Answers: 5,049

    The name value passed is a string. You can set a varaible before initializing Datatabes and use that instead of a literal string, for example:

    lat myColumnName = 'column_name';
    
    let table = new DataTable('#tableId', function() {
        // other options
        order: [{ name:MyColumnName, dir: 'desc' }],
    });
    

    Kevin

  • burbur Posts: 30Questions: 2Answers: 2
    edited March 10

    I don't really understand what you mean. Can you provide a test case?

    Potentially you could do something like:

    let table = new DataTable('#tableId', function() {
        // other options
        order: [{
            name: condition1 ? 'column_name1'
                    : condition2 ? 'column_name2'
                    : 'default_column_name',
            dir: 'desc'
        }],
    });
    

    Or if you can't define your condition until the table is initialized you could do:

    let table = new DataTable('#tableId', function() {
        // options
    });
    
    $(document).ready(function() {
        let column_name = 'default_column_name';
    
        if (condition1) column_name = 'column_name1';
        else if (condition2) column_name = 'column_name2';
    
        table.order([{ name: column_name, dir: 'desc' }]);
    });
    
Sign In or Register to comment.