How to get selected rows in Datatable

Is there any way to get selected rows in Datatable?

Used checkboxColumn: true mentioned in below link
https://github.com/frappe/datatable/issues/59

But how to get row Ids, that is selected?

What is the Event to capture selected rows?

Thanks in Advance…

@hetal1110 have you achieved this?

Try below :
let selected_rows = cur_frm.doc["item"].grid.get_selected() ;

@sanjay thanks for your reply,

are you talking about query/ script report view or child table view?

This is what i’ve tried.

frappe.query_reports["Testing"] = {
    onload: function(report) {
	    report.page.add_inner_button(__("Test"), function() {
			let selected_rows = cur_frm.doc["item"].grid.get_selected();
                console.log(selected_rows);
	    });
    },

    get_datatable_options(options) {
        return Object.assign(options, {
            checkboxColumn: true
        });
    }
};

but getting below error:

VM1924:5 Uncaught TypeError: Cannot read property 'doc' of null
    at <anonymous>:5:32
    at HTMLButtonElement.i (page.js:452)
    at HTMLButtonElement.dispatch (jquery.min.js:3)
    at HTMLButtonElement.r.handle (jquery.min.js:3)

For report having checkbox, you can get selected row as mentioned below:

  	frappe.query_report.page.add_inner_button(__("Test"), function() {
  		var selected_rows = [];
  		$('.dt-scrollable').find(":input[type=checkbox]").each((idx, row) => {
  			if(row.checked){
  				selected_rows.push(frappe.query_report.data[idx]);
  			}
  		});
  	});

Hi. Thanks for your solution, it helped me, but it is not right. After scrolling, indexes are different, you need to do following…

$('.dt-scrollable').find(":input[type=checkbox]").each((idx, row) => {
	if(row.checked){
         selected_rows.push(frappe.query_report.data[$(row.closest(".dt-cell")).data("row-index")]);
	}
});

Thank you to rectify the issue.

But, this is still not good solution, check thread:

So what result are you expecting ?

When you have table with 100 rows, you will select all of them, you will get only 25 rows which are rendered actually. I need all of selected rows.

where i to put this code i also need the child table selected row

this code mention i have to mention in child table or parent function can you plz help me

var selectedItems = frm.doc.child_table_name.filter(item => item.__checked);
selectedItems.map(row => ({//your code})

1 Like