Hey
Sorry I was bit occupied so could not the code
Actually I want to add this functionality in a button
so i tried to modified the code but seems that does not work
Code in my custom_doctype(demo2).js file frappe.ui.form.on('demo2', { refresh: function(frm) { frm.add_custom_button(__('Delete'), function() { frm.set_value('table', []); frm.refresh_field('table'); } } });
frappe.ui.form.on('demo2', {
refresh: function(frm) {
frm.add_custom_button(__('Delete'), function() {
console.log('Table - Total number of rows before delete', frm.doc.table.length);
//frm.set_value('table', []);
frappe.model.clear_table(frm.doc, 'table');
frm.refresh_field('table');
console.log('Table - Total number of rows after delete', frm.doc.table.length);
});
}
});
If the code works then:
The delete button will be created
When clicking the delete button, two messages will be posted into the console of the browser, they will state the table’s total number of rows before and after the process
The code should work unless the client script is not for the form of demo2 doctype or the child table fieldname isn’t correct.
So just to confirm writing code in client script from frontend and writing js script from backend is same right?
Also I hav uploaded my doctype screenshot for reference/confirmation above
@Harsh_Magiya The script shouldn’t have caused the form to be empty…
How about you try the following code…
frappe.ui.form.on('demo2', {
onload: function(frm) {
frm.add_custom_button(__('Delete'), function() {
console.log('Table - Total number of rows before delete', frm.doc.table.length);
frm.set_value('table', []);
if (frm.doc.table.length) {
frappe.model.clear_table(frm.doc, 'table');
}
console.log('Table - Total number of rows after delete', frm.doc.table.length);
});
}
});
Hey @kid1194
sorry while pasting the code I might have pressed a button and that made the syntax incorrect
The above code is correct
Just 2 more things
No msg came like you said
When clicking the delete button, two messages will be posted into the console of the browser, they will state the table’s total number of rows before and after the process
What if after deleting I want to run a function from .py file
how can I add that
frappe.ui.form.on('demo2', {
onload: function(frm) {
frm.add_custom_button(__('Delete'), function() {
console.log('Table - Total number of rows before delete', frm.doc.table.length);
frm.set_value('table', []);
console.log('Table - Total number of rows after delete', frm.doc.table.length);
// Here you can add anything you want to call after the table is cleared
});
}
});
The logs should be shown in the browser’s console…
But since refresh is working for you then use the following code…
frappe.ui.form.on('demo2', {
refresh: function(frm) {
if (!frm._del) {
frm._del = 1;
frm.add_custom_button(__('Delete'), function() {
console.log('Table - Total number of rows before delete', frm.doc.table.length);
frm.set_value('table', []);
frappe.model.clear_table(frm.doc, 'table');
console.log('Table - Total number of rows after delete', frm.doc.table.length);
// Here you can add anything you want to call after the table is cleared
});
}
}
});