I needed to programmatically find a row in a datatable, split the table into a page for the corresponding row, and expand that row. I know that some people have created custom libraries to handle it, but I would expect such basic functionality out of the box. Anyway, this is what I did. This is not ideal, but it can help.
- Using jquery to get a table object
- Get the nodes of the table (rows) and the number of nodes; this is important and necessary, because the table object has only those rows that you see on the current page.
- Since the page size of the table can be changed by the user, and there is no function to retrieve the page, I saved it in local storage so that I could get it. I know that I can get something like var pageSize = oTable.fnSettings () ._ iDisplayLength; But I prefer not to refer to the internal implementation of the object.
- Find the column number you are looking for
- Find row # for the row that has the corresponding column (first) in this case.
- Calculate page #
- Go to the page and use the timeout to load the page
- Use jquery to trigger any action that you want to invoke on the corresponding line (e.g. expand).
Here is a sample code:
let oTable = $('table#transactions').dataTable(), nodes = oTable.fnGetNodes(), totalRows = oTable.fnGetData().length, pageSize = vmTable.pageLength, idColumn = Object.keys(vmTable.columns).indexOf('column_name_to_be_matched'), matchedPage = 0; for (var i=0; i<totalRows; i++){ if (searchValue) === parseInt(nodes[i].cells[idColumn].innerHTML)) { matchedPage = Math.floor(i/pageSize); break; } } if (matchedPage > 0) oTable.fnPageChange(matchedPage); $timeout(function() { $(`table#transactions ul[aria-labelledby="dd${searchValue}"] span.expand`).parent().trigger('click'); }, 800);
source share