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.
avc
December 2, 2023, 7:13am
3
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
frappe.qb.from_(timesheet_detail)
.select(UnixTimestamp(timesheet_detail.from_time), Count("*"))
.where(timesheet_detail.project == name)
.where(timesheet_detail.from_time > CurDate() - Interval(years=1))
.where(timesheet_detail.docstatus < 2)
.groupby(Date(timesheet_detail.from_time))
.run()
)
def get_project_list(
doctype, txt, filters, limit_start, limit_page_length=20, order_by="modified"
):
user = frappe.session.user
customers, suppliers = get_customers_suppliers("Project", frappe.session.user)
ignore_permissions = False
if is_website_user():
if not filters:
filters = []
Hope this helps.
Thanks so much @avc
I’ve learned a lot from you reply, much appreciated
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
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
avc
December 2, 2023, 1:33pm
5
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
1 Like
Wonderful, thanks so much
1 Like
@avc Hello.
What if I want to set filters in web view?
We have filters in webforms for list, but it require login.
Is there any method?