简体   繁体   中英

datatable data of selected row with column

i'm trying to get specific column of my datatable refered by name of the selected row but it select either the row or all data of the specific column never the specific column value of selected row :

 var columnValue1 = getTable().column('ALEN:name').rows( { selected: true } ).data();
    var columnValue2 = getTable().rows( { selected: true } ).column('ALEN:name').data();

OUTPUT:

Columnvalue1 : output the full selected row not only the column 'ALen' of the selected row Columnvalue2 : output the data of all lines of the column 'Alen' not only the selected row

As says in a comment, this depends on DataTable initialization (ajax or precharged data) and if you have single or multiple selection...

MULTIPLE SELECTION EXAMPLE:

If you have datatable with ajax you can access field directly:

var rowsData = getTable().rows({ selected: true }).data().filter(x => x.FieldName <= 0).toArray()

If you don't have ajax and you have precharged data:

var rowsData = getTable().rows({ selected: true }).data().filter(x => x[15] <= 0).toArray()

And finally you can iterate over rowsData because is an array of objects (rows) with fields.

SINGLE SELECTION EXAMPLE:

If you have datatable with ajax you can access field directly:

var fieldValue = getTable().row({ selected: true }).data().FieldName

If you don't have ajax and you have precharged data:

var fieldValue= getTable().row({ selected: true }).data()[15]

i created a function that index on the name then determine what is his position in the selected row for exemple i want to return the value of the row selected with name Alen :

var columnValue2 = getTable().rows( { selected: true } ).data()[0][tab.getColumnIndex("Alen")];

This is my function:

this.getColumnIndex = function(ColumnName){
        var index;
        //dataTableOption : function to declare option of datatable like columns-searching 
        for (let i=0;i<this.dataTableOption.columns.length; i++ ){
            
            if ( this.dataTableOption.columns[i].name == ColumnName){
                
                 index =i;
        }
        }
        return index ;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM