选择字段中的筛选选项

假设你有两个下拉字段,分别名为“州”和“城市”。州有两个值:卡纳塔克邦和马哈拉施特拉邦;城市有四个值:班加罗尔、迈索尔、孟买和浦那。如果你想根据州中选择的值来过滤城市中的选项,可以编写如下所示的自定义脚本。

frappe.ui.form.on("Lead", "state", function(frm) {
  if(frm.doc.state == "Karnataka") {
    set_field_options("city", ["Bangalore","Mysore"])
  } else if(frm.doc.state == "Maharashtra") {
    set_field_options("city", ["Mumbai","Pune"])
  } else if(frm.doc.state == "") {
    set_field_options("city", ["","Bangalore","Mysore","Mumbai","Pune"])
  }
});

以下是一个自定义脚本的示例,用于过滤子表中选择字段的选项:

frappe.ui.form.on('Parent DocType', {
    refresh: function (frm) {
        // Trigger this function when the form is refreshed or reloaded
        frm.fields_dict['child_table_fieldname'].grid.get_field('select_fieldname').get_query = function(doc, cdt, cdn) {
            return {
                filters: [
                    ['Some Field', '=', 'Some Value'] // Replace with your filter criteria
                ]
            };
        };
    }
});

示例场景

假设你有一个名为销售订单的父文档类型,其中包含一个名为项目的子表字段。该子表有一个名为项目代码的选择字段,你希望过滤选项,只显示特定类别中的项目。

你的脚本可能如下所示:

frappe.ui.form.on('Sales Order', {
    refresh: function (frm) {
        frm.fields_dict['items'].grid.get_field('item_code').get_query = function(doc, cdt, cdn) {
            return {
                filters: [
                    ['Item Group', '=', 'Specific Category'] // Adjust the filter criteria as needed
                ]
            };
        };
    }
});