Filter list view using callback data

Hi ,
I need to filter my listview onload.
The filter by value is taken from another doctype using get_value methode.

But it the value comes as a promise , and I cannot set it to the filter route options.

I will post my code below…

frappe.listview_settings['Courses'] = {
	add_fields: [],
	onload: function(listview) {
		frappe.route_options = {
			"academic_year": getCurrentAcid()
		};      
	}
};
function getCurrentAcid()
	{
		frappe.call({                        
				method: "frappe.client.get_value", 
				args: { 
					    doctype: "Academic Year",
					    fieldname: "academic_year",
						filters: { status: 1 },
					  },
				 callback: function(r) {
					    return r.message.academic_year;
				 }
		 });
	}

here academic_year gets value after callback which takes time and it doesn’t work.

Any idea how I can make it right?

Or can I do it using python ?

Thanks,
vivek

Hi all,

Someone please do help me with this.

@ninjas005

In async programming, you don’t return values, you set them or pass a callback function.

In your case, you should set frappe.route_options in the callback and not in the onload function.

Hi @anand ,

I forgot to mention , I’ve also tried

frappe.listview_settings['Courses'] = {
	add_fields: [],
	onload: function(listview) {
		frappe.call({                        
				method: "frappe.client.get_value", 
				args: { 
					    doctype: "Academic Year",
					    fieldname: "academic_year",
						filters: { status: 1 },
					  },
				 callback: function(r) {
					frappe.route_options = {
						"academic_year": r.message.academic_year
					};    
						
				 }
		 });
	}
};  

This also doesn’t work.

Filter is not shown when I try like this.

Can you explain with screenshot what you are trying to do?

Hi @anand ,

I’ve solved the issue.
I just had to refresh the listview using " listview.refresh( );"
inside the callback.

My problem was, filter got applied in the listview after page was loaded.
so i needed to refresh the page to see the filtered list.

Thanks

2 Likes

Hi @ninjas005,

So which one of your codes that you solved with `listview.refresh();" ?