Javascript sap.m.Text对象中的条件粗体

Javascript sap.m.Text对象中的条件粗体,javascript,sapui5,Javascript,Sapui5,我在控制器文件中有以下代码,用于将项绑定到表: oTable.bindAggregation("items", { path: sRelPath, template: new sap.m.ColumnListItem({ cells: [ new sap.m.Text({ text: {

我在控制器文件中有以下代码,用于将项绑定到表:

        oTable.bindAggregation("items", {
            path: sRelPath,
            template: new sap.m.ColumnListItem({
                cells: [
                    new sap.m.Text({
                        text: {
                            path: 'relationship_type',
                            formatter: this.formatter.textBold
                        },
                        id: "relTypeText"
                    }),
                    new sap.m.Text({
                        text: "{client_name}",
                        id: "relClientName"
                    })
                ]
            }),
我试图将“relationship_type”值的格式设置为粗体,如果它返回时包含特定的文本字符串。我正在使用此格式化程序:

     textBold: function(sText) {
        if (typeof sText === "string" && sText.indexOf("(Self)") > -1) {
            //return $("#relTypeText").html("<b>"+sText+"</b>");
            return sText.bold();
        }
        return sText;
     },
     textBold: function(sText) {
        if (typeof sText === "string") {
            if (sText.indexOf("(Self)") > -1) {
                this.setDesign("Bold");
            } 
        }
        return sText;
     },
<Ref. to label>.setDesign(sap.m.LabelDesign.Bold)
它在前端返回了这个:


我最初的建议是,如果允许您在格式化程序中使用Label和LabelDesign,请执行以下操作:

     textBold: function(sText) {
        if (typeof sText === "string" && sText.indexOf("(Self)") > -1) {
            //return $("#relTypeText").html("<b>"+sText+"</b>");
            return sText.bold();
        }
        return sText;
     },
     textBold: function(sText) {
        if (typeof sText === "string") {
            if (sText.indexOf("(Self)") > -1) {
                this.setDesign("Bold");
            } 
        }
        return sText;
     },
<Ref. to label>.setDesign(sap.m.LabelDesign.Bold)
.setDesign(sap.m.LabelDesign.Bold)
或者使用基于“关系类型”值的表达式绑定

备选方案:我认为问题在于您试图使用格式化程序返回HTML代码。在SAPUI5中,格式化程序函数处理从模型绑定传递的值,并返回要应用于生成的HTML代码的格式。它们将对绑定值应用样式类,通常不应返回HTML元素

如果某个条件匹配,是否可以将属性附加到控件元素

对。这是可能的。另一种适合您需要的方法是向所讨论的元素添加样式类。在本例中,第一个文本字段

<Controller Reference>.getView().byId("id of your element").addStyleClass("myboldclass")
.getView().byId(“元素的id”).addStyleClass(“myboldclass”)
其中“myboldclass”是包含所需格式的类

与中一样,如果我使用sap.m.Label,我可以使用格式化程序附加'design:Bold'属性吗

请参阅使用带格式化程序的标签设计或表达式绑定的初步建议


希望这能对您有所帮助。

我的初步建议是,如果允许您在格式化程序中使用Label和LabelDesign,如下所示:

     textBold: function(sText) {
        if (typeof sText === "string" && sText.indexOf("(Self)") > -1) {
            //return $("#relTypeText").html("<b>"+sText+"</b>");
            return sText.bold();
        }
        return sText;
     },
     textBold: function(sText) {
        if (typeof sText === "string") {
            if (sText.indexOf("(Self)") > -1) {
                this.setDesign("Bold");
            } 
        }
        return sText;
     },
<Ref. to label>.setDesign(sap.m.LabelDesign.Bold)
.setDesign(sap.m.LabelDesign.Bold)
或者使用基于“关系类型”值的表达式绑定

备选方案:我认为问题在于您试图使用格式化程序返回HTML代码。在SAPUI5中,格式化程序函数处理从模型绑定传递的值,并返回要应用于生成的HTML代码的格式。它们将对绑定值应用样式类,通常不应返回HTML元素

如果某个条件匹配,是否可以将属性附加到控件元素

对。这是可能的。另一种适合您需要的方法是向所讨论的元素添加样式类。在本例中,第一个文本字段

<Controller Reference>.getView().byId("id of your element").addStyleClass("myboldclass")
.getView().byId(“元素的id”).addStyleClass(“myboldclass”)
其中“myboldclass”是包含所需格式的类

与中一样,如果我使用sap.m.Label,我可以使用格式化程序附加'design:Bold'属性吗

请参阅使用带格式化程序的标签设计或表达式绑定的初步建议


希望对您有所帮助。

您可以在CSS中定义如下样式

.boldText {
   font-weight: bold;
}
还可以定义格式化程序函数:

textBold:函数(sText){
if(sText.indexOf(“(Self)”)>-1){
this.addStyleClass(“boldText”);//这是指文本控件
}
返回文本;
};
你可以检查这个工作


谢谢。

您可以在CSS中定义如下样式

.boldText {
   font-weight: bold;
}
还可以定义格式化程序函数:

textBold:函数(sText){
if(sText.indexOf(“(Self)”)>-1){
this.addStyleClass(“boldText”);//这是指文本控件
}
返回文本;
};
你可以检查这个工作


谢谢。

是的,@Sid,谢谢。我更新了上面的帖子,所以你可以看到我的最终代码。干杯是的,@Sid,是的,谢谢。我更新了上面的帖子,所以你可以看到我的最终代码。干杯