Custom Script Help Need - Filter not working when changing the parent value

We have BranchA and BranchB.
BranchA can access Warehouse1 and Wh2.
BranchB can access Wh3 and Wh4.
So in Quotation I am adding the below script to filter warehouse. So when I choose branch (Custom Field) in the item child table it should show only Wh1 and Wh2.
And if choose BranchB item child table it should show on Wh3 and Wh4. This is working First time when i choose the branch. But when I chaning the branch the filter is not working… Where I am wrong… Pls Help.
branch: function(frm) {
if(cur_frm.doc.branch_abbr==‘BRA’ ) {
frm.fields_dict[‘items’].grid.get_field(‘warehouse’).get_query = function(doc, cdt, cdn) {
var child = locals[cdt][cdn];

    return {    
        
        filters:[
            ['warehouse_name', '=', 'wh1']
        ]
       
    };
  
}; 
    
        
    } 
else {
   
    frm.fields_dict['items'].grid.get_field('warehouse').get_query = function(doc, cdt, cdn) {
    var child = locals[cdt][cdn];

    return {    
        
        filters:[
       
            ['warehouse_name', '=', '02']
        ]
         
    };
 
};

But if click on the edit button in to take full screen there the filter is working the way i want. But in the grid list only it is not filtering…

Use Code something like this

frappe.ui.form.on("Quotation", "refresh", function(frm) {
    frm.fields_dict['items'].grid.get_field('item_code').get_query = function(doc, cdt, cdn) {
        var child = locals[cdt][cdn];
        //console.log(child);
        return {    
            filters:[
                ['Item', 'item_group', '=', child.item_groups]
            ]
        };
    };
});

Dear @mohitchechani thanks for your time and reply. I used the same script only. The difference is I am using in branch: function(frm) because i want to execute this filter when branch value change. It is working if I click on edit button and choose from the full detailed screen. but in the grid view the filter is not working…

then instead of “=” use ‘in’ and then specify your branch name under that filter.

@mohitchechani Thanks for your time again. but my problem is not the operator. I think I didnt explain my case clearly. All I want is I want to filter a linked field in child table based on a field in form. With the below code it is working if click on Addrow button.
For eg. I choose BA branch and come down and enter 1 item (here it is showing only wh1 and wh2 but i didnt choose any ) then now I again I am changing the branch to BB. Now if i go to items child table and click on warehouse still it is showing wh1 and wh2. It suppose to show wh3 and wh4. But if click on new row in child table it is showing wh3 and wh4. so It is working for the new row but not the first row. I think i am missing something…

branch: function(frm) {
   if(cur_frm.doc.branch_abbr=='BA' ) { 
         frm.fields_dict['items'].grid.get_field('warehouse').get_query = function(doc, cdt, cdn) {
        var child = locals[cdt][cdn];
            return {    
        filters:[
            ['warehouse_name', 'in', ['WH1','WH2']]
        ]
    };
};
    } //if end here
else {
    frm.fields_dict['items'].grid.get_field('warehouse').get_query = function(doc, cdt, cdn) {
    var child = locals[cdt][cdn];
    return {    
        filters:[
            ['warehouse_name', 'in',['WH3', 'WH4'] ]
                   ]
    };
};
}
}