简体   繁体   English

SAPUI5 sap.m.StandardTreeItem 如何设置背景色?

[英]How to Set the Background Color of SAPUI5 sap.m.StandardTreeItem?

I would like to set a custom color of sap.m.Tree items .我想设置sap.m.Tree items的自定义颜色。 Looking other examples in sap.m.List , I implemented onAfterRendering of sap.m.StandardTreeItem :查看sap.m.List其他示例,我实现了onAfterRenderingsap.m.StandardTreeItem

snip
    sap.m.StandardTreeItem.extend('MySuperDuperTreeItem', {
     metadata: {
     properties: {
         status: 'string'
     }
     },      
     onAfterRendering: function() {
     if (sap.m.StandardTreeItem.prototype.onAfterRendering) {
         sap.m.StandardTreeItem.prototype.onAfterRendering.apply(this, arguments);
     }
     //var a = getItem(); NOT WORKING
     this.$().find('div').each(function() {
         if ($(this).hasClass('sapMCbBg')) {
         $(this).addClass("myPurple");
         }
     });
     },
     renderer:{}

 });

I have put an example here .在这里放了一个例子。

From the JSBin link, you can see the check boxes have a custom color, but I would like to set their color based on item's title.从 JSBin 链接,您可以看到复选框具有自定义颜色,但我想根据项目的标题设置它们的颜色。 Also, the tree item detail button is not properly displayed an expand and collapse.此外,树项目详细信息按钮未正确显示展开和折叠。

Is there an alternative way to color check boxes?有没有其他方法可以为复选框着色? Why it is not possible to access sap.m.StandardTreeItem members (like getTitle() ) in onAfterRendering function?为什么无法在 onAfterRendering 函数中访问sap.m.StandardTreeItem成员(如getTitle() )?

Use this.getTitle() in the onAfterRendering method to get the title.onAfterRendering方法中使用this.getTitle ()来获取标题。 And based on that title you can add your style class based on the title as below根据该标题,您可以根据标题添加样式类,如下所示

    onAfterRendering: function() {
         sap.m.StandardTreeItem.prototype.onAfterRendering.apply(this, arguments);
         var title = this.getTitle();
         this.$().find('div').each(function() {
             if ($(this).hasClass('sapMCbBg')) {
               if (jQuery.sap.startsWith(title, "A-Node")) {
                 $(this).addClass("myPurple");
               } else if (jQuery.sap.startsWith(title, "B-Node")) {
                 $(this).addClass("myYellow");
               }
             }
         });
     }

And the thing about the tree item detail button is not properly displayed an expand and collapse, you have to just modify your code little bit as below并且关于树项详细信息按钮的事情没有正确显示展开和折叠,您只需稍微修改您的代码,如下所示

 var oStandardTreeItem = new MySuperDuperTreeItem({
     title: "{text}",
     type: sap.m.ListType.Detail
 });
 oTree.bindItems("/", oStandardTreeItem);
 oTree.expandToLevel(3);
 oTree.setMode(sap.m.ListMode.MultiSelect);
 var oPage = new sap.m.Page("TreeTest", {
     title : "Test Page for sap.m.Tree",
     content : [oTree]
 });

Here is the link that renders the check boxes with different colors based on the title.这是根据标题以不同颜色呈现复选框的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM