diff --git a/frappe/model/db_query.py b/frappe/model/db_query.py index 3ef9be5b56..26075f4daa 100644 --- a/frappe/model/db_query.py +++ b/frappe/model/db_query.py @@ -578,3 +578,13 @@ def get_list(doctype, *args, **kwargs): '''wrapper for DatabaseQuery''' kwargs.pop('cmd', None) return DatabaseQuery(doctype).execute(None, *args, **kwargs) + +@frappe.whitelist() +def get_count(doctype, filters=None): + if filters: + filters = json.loads(filters) + + if isinstance(filters, list): + filters = frappe.utils.make_filter_dict(filters) + + return frappe.db.count(doctype, filters=filters) diff --git a/frappe/public/js/frappe/list/list_renderer.js b/frappe/public/js/frappe/list/list_renderer.js index 9c5e74fbab..446fadbdd7 100644 --- a/frappe/public/js/frappe/list/list_renderer.js +++ b/frappe/public/js/frappe/list/list_renderer.js @@ -355,6 +355,29 @@ frappe.views.ListRenderer = Class.extend({ this.render_tags($item_container, value); }); + this.render_count(values.length); + }, + + render_count: function(current_count) { + console.log(this) + const $header_right = this.list_view.list_header.find('.list-item__content--activity'); + + frappe.call({ + method: 'frappe.model.db_query.get_count', + args: { + doctype: this.doctype, + filters: this.list_view.get_filters_args() + } + }).then(r => { + const count = r.message; + const $html = $(`${current_count} of ${count}`); + + $html.css({ + marginRight: '10px' + }) + $header_right.addClass('text-muted'); + $header_right.html($html); + }) }, // returns html for a data item, diff --git a/frappe/utils/data.py b/frappe/utils/data.py index c1e498fc27..e0ed483fad 100644 --- a/frappe/utils/data.py +++ b/frappe/utils/data.py @@ -781,6 +781,16 @@ def make_filter_tuple(doctype, key, value): else: return [doctype, key, "=", value] +def make_filter_dict(filters): + '''convert this [[doctype, key, operator, value], ..] + to this { key: (operator, value), .. } + ''' + _filter = frappe._dict() + for f in filters: + _filter[f[1]] = (f[2], f[3]) + + return _filter + def scrub_urls(html): html = expand_relative_urls(html) # encoding should be responsibility of the composer