Pre-filter item on Child Table

Hello everyone,

I am working on a custom app in ERPNext and I need to pre-filter the items I add to a child table. I have a Bird doctype that contains information about a specific bird. I also have another doctype used for the weighing information of the bird, which includes a child table. When I select a bird on a line in that table, I don’t want to see the birds that are already listed in the subsequent lines.

I have worked on a Client Script, but it doesn’t fully meet my needs. Currently, the script prevents me from saving the document if a bird is present more than once, which is good. However, the filter doesn’t work as expected. I still see all the birds in the dropdown, and I can add the same bird multiple times (without saving).

There is the script:

frappe.ui.form.on(‘Chicken Weighing’, {
refresh(frm) {
frm.fields_dict[‘chicken_weighing_criteria’].grid.get_field(‘bird’).get_query = function(doc, cdt, cdn) {
let existing_birds = frm.doc.chicken_weighing_criteria.map(d => d.bird);
return {
filters: [
[“Bird”, “name”, “not in”, existing_birds]
]
};
};
},
onload(frm) {
frm.trigger(‘refresh’);
},
chicken_weighing_criteria_add(frm, cdt, cdn) {
let row = locals[cdt][cdn];
let existing_birds = frm.doc.chicken_weighing_criteria.map(d => d.bird);
if (existing_birds.includes(row.bird)) {
frappe.msgprint((‘Cet oiseau est déjà présent dans le tableau.’));
frappe.model.delete_doc(cdt, cdn);
} else {
frm.trigger(‘refresh’);
}
},
validate(frm) {
let birds = frm.doc.chicken_weighing_criteria.map(d => d.bird);
let unique_birds = new Set(birds);
if (unique_birds.size !== birds.length) {
frappe.msgprint(
(‘Vous ne pouvez pas ajouter le même oiseau plusieurs fois.’));
frappe.validated = false;
}
}
});

Thank you for your help!