Can I check # of data rows from within column.render?

Can I check # of data rows from within column.render?

jrichviewjrichview Posts: 36Questions: 7Answers: 0

I'm trying to render a delete button column, but render it disabled if there is currently only one row in the table. The body of my render function is like this:

                                var disabled = opts.allowDeleteAll === false  && _tbl.DataTable().data().count() < 2;
                                var cls = "btn btn-sm grid-btn" + (disabled === true ? " btn-dark" : " btn-danger");
                                var disattr = (disabled === true ? 'disabled' : '');
                                return '<button type="button" class="'  + cls  + '" onclick="$.fn.ParticipantWiz(\'delParty\','
                                    + meta.row.toString() + ',' + data.toString() + ')" '
                                    + disattr + '> <i class="fas fa-trash-alt"></i> Delete</button > ';

I have two rows, but on the first render call the value of data().count() is 1. So I get one button disabled and the other enabled.

I would imagine there's internal code that binds data a row at a time and that's probably what is biting me. I can't just base the render on the data passed in the options because the user can add and delete.

Any recommendations how best to implement this?

This question has an accepted answers - jump to answer

Answers

  • jrichviewjrichview Posts: 36Questions: 7Answers: 0

    forgot to mention but .rows() seems to have same problem as .data()

  • kthorngrenkthorngren Posts: 13,779Questions: 25Answers: 3,242
    Answer ✓

    My suggestion would be to use rowCallback. The problem with render is that the total number of rows won't be known, via the API's, until the last row. Only those rows on the active page will be processed. But you can get an instance of the API to check for the number of rows. Also I think the dataIndex parameter is the same as meta.row.

    Using data().count() will give you the number of cells (rows * columns). You can either use rows().count() or the page.info() API. Here is an example:
    http://live.datatables.net/dunayeja/1/edit

    Kevin

  • jgnomeozjgnomeoz Posts: 1Questions: 0Answers: 0

    I ended up here looking for my solution to find length from all rows to define which user is rendering this DataTable (If admin will render a lot more rows),
    so I accidentally came up with this meta.settings.json.data that contains all rows in data you might be able to access whole response data and compare to your selected row.
    here's my code displaying id instead of row #

    columnDefs: [{
    targets: 0,
    render: function render(data, meta) {
    if(meta.settings.json.data.length > 100)
    return data;
    else
    return meta.row + meta.settings._iDisplayStart + 1;
    }
    }]

    I hope someone find this useful.

Sign In or Register to comment.