How to order the documents in a DocType's Web View

Hello

I have created a Web View of the Brand DocType.

Here is my brand_row.html

<div class='py-8 row'>
	<div class='col-sm-1'>
		<img alt='{{ doc.name }}' src='{{ doc.image }}'>
	</div>
	<div class='col'>
		<a class='font-size-lg' href='{{ doc.route }}'>{{ doc.title or doc.name }}</a>
		<p class='text-muted'>By {{ doc.description }}</p>
	</div>
</div>

How can I order these Brands other than by the last date the Document was edited, which seems to be the default ? In other words I want to apply an alphabetical sort order to the Brand Name so that the brands appear in this order :
Esperssif
GigaDevice
YiLink

Thanks so much.

Further

The default sort order is based on the ‘modified’ docfield, and the sort order of the List View has no effect on the sort order of this Web View, neither has the DocType > Brand > Settings > View Settings > Default Sort Field any effect on the order.

So I presume I’d have to look into the template which creates the Web View, but I have no idea of even where to start investigating.

Hi @EugeneP:

You need to define custom get_list_context and get_list methods on your brand.py file
It will provide custom “context” for webview.

See this on project.py

Hope this helps.

Thanks so much @avc

I’ve learned a lot from you reply, much appreciated :+1:

However, even changing the order_by=“brand”, does not modify the order. I presume the order_by=“brand” is only for the query from the database, not for the order of display on the Web View ?

I attach my entire brand.py, maybe you can spot a Murphy :rofl:

import copy

import frappe
from frappe.website.website_generator import WebsiteGenerator

class Brand(WebsiteGenerator):
	pass

def get_brand_defaults(item, company):
	item = frappe.get_cached_doc("Item", item)
	if item.brand:
		brand = frappe.get_cached_doc("Brand", item.brand)

		for d in brand.brand_defaults or []:
			if d.company == company:
				row = copy.deepcopy(d.as_dict())
				row.pop("name")
				return row

	return frappe._dict()

def get_brand_list(
	doctype, txt, filters, limit_start, limit_page_length=20, order_by="brand"
):
	fields = "distinct *"
	return frappe.get_list(
		doctype,
		fields=fields,
		filters=filters,
		# or_filters=or_filters,
		limit_start=limit_start,
		limit_page_length=limit_page_length,
		order_by=order_by,
		# ignore_permissions=ignore_permissions,
	)

def get_list_context(context=None):
	from erpnext.controllers.website_list_for_contact import get_list_context
	list_context = get_list_context(context)
	list_context.update(
		{
			"show_sidebar": False,
			"show_search": False,
			"no_breadcrumbs": False,
			"title": "Brands",
			"get_list": get_brand_list,
			"row_template": "setup/doctype/brand/templates/brand_row.html",
		}
	)
	return list_context

Hi @EugeneP :

Try with this:

def get_brand_list(doctype, txt, filters, limit_start, limit_page_length=20, order_by=None):
	brand_list = frappe.get_list(doctype="Brand", fields=["name", "brand"], order_by="brand")
	return brand_list
	
	

def get_list_context(context=None):
		
	return {
		"title": "Brands",
		"get_list": get_brand_list,
		"no_breadcrumbs": False,
	}

Hope this helps :wink:

1 Like

Wonderful, thanks so much :muscle:

1 Like