How can I conditionally change a field's label name.
For example:
If I am having
this.model.get('field_name_1') as check , then the field field_name_2 should be displayed as abc or else xyz.
I expect the code changes to be made at record.js file.
How can I conditionally change a field's label name.
For example:
If I am having
this.model.get('field_name_1') as check , then the field field_name_2 should be displayed as abc or else xyz.
I expect the code changes to be made at record.js file.
How would you report or filter on ABC vs. XYZ? You wouldn't be able to filter specify a filter for ABC vs. XYZ and if they were the same field and you wouldn't be able to specify one or the other in a Sugar Report.
I think this is better and more easily accomplished by having two fields ('ABC' and 'XYZ') and using Sugar Logic to make one visible or the other depending on value of 'field_name_1'.
App Ecosystem @ SugarCRM
I don't need to make report of this particular field. And what if I use a non-db field here? I got the idea of two fields and conditionally hiding one. But is there any possibilities in getting the label value to change dynamically?
You may not report on it now but your users may want that in the future. I don't think what you are trying to do will be accepted by your users in long term. Just a thought.
Field labels are stored in the 'label' property on Field objects. If you modify this and then re-render the field then it should display with your new name. However, if field controller is destroyed and a new one is created from metadata then it will revert to old name. To prevent that, you'd have to go into Sugar metadata cached in browser and change label for this field there. But even then, if metadata changes on server then browser will download new metadata and overwrite your local changes.
So this is pretty hacky to do just from a Record view.
A more durable solution would be to create your own Sugar Field that supports 2 labels and that are both configured using metadata. Or you look at Sugar Logic as mentioned before.
App Ecosystem @ SugarCRM
Hi Surabhil,
I tried doing this using this.setLabel in record.js, but was having difficulty getting it to work (anyone feel free to speak up if you know how to get it working that way). But! I did get it working just by copying the contents of the setLabel function:
// custom/modules/Accounts/clients/base/views/record/record.js ({ extendsFrom:'RecordView', initialize:function(options){ this.plugins=_.union(this.plugins||[],['HistoricalSummary']); this._super('initialize',[options]); this.model.on('sync', this.changeLabel, this); }, changeLabel:function() { if(this.model.get('status_c') == 'New') this.$('.record-label[data-name="description"]').text('asdf'); } })
I hope that helps!
-Alan
Does this still work if fields are toggled between edit and detail modes?
App Ecosystem @ SugarCRM
It can. I modified them so that after hitting Save, it re-evaluates what the label should say:
// custom/modules/Accounts/clients/base/views/record/record.js ({ extendsFrom:'RecordView', initialize:function(options){ this.plugins=_.union(this.plugins||[],['HistoricalSummary']); this._super('initialize',[options]); this.model.on('sync', this.changeLabel, this); // Evaluate on page load this.model.on('change:this.status_c', this.changeLabel, this); // Evaluate after Save (if field changes) }, changeLabel:function() { if(this.model.get('status_c') == 'New') this.$('.record-label[data-name="description"]').text('New Description'); else this.$('.record-label[data-name="description"]').text('Description'); // Reset if status is not "New" } })
Thanks.This works for me.
Can we pass label constants here instead of text?
Yep! You can do something like this (in place of where I have "New Description"):
app.lang.get("LBL_DESCRIPTION", "Accounts")