Custom Scripts
การกรองตัวเลือกในฟิลด์ Select
การกรองตัวเลือกในฟิลด์ Select
สมมติว่าคุณมีฟิลด์แบบดรอปดาวน์สองฟิลด์ชื่อ State และ City State มีสองค่าคือ Karnataka และ Maharashtra และ City มีสี่ค่าคือ Bangalore, Mysore, Mumbai และ Pune หากคุณต้องการกรองตัวเลือกใน City ตามค่าที่เลือกใน State คุณสามารถเขียนสคริปต์กำหนดเองดังที่แสดงด้านล่าง
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"])
}
});

นี่คือตัวอย่างของสคริปต์กำหนดเองที่กรองตัวเลือกในฟิลด์ Select ภายในตารางลูก:
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
]
};
};
}
});
สถานการณ์ตัวอย่าง
สมมติว่าคุณมี DocType แม่ชื่อ Sales Order ที่มีฟิลด์ตารางลูกชื่อ items ตารางลูกมีฟิลด์ Select ชื่อ itemcode และคุณต้องการกรองตัวเลือกให้แสดงเฉพาะรายการจากหมวดหมู่เฉพาะ
นี่คือลักษณะของสคริปต์ของคุณ:
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
]
};
};
}
});