get column data

get column data

aniljanilj Posts: 6Questions: 0Answers: 0
edited February 2014 in DataTables 1.10
How do I get the column header value into the title variable?

[code]
var field = e.target.name; /* form input field name */
searchTable.columns().each(function (items) {
items.map( function(item) {

var title = searchTable.columns(item).header().to$();
});
});
[/code]

That seems to show me what the HTML th header names are for the columns not what the backend header name I define and return in JSON. Ultimately, what I am trying to do is search a table using something like this but need to determine the index to provide it (e.g. 2) so thought I would loop through all the columns:

[code]
searchTable.columns( 2 ).search( this.value ).draw();
[/code]

Thanks

Replies

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin
    Your code on line 5 there gives you a jQuery object with the header cell in the result set. So if you want the text that is in the cell, you can use jQuery's `text()` method.

    Btw - in 1.10.0-beta.2 which I'm going to publish very shortly (and this is in the dev version already) I've added an `eq()` method so you don't need to use that inner map. For example:

    [code]
    var field = e.target.name; /* form input field name */
    searchTable.columns().eq(0).each(function ( colIdx ) {
    var title = searchTable.columns( colIdx ).header().to$().text();
    // ...
    });
    [/code]

    Allan
  • aniljanilj Posts: 6Questions: 0Answers: 0
    [code]
    function getTableColumnIdx (table) {
    table.columns().eq(0).each(function (colIdx) {
    var title = table.columns( colIdx ).header().to$().text();
    logHandler("Column idx: " + colIdx + " name: " + title);
    if (title == "From" || title == "To") {
    return table.column( colIdx ).index( 'visible' );
    }
    });
    }
    [/code]

    Why doesn't this function return when the match is found? I must be missing something obvious. I end up getting an output like this:

    [quote]
    [Log] Column idx: 0 name: id (app.js, line 4)
    [Log] Column idx: 1 name: Price (app.js, line 4)
    [Log] Column idx: 2 name: From (app.js, line 4)
    [Log] Column idx: 3 name: To (app.js, line 4)
    [Log] Column idx: 4 name: Description (app.js, line 4)
    [Log] Column idx: 5 name: Date Posted (app.js, line 4)
    [/quote]

    I don't suppose there is a helper function in DT where I can give it a column name and it gives me the index of its position in the table?
  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin
    edited February 2014
    You are returning from the inner function - in the `each` . The 'outer' function is returning undefined.

    > I don't suppose there is a helper function in DT where I can give it a column name and it gives me the index of its position in the table?

    Sure :-):

    [code]
    table.column( ':contains("My Column Title")' ).index();
    [/code]

    The column selector can be a jQuery selector which is performed on the cells in the header: http://next.datatables.net/reference/type/column-selector

    Allan
This discussion has been closed.