How do I get the name of a column from within a .on('click', '.region-column, .displayName-column',

How do I get the name of a column from within a .on('click', '.region-column, .displayName-column',

CmdrBeavisCmdrBeavis Posts: 5Questions: 2Answers: 0

I've been trying to figure this out for .. longer than I care to admit. It seems like this would be a pretty simple thing, but I'm sure having a hard time. I've got an .on('click' ...) handler that fires if either one of two different columns is clicked in. (I'm trying to do editing using contentEditable, on a div within these two columns and don't want to make two separate handlers because DRY.)

$('#grid-data tbody').on('click', '.region-column, .displayName-column', function (oEvent) {

    // experiments ...
    
    var $this = $(this);
    var $thisCell = customerRegionMappingDataTable.cell($this.parents('td'));
    // I need the name of the field getting updated! Is it in $thisCell ?
    // not that I can find.

    // this works, but still don't know how to use it.
    var cellIdx = this.cellIndex;

    // didn't work 
    //var idx = this.cellIndex;
    //var altCellIdx = customerRegionMappingDataTable.cell(this).index().column;
    //var title = customerRegionMappingDataTable.column(idx).header();
    //console.log('Column title clicked on: ' + $(title).html());

    // neither of these appear to work either 
    var columnInfo = customerRegionMappingDataTable.columns(cellIdx);

    var aRowInfo = customerRegionMappingDataTable.row($(this));

I need to know which column was clicked on/in so that when I update the value, I can persist the change back to the appropriate db field [via a $.ajax() call or whatever].

Thanks!

Answers

  • kthorngrenkthorngren Posts: 20,298Questions: 26Answers: 4,769

    Try column().header(). The example shown should give you the column name.

    Kevin

  • CmdrBeavisCmdrBeavis Posts: 5Questions: 2Answers: 0
    edited July 2017

    Hey Kevin,

    Darn, I wasn't clear, sorry about that. column().header() does in fact give me the header text, e.g. "Column title clicked on: Displayed Customer Name", but I need the actual 'name' of the column. In this case, "displayName".

    Here's my column definition;

        customerRegionMappingDataTable = $("#grid-data").DataTable({
            "processing": true,
            "lengthMenu": [10, 25, 50],
            "sPaginationType": "full_numbers",
            ...
            columns: [
                {
                    "name": "displayName",
                    data: "DisplayName",
                    title: "Displayed Customer Name",
                    ...
                }
    
    

    EDIT: Actually, now that I think about it, I should probably grab that 'data' value as well.

    Thanks,
    Scott

  • CmdrBeavisCmdrBeavis Posts: 5Questions: 2Answers: 0

    Anyone?

    Bueler?

    Bueler? ;)

  • allanallan Posts: 61,697Questions: 1Answers: 10,102 Site admin

    That method isn't actually available in DataTables 1.x. It is in my v2 branch (which is very very early stages) and I accidentally published the reference to the site. That method doesn't exist in the list of API methods, which is for the shipping version of DataTables.

    Currently in 1.x there is no API method to get the columns.name property of a column I'm afraid.

    Allan

This discussion has been closed.