1

I am using JQUERY datatables to enhance my standard HTML table on the web page. Currently I have checkbox that users can select up to 5 items on the table. I have the below code which allows users to select on any row which then highlights a leaflet JS map (this bit works completely fine).

However, what I am trying to do is not allow users to click on every row but only the 5 selected rows. The issue now is table.rows.length is 2000 rather than 5 as I need to somehow change this to only look for the selected rows.

The below code currently works for every row in the table labelled ID: tabledt. How can I change my code below to only allow this functionality on the 5 selected rows rather than every row they click on?

A class 'selected' gets added to the attribute which gets selected.

    var prevHighlightMarker = undefined;
var table = document.getElementById("tabledt");
if (table) {
    for (var i = 0; i < table.rows.length; i++) {
        table.rows[i].onclick = function () {
            highlightMarker(prevHighlightMarker, false);
            tableText(this);
            highlightMarker(cMarkers[$(this).data('id')], true);
        };
    }
}

The below code is how the data table gets defined and how I limit to only 5 rows:

    var table = $('#tabledt').DataTable({
    columnDefs: [{
        orderable: false,
        className: 'select-checkbox',
        targets: [0]
    }],
    select: {
        style: 'os',
        selector: 'td:first-child'
        
    },
    order: [[1, 'asc']]
});

table.on('select', function (e, dt, type, ix) {
    var selected = dt.rows({ selected: true });


    if (selected.count() > 5) {
        dt.rows(ix).deselect();
    }

    //var rowData = dt.rows({ selected: true }).data().toArray();

});

1 Answer 1

1

You can do something like this to check if the item contains the class:

var table = document.getElementById("tabledt");
if (table) {
    for (var i = 0; i < table.rows.length; i++) {
        table.rows[i].onclick = function () {
            if (this.classList.contains('selected')) {
                highlightMarker(prevHighlightMarker, false);
                tableText(this);
                highlightMarker(cMarkers[$(this).data('id')], true);
            }
        };
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.