How to apply filter for listview of doctype

frappe.listview_settings[‘Purchase Invoice’] = {
onload: function(listview) {
frappe.call({
method: “demo_app.public.python.listview_all.user_filter”,
callback: function(r) {
frappe.route_options = {“supplier”: [“=”, “SUP-2023-00116”]}
}
});
},
};

Hi @RK_1214,

Please check its syntax.

frappe.listview_settings['Purchase Invoice'] = {
    onload : function(listview) {
        frappe.route_options = {
            "supplier": "Supplier 1"
          };
        listview.refresh();
    }
};

Output:
When reloading the doctype then automatically filter will set.

Please set it according to scenario.

Thank You!

1 Like

Thank you for reply,

But frappe.route_options not work in frappe.call()

please add this after your code.

listview.refresh();

then check it.

frappe.listview_settings[‘Purchase Invoice’] = {
onload: function(listview) {
frappe.call({
method: “demo_app.public.python.listview_all.user_filter”,
callback: function(r) {
frappe.route_options = {“supplier”: [“=”, “SUP-2023-00116”]}
listview.refresh();
}
});
},
};

Not working

frappe.call not work in frappe.listview_settings ,
for that use like this

let user_l = [];
frappe.call({
    method: "demo_app.public.python.listview_all.user_filter",
    args: { doctype: doc },
}).then(function(r) {
    user_l.push(r.message);
});

frappe.listview_settings['Purchase Invoice'] = {
    onload: function(r) {
        frappe.route_options = []
        frappe.route_options = user_l[0]
    }
};
1 Like

Hi bro could u pls help as I am performing filter based on the role selected by user in the dialog box below I have shared the code I followed the same function as u mentioned but it is not working

frappe.listview_settings[“Proposed Branch Details”] = {
onload: function (listview) {
if (
frappe.user.has_role(“System Manager”) ||
frappe.user.has_role(“IT Branch Admin”)
) {
return;
}

let roles = getUserRolesAsOptions(sample);

if (roles.length > 1) {
console.log(“Multi Role”);
promptForRoleSelection(listview);
} else if (roles.length === 1) {
console.log(“Single Role”);
handleSelectedOption(listview, roles[0]);
}
},
};

function promptForRoleSelection(listview) {
let sample = [
“Branch Initiator”,
“Branch Responsible Person”,
“Template Verifier”,
“Approval Manager 1”,
“Approval Manager 2”,
“Approval Manager 3”,
“Approval Manager 4”,
“RB Manager”,
];
let d = new frappe.ui.Dialog({
title: “Select Your Role”,
fields: [
{
label: “Choose your Role”,
fieldname: “role”,
fieldtype: “Select”,
options: getUserRolesAsOptions(sample),
},
],
primary_action_label: “Submit”,
primary_action(values) {
let selectedRole = values.role;
handleSelectedOption(listview, selectedRole);
d.hide();
},
secondary_action_label: “Cancel”,
secondary_action() {
d.hide();
},
});

d.show();
}

function getUserRolesAsOptions(suppliedList) {
let options = ;
for (let i = 0; i < frappe.user.roles.length; i++) {
let role = frappe.user.roles[i];
if (
suppliedList.includes(role) &&
role !== “All” &&
role !== “Guest” &&
role !== “Desk User”
) {
options.push(role);
}
}
return options;
}

function handleSelectedOption(listview, option) {
// Set the route options based on the selected role
if (option === “Branch Initiator”) {
frappe.route_options = {
workflow_state: “Draft”,
initiated_by: frappe.session.user,
};
} else if (option === “Branch Responsible Person”) {
frappe.route_options = {
workflow_state: “Assigned to the Responsible Person”,
initiated_by: frappe.session.user,
};
} else if (option === “RB Manager”) {
frappe.route_options = {
workflow_state: “Approval pending with RB Manager”,
rb_manager_details: frappe.session.user,
};
} else if (option === “Approval Manager 1”) {
frappe.route_options = {
workflow_state: “Approval pending with Approval Manager 1”,
approval_manager_1: frappe.session.user,
};
} else if (option === “Approval Manager 2”) {
frappe.route_options = {
workflow_state: “Approval pending with Approval Manager 2”,
approval_manager_2: frappe.session.user,
};
} else if (option === “Approval Manager 3”) {
frappe.route_options = {
workflow_state: “Approval pending with Approval Manager 3”,
approval_manager_3: frappe.session.user,
};
} else if (option === “Approval Manager 4”) {
frappe.route_options = {
workflow_state: “Approval pending with Approval Manager 4”,
approval_manager_4: frappe.session.user,
};
} else if (option === “Template Verifier”) {
frappe.route_options = {
workflow_state: “Assigned to the Group Queue”,
};
}

// Refresh the list view to apply the filters
listview.refresh();
}