How to set restrictions in items table column name

I’d like the ‘Fat’ and ‘SNF’ names to be displayed only when I choose ‘Milk supplier1.’ If I select any other supplier, I want ‘Fat’ and ‘SNF’ to be automatically hidden.

Hi:

Check this …

Hope this helps.


thanks for your reply. Not working brother

Hi:

try with

eval: parent.supplier == "Milk supplier 2"

Anyway, I know it is working for “child form”. (“Edit” button on row). I am not sure if column show/hide behavior depends on this. Take a look here:

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();
    }
}
frappe.ui.form.on('Purchase Invoice', {
    refresh: function(frm) {
        // Get the supplier name from the Purchase Invoice
        var supplier = frm.doc.supplier;
        
        // Fields to hide in the Purchase Invoice Item table
        var fieldsToHide = ['fat', 'snf']; // Replace with the actual fieldnames
        
        // Table name in Purchase Invoice DocType
        var table = 'items'; // Replace with the actual table name
        
        if (supplier !== 'Milk supplier 1') {
            removeColumns(frm, fieldsToHide, table);
        }
    }
});

Not working brother

my version for your kind reference

	refresh: function(frm) {
		//frm.trigger('item_code');
		if (! in_list(['公斤','KG','Kg','kg'], frm.doc.stock_uom)){
			frm.events['hide_child_columns'](frm, ["gross_weight"], 'items');
		}
       },
	hide_child_columns: function(frm, fields, table_field) {
		let grid = frm.get_field(table_field).grid;
		
		for (let field of fields) {
			grid.fields_map[field].hidden = 1;
			//grid.fields_map[field].in_list_view = 0;
		}
		
		grid.visible_columns = undefined;
		frappe.model.user_settings.remove(grid.frm.doctype, 'GridView');
		grid.setup_visible_columns();
		
		grid.header_row.wrapper.remove();
		delete grid.header_row;
		grid.make_head();
		grid.reset_grid();
	},
2 Likes

That work for me.

const switch_prescription_form = function (frm) {
    const isPrescription = frm.doc.is_prescription

    const grid = frm.fields_dict['items'].grid

    // Clear Current Grid
    grid.header_row.wrapper.remove()
    grid.grid_rows.forEach(r => r.wrapper.remove())

    grid.header_row = null
    grid.data = []
    grid.grid_rows = null

    // Reset Grid List View
    const docFields = frappe.meta.get_docfields('Sales Order Item')
    // itemFields is field group 1, like ['item_code', 'qty', 'rate', 'amount', ...]
    // prescriptionFields is field group 2, like ['item_code', 'qty', 'rate', 'amount', 'a field', 'b field', ...]
    const fields = !isPrescription ? itemFields : prescriptionFields
    grid.visible_columns = docFields.filter(df => fields.includes(df.fieldname))
                                     .map(df => [df, df.columns])
                                     
    grid.refresh()
}

frappe.ui.form.on('Sales Order', {
    my_fields: (frm) => {
        switch_prescription_form(frm)
    }
})

frappe version 13.36.3
erpnext version 13.36.2

1 Like

thanks for your reply. This script is working after saving. I want instantly changes

frappe.ui.form.on('Purchase Invoice', {
    refresh: function(frm) { 
        
        if (!in_list(['Farmers'], frm.doc.supplier_group)) { 
             frm.refresh_fields();
            frm.events['hide_child_columns'](frm, ["fat", "snf"], 'items');
        }
    },
    hide_child_columns: function(frm, fields, table_field) {
		let grid = frm.get_field(table_field).grid;
		
		for (let field of fields) {
			grid.fields_map[field].hidden = 1;
			//grid.fields_map[field].in_list_view = 0;
		}
		
		grid.visible_columns = undefined;
		frappe.model.user_settings.remove(grid.frm.doctype, 'GridView');
		grid.setup_visible_columns();
		
		grid.header_row.wrapper.remove();
		delete grid.header_row;
		grid.make_head();
		grid.reset_grid();
	},
});

Thank you for getting back to me.I’ve attempted all the scripts, but regrettably, none of them seem to be functioning correctly. There is one script, however, that is only partially operational.

frappe.ui.form.on('Purchase Invoice', {
    refresh: function(frm) { 
        
        if (!in_list(['Farmers'], frm.doc.supplier_group)) { 
             frm.refresh_fields();
            frm.events['hide_child_columns'](frm, ["fat", "snf"], 'items');
        }
    },
    hide_child_columns: function(frm, fields, table_field) {
		let grid = frm.get_field(table_field).grid;
		
		for (let field of fields) {
			grid.fields_map[field].hidden = 1;
			//grid.fields_map[field].in_list_view = 0;
		}
		
		grid.visible_columns = undefined;
		frappe.model.user_settings.remove(grid.frm.doctype, 'GridView');
		grid.setup_visible_columns();
		
		grid.header_row.wrapper.remove();
		delete grid.header_row;
		grid.make_head();
		grid.reset_grid();
	},
});