From c6a080c252ae0a42d543046f1cb0878e1fb6813d Mon Sep 17 00:00:00 2001 From: Tom-Finke Date: Mon, 10 Oct 2022 13:21:18 +0200 Subject: [PATCH] feat: support list view for "show titles instead of name" (#18060) * initial commit * fix: account for set_fields not being called * fix: two links with title field with the same name: add prefix * chore: add semicolons * fix: only fetch for title field value for link title doctypes * fix: linting errors * style: formatting [skip ci] Co-authored-by: Raymond Reggers Co-authored-by: Saqib Ansari (cherry picked from commit 42f6aa664d5efccf78524cd3500c244772e73b4e) --- frappe/public/js/frappe/list/base_list.js | 6 +-- frappe/public/js/frappe/list/list_view.js | 54 ++++++++++++++++++++--- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/frappe/public/js/frappe/list/base_list.js b/frappe/public/js/frappe/list/base_list.js index 307971719845..2bf738d28bd9 100644 --- a/frappe/public/js/frappe/list/base_list.js +++ b/frappe/public/js/frappe/list/base_list.js @@ -77,12 +77,12 @@ frappe.views.BaseList = class BaseList { .then((doc) => (this.list_view_settings = doc.message || {})); } - setup_fields() { - this.set_fields(); + async setup_fields() { + await this.set_fields(); this.build_fields(); } - set_fields() { + async set_fields() { let fields = [].concat(frappe.model.std_fields_list, this.meta.title_field); fields.forEach((f) => this._add_field(f)); diff --git a/frappe/public/js/frappe/list/list_view.js b/frappe/public/js/frappe/list/list_view.js index 0064f725a67e..dd7c9c05dec2 100644 --- a/frappe/public/js/frappe/list/list_view.js +++ b/frappe/public/js/frappe/list/list_view.js @@ -170,7 +170,18 @@ frappe.views.ListView = class ListView extends frappe.views.BaseList { ); } - set_fields() { + get_fields() { + return super + .get_fields() + .concat( + Object.entries(this.link_field_title_fields || {}).map( + (entry) => entry.join(".") + " as " + entry.join("_") + ) + ); + } + + async set_fields() { + this.link_field_title_fields = {}; let fields = [].concat( frappe.model.std_fields_list, this.get_fields_in_list_view(), @@ -183,7 +194,34 @@ frappe.views.ListView = class ListView extends frappe.views.BaseList { "color" ); - fields.forEach((f) => this._add_field(f)); + await Promise.all( + fields.map((f) => { + return new Promise((resolve) => { + const df = + typeof f === "string" ? frappe.meta.get_docfield(this.doctype, f) : f; + if ( + df && + df.fieldtype == "Link" && + frappe.boot.link_title_doctypes.includes(df.options) + ) { + frappe.model.with_doctype(df.options, () => { + const meta = frappe.get_meta(df.options); + if (meta.show_title_field_in_link) { + this.link_field_title_fields[ + typeof f === "string" ? f : f.fieldname + ] = meta.title_field; + } + + this._add_field(f); + resolve(); + }); + } else { + this._add_field(f); + resolve(); + } + }); + }) + ); this.fields.forEach((f) => { const df = frappe.meta.get_docfield(f[1], f[0]); @@ -691,8 +729,11 @@ frappe.views.ListView = class ListView extends frappe.views.BaseList { const df = col.df || {}; const label = df.label; const fieldname = df.fieldname; + const link_title_fieldname = this.link_field_title_fields[fieldname]; const value = doc[fieldname] || ""; - + const value_display = link_title_fieldname + ? doc[fieldname + "_" + link_title_fieldname] || value + : value; const format = () => { if (df.fieldtype === "Code") { return value; @@ -716,9 +757,12 @@ frappe.views.ListView = class ListView extends frappe.views.BaseList { (df.fetch_from && ["Text", "Small Text"].includes(df.fieldtype)); if (strip_html_required) { - _value = strip_html(value); + _value = strip_html(value_display); } else { - _value = typeof value === "string" ? frappe.utils.escape_html(value) : value; + _value = + typeof value_display === "string" + ? frappe.utils.escape_html(value_display) + : value_display; } if (df.fieldtype === "Rating") {