Set a (filtered) column to a value - best method?

Set a (filtered) column to a value - best method?

vogomatixvogomatix Posts: 38Questions: 3Answers: 7
edited July 2014 in DataTables 1.10

Having applied a filter to a table, what is the best way to set all filtered elements of a column of that table to a value?

I currently have:

    /* set value in price overrides */
    var cell, i;
    var cells = api.cells( undefined, api.columns('.columnClass'), {search: 'applied'}).flatten();
    for (i = 0; i < cells.length; i++) {
       cell = cells[i];
       api.cell(cell).data(newValue);
    }

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    Looks good to me! You could use each() rather than a for loop, but the principle is the same:

    api.cells( undefined, api.columns('.columnClass'), {search: 'applied'}).eq(0).each( function ( cell ) {
      api.cell( cell ).data( newValue );
    } );
    

    Allan

  • vogomatixvogomatix Posts: 38Questions: 3Answers: 7
    edited July 2014

    @allan I think there is a problem with setting cells on row 0 in 1.10.0

    The following code does not realise it is a cell index if the cell is in row 0...
    The result appears to be that it sets cell(0,0) instead of cell(0,x)

        _api_register( 'cells()', function ( rowSelector, columnSelector, opts ) {
            // Argument shifting
            if ( $.isPlainObject( rowSelector ) ) {
                // If passing in a cell index
                if ( rowSelector.row ) {
                    opts = columnSelector;
                    columnSelector = null;
                }
                else {
                    opts = rowSelector;
                    rowSelector = null;
                }
            }
    
  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    Answer ✓

    There is yes. That has been fixed in 1.10.1 which has just been released :-)

    Allan

  • vogomatixvogomatix Posts: 38Questions: 3Answers: 7

    @allen Thankyou (I also noticed this change after I wrote the above - regrettably cannot upgrade at the moment, have solved problem by direct use of cell row and column values.

  • garraethgarraeth Posts: 2Questions: 0Answers: 0
    edited July 2014

    @allen,

    I'm also noticing some strangeness. But I am new to datatables so I may be misunderstanding.

    I'm trying to update a single column but instead of updating the column I chose, it updates columns 0 and 1.

    I've tried these three different ways to select the column:

    var column = $('#row_5 td:nth-of-type(8)');
    var cell = table.cell('#row_5', column).data( "test" );

    var cell = table.cell('#row_5', 'status:name').data( "test" );

    var cell = table.cell('#row_5', 8).data( "test" );

    edit: I'm on 1.10.1

  • vogomatixvogomatix Posts: 38Questions: 3Answers: 7
    edited July 2014

    To get round the row 0 bug I altered my code to do:

    /* set value in price overrides */
    var cell, i;
    var cells = api.cells( undefined, 
                       api.columns('.columnClass'), 
                      {search: 'applied'}).flatten();
    for (i = 0; i < cells.length; i++) {
       cell = cells[i];
       api.cell(cell.row, cell.column).data(newValue);
    }
    
    

    Using x,y coordinates instead of cell objects does not appear to be affected by the bug

  • garraethgarraeth Posts: 2Questions: 0Answers: 0

    @vogomatix,

    Thanks a ton for the reply. Knowing what works helped me narrow down what my issue was.

    For anyone else, my problem was I didn't understand that when you do cell.data(newValue), DataTables runs through your columnDefs...specifically any render calls within it that you might have.

    So I was trying to toggle an on/off button image by actually changing the text to <img src='on.jpg'> or <img src='off.jpg'> (but the data values were 0 or 1). eg: cell.data("<img src='on.jpg'>").

    And in my render was an if/then saying if the data was 0, show "on.jpg" and 1, show "off.jpg". So to fix it, rather than do cell.data("on.jpg"), I had to do cell.data(1) and let my render do it's if/then.

    (I shortened cell.data but it's the full call w/ row/col/etc.)

    The table.cell('#row_'+iID, 'status:name') (where "status" is the name value I gave it within my columnDefs) notation didn't work at all and I had to use @vogomatix's method of just using a number for the column (which isn't actually the column, but rather the index into the row param within my render within my columnDefs).

    I'm still learning, so this might be a really bad way of doing it. But it works for me.

This discussion has been closed.