Try ERPNext Buy Support Partners Foundation

Show or hide column base on condition

Hi I’m trying to hide a certain column base on a condition. I have tried what I saw on the old thread Show or hide columns child table but doesnt work for me.

My code:

frappe.ui.form.on("Purchase Invoice", "onload", function(frm) {
	console.log('>>> ONLOAD <<<');

var df = frappe.meta.get_docfield("Purchase Invoice Item","sales_order", cur_frm.doc.name);
    	df.in_list_view = 0;
		

    	cur_frm.fields_dict.items.grid.set_editable_grid_column_disp("sales_order", false);
    	cur_frm.fields_dict.items.grid.set_editable_grid_column_disp("set_column_disp", false);

    	frm.set_df_property("sales_order", "read_only", 0);
    	frm.set_df_property("sales_order", "in_list_view", 0);

});
1 Like

Hi @johnskywalker I guess that you can use this

cur_frm.fields_dict.items.grid.fields_map. sales_order.hidden = 1;
cur_frm.refresh_field(“items”);

Dear @johnskywalker

Did you achieved column hiding?

To hide grid columns using custom script, you can call this function at your desired event:

function removeColumns(frm, fields, table) {
    let grid = frm.get_field(table).grid;
    
    for (let field of fields) {
        grid.fields_map[field].hidden = 1;
    }
    
    grid.visible_columns = undefined;
    grid.setup_visible_columns();
    
    grid.header_row.wrapper.remove();
    delete grid.header_row;
    grid.make_head();
    
    for (let row of grid.grid_rows) {
        if (row.open_form_button) {
            row.open_form_button.parent().remove();
            delete row.open_form_button;
        }
        
        for (let field in row.columns) {
            if (row.columns[field] !== undefined) {
                row.columns[field].remove();
            }
        }
        delete row.columns;
        row.columns = [];
        row.render_row();
    }
    
}
2 Likes