From 49ccc0ab6ebc8bc0ee01b2b5842d17583bc37149 Mon Sep 17 00:00:00 2001
From: barredterra <14891507+barredterra@users.noreply.github.com>
Date: Thu, 8 Jun 2023 18:55:53 +0200
Subject: [PATCH] feat: make option sorting configurable
---
frappe/core/doctype/docfield/docfield.json | 10 +++++++++-
.../doctype/custom_field/custom_field.json | 10 +++++++++-
.../doctype/customize_form/customize_form.py | 1 +
.../customize_form_field.json | 10 +++++++++-
frappe/public/js/frappe/form/controls/select.js | 17 ++++++++++-------
5 files changed, 38 insertions(+), 10 deletions(-)
diff --git a/frappe/core/doctype/docfield/docfield.json b/frappe/core/doctype/docfield/docfield.json
index 90b1c6cb77..71d949507b 100644
--- a/frappe/core/doctype/docfield/docfield.json
+++ b/frappe/core/doctype/docfield/docfield.json
@@ -21,6 +21,7 @@
"search_index",
"column_break_18",
"options",
+ "sort_options",
"show_dashboard",
"defaults_section",
"default",
@@ -550,13 +551,20 @@
"fieldtype": "Data",
"label": "Documentation URL",
"options": "URL"
+ },
+ {
+ "default": "0",
+ "depends_on": "eval: doc.fieldtype === 'Select'",
+ "fieldname": "sort_options",
+ "fieldtype": "Check",
+ "label": "Sort Options"
}
],
"idx": 1,
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
- "modified": "2023-02-20 12:07:29.552523",
+ "modified": "2023-06-08 18:31:50.153235",
"modified_by": "Administrator",
"module": "Core",
"name": "DocField",
diff --git a/frappe/custom/doctype/custom_field/custom_field.json b/frappe/custom/doctype/custom_field/custom_field.json
index 63be70c644..f3d99efb14 100644
--- a/frappe/custom/doctype/custom_field/custom_field.json
+++ b/frappe/custom/doctype/custom_field/custom_field.json
@@ -21,6 +21,7 @@
"hide_seconds",
"hide_days",
"options",
+ "sort_options",
"fetch_from",
"fetch_if_empty",
"options_help",
@@ -435,13 +436,20 @@
"fieldtype": "Check",
"label": "Is System Generated",
"read_only": 1
+ },
+ {
+ "default": "0",
+ "depends_on": "eval: doc.fieldtype === 'Select'",
+ "fieldname": "sort_options",
+ "fieldtype": "Check",
+ "label": "Sort Options"
}
],
"icon": "fa fa-glass",
"idx": 1,
"index_web_pages_for_search": 1,
"links": [],
- "modified": "2022-06-13 06:39:03.319667",
+ "modified": "2023-06-08 18:33:28.863144",
"modified_by": "Administrator",
"module": "Custom",
"name": "Custom Field",
diff --git a/frappe/custom/doctype/customize_form/customize_form.py b/frappe/custom/doctype/customize_form/customize_form.py
index f403079cd8..4da303a016 100644
--- a/frappe/custom/doctype/customize_form/customize_form.py
+++ b/frappe/custom/doctype/customize_form/customize_form.py
@@ -665,6 +665,7 @@ docfield_properties = {
"label": "Data",
"fieldtype": "Select",
"options": "Text",
+ "sort_options": "Check",
"fetch_from": "Small Text",
"fetch_if_empty": "Check",
"show_dashboard": "Check",
diff --git a/frappe/custom/doctype/customize_form_field/customize_form_field.json b/frappe/custom/doctype/customize_form_field/customize_form_field.json
index d8da44101b..1c89a03a37 100644
--- a/frappe/custom/doctype/customize_form_field/customize_form_field.json
+++ b/frappe/custom/doctype/customize_form_field/customize_form_field.json
@@ -29,6 +29,7 @@
"precision",
"length",
"options",
+ "sort_options",
"fetch_from",
"fetch_if_empty",
"show_dashboard",
@@ -462,13 +463,20 @@
"fieldname": "ignore_xss_filter",
"fieldtype": "Check",
"label": "Ignore XSS Filter"
+ },
+ {
+ "default": "0",
+ "depends_on": "eval: doc.fieldtype === 'Select'",
+ "fieldname": "sort_options",
+ "fieldtype": "Check",
+ "label": "Sort Options"
}
],
"idx": 1,
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
- "modified": "2023-02-20 12:07:40.242470",
+ "modified": "2023-06-08 18:36:22.833032",
"modified_by": "Administrator",
"module": "Custom",
"name": "Customize Form Field",
diff --git a/frappe/public/js/frappe/form/controls/select.js b/frappe/public/js/frappe/form/controls/select.js
index 1b91acb383..de96cd902c 100644
--- a/frappe/public/js/frappe/form/controls/select.js
+++ b/frappe/public/js/frappe/form/controls/select.js
@@ -74,7 +74,7 @@ frappe.ui.form.ControlSelect = class ControlSelect extends frappe.ui.form.Contro
if (this.$input) {
var selected = this.$input.find(":selected").val();
this.$input.empty();
- frappe.ui.form.add_options(this.$input, options || []);
+ frappe.ui.form.add_options(this.$input, options || [], this.df.sort_options);
if (value === undefined && selected) {
this.$input.val(selected);
@@ -102,15 +102,18 @@ frappe.ui.form.ControlSelect = class ControlSelect extends frappe.ui.form.Contro
}
};
-frappe.ui.form.add_options = function (input, options_list) {
+frappe.ui.form.add_options = function (input, options_list, sort) {
let $select = $(input);
if (!Array.isArray(options_list)) {
return $select;
}
- options_list
- .map((raw_option) => parse_option(raw_option))
- .sort((a, b) => a.label.localeCompare(b.label))
+ let options = options_list.map((raw_option) => parse_option(raw_option));
+ if (sort) {
+ options = options.sort((a, b) => a.label.localeCompare(b.label));
+ }
+
+ options
.map((option) =>
$("