How do I get column data from a rows() object?

How do I get column data from a rows() object?

jgoldmanjgoldman Posts: 4Questions: 1Answers: 0

I need to be able to search a DataTables object from "outside" the table. My actual application is very complicated with several different tables, but I've created a small fiddle which shows the basic situation.

My basic problem is that while I can use the rows() selector to get a specific row selected, I can't then get specific columns from that row. I've tried chaining the column function, but that seems to give me just the rows in the base table, not the selected one.

Hopefully the fiddle below ( https://jsfiddle.net/6ad7gcgt/19/ if the embed doesn't work) will demonstrate what I'm trying to do if this wasn't clear.

Thank you for your help!

This question has an accepted answers - jump to answer

Answers

  • jgoldmanjgoldman Posts: 4Questions: 1Answers: 0

    I've come up with a workaround for my problem, but one I don't really like -- it feels like I'm making DataTables work harder than it should.

    So assuming the dataTable variable below points to a valid DataTable object, and searchCode is the text we're searching for, after determining I have one result, I then turn around and do a search, which then lets me get the data from the proper column. (The code below probably makes more sense if you look a the fiddle above). I welcome suggestions and improvements to my code, of course!

        var names = dataTable.rows( function (idx, data, node) {
            return data[0] == searchCode ? true : false;
        });
        if (names.data().length == 1) {
            var foo = names.search(searchCode);
            alert("position: "+foo.column(2, {search:'applied'}).data()[0]);
            // other stuff here...
            dataTable.draw();
        } else {
            alert("no match");
        }
    
  • JCR1951JCR1951 Posts: 34Questions: 6Answers: 0

    I'm only an user from datatables.
    But I had search for retrieving hidden values.
    And I found a script maybe you can use to retrieve normal columns.
    The first part can be needed for problems with pagination.

    https://datatables.net/forums/discussion/35210/jquery-doesnt-take-hidden-td#latest

  • allanallan Posts: 61,917Questions: 1Answers: 10,151 Site admin
    Answer ✓

    I'd suggest using the cell().data() method - that gives you the data for an individual cell (i.e. the cross section between a row and a column). You could use something like:

    dataTable.cell( idx, 2 ).data();
    

    i.e. get the data for row idx and column index 2.

    Allan

  • jgoldmanjgoldman Posts: 4Questions: 1Answers: 0

    Thanks, that really helped! (In my case, since I had a rows() object, but I know there is just one match, I get idx (the index) from names.indexes()[0] . Then I can get the value as you say above.)

This discussion has been closed.