select_fields()
Method Documentation
Purpose
This method automatically checks specific fields in a MultiCheck
dialog when the user clicks “Export”. It selects:
- All mandatory fields (those marked
reqd: 1
in the doctype or child tables), - Plus any additional explicitly specified fields (e.g.,
"lead"
).
Function Logic
select_fields() {
let mandatory_table_fields = frappe.meta
.get_table_fields(this.doctype)
.filter((df) => df.reqd)
.map((df) => df.fieldname);
// Add main doctype to list (used in field filtering)
mandatory_table_fields.push(this.doctype);
console.log("this.doctype", this.doctype);
// Manually include additional fields (not necessarily mandatory)
const always_include = ["lead"];
let multicheck_fields = this.dialog.fields
.filter((df) => df.fieldtype === "MultiCheck")
.map((df) => df.fieldname)
.filter((doctype) => mandatory_table_fields.includes(doctype));
let checkboxes = [].concat(
...multicheck_fields.map((fieldname) => {
let field = this.dialog.get_field(fieldname);
console.log("field--", field);
// Select checkboxes where option is either 'danger' or listed in always_include
return field.options
.filter((option) => option.danger || always_include.includes(option.value))
.map((option) => option.$checkbox.find("input").get(0));
})
);
// Uncheck all options first
this.unselect_all();
// Programmatically check selected options
$(checkboxes).prop("checked", true).trigger("change");
}
Breakdown
Step | Description | ||
---|---|---|---|
get_table_fields() |
Fetches all child table fields of the current doctype . |
||
filter(df => df.reqd) |
Filters only those with reqd: true . |
||
mandatory_table_fields.push(this.doctype) |
Adds the parent doctype field name to ensure main field is included. | ||
always_include = ["lead"] |
Explicitly define additional fields to always tick. | ||
this.dialog.fields |
Access all dialog fields. | ||
MultiCheck fields only |
Filters for MultiCheck field types to target field groups. |
||
`option.danger | always_include.includes(option.value)` | Final condition to check a field. | |
this.unselect_all() |
Ensures all previous selections are cleared. | ||
$(checkboxes).prop("checked", true).trigger("change") |
Ticks the required checkboxes and triggers change event for UI update. |
Output Example
When clicking Export, the following will happen:
- All mandatory fields from the table are auto-checked.
- The
"lead"
field will be selected even if it’s not mandatory or dangerous. - Fields are logged in the console for debug.
How to Extend
To include more fields:
const always_include = ["lead", "donor_type", "email"];
To skip danger
fields and include only explicit ones:
.filter((option) => always_include.includes(option.value))