简体   繁体   English

如何更改表(SAPUI5)中行的颜色?

[英]How to change the color of row in table (SAPUI5)?

I have a table in which data is coming from back end. 我有一个表,其中数据来自后端。 Based on particular condition(data coming from back end (gateway service)), if that condition is true then that particular row will have different color and will have a link on full row. 根据特定条件(来自后端(网关服务)的数据),如果该条件为true,则该特定行将具有不同的颜色,并将在整行上具有链接。 And if condition is false then it will be a normal row. 如果条件为假,则它将是正常行。

So how to achieve it ? 那么如何实现呢?

Semantic colors for rows are now supported which can be leveraged by using the property highlight 现在支持行的语义颜色,可以通过使用属性highlight来利用

  • on ColumnListItem when using sap.m.Table (since 1.44): 在使用ColumnListItem时在sap.m.Table (从1.44开始):

     <ColumnListItem highlight="{= ${odataModel>foo} > 50 ? 'Error' : 'None'}" > ... 
  • on RowSettings when using sap.ui.table.Table (since 1.48): 在使用RowSettings时在sap.ui.table.Table (从1.48开始):

     <Table> <rowSettingsTemplate> <RowSettings highlight="{= ${odataModel>foo} > 50 ? 'Error' : 'None'}" </rowSettingsTemplate> <columns> ... 

    表突出显示的行颜色


Samples 样品

I think there are few different ways to change the colour, the easiest would be to change the style of the associate control 我认为更改颜色的方法很少,最简单的方法是更改​​关联控件的样式

<control>.addStyleClass(myClass);
<control>.toggleStyleClass(myClass, true);

in the following example JsBin - alert overdue rows i use the following on a table row 在以下示例JsBin-警报过期行中,我在表行上使用了以下内容

row.$().addClass("overdue");

it adds the css style "overdue" to the domRef of the row 它将CSS样式“过期”添加到该行的domRef中

I assume you've got regular HTML table, then: 我假设您有常规的HTML表,然后:

$("table tr").each(function(){
    if( condition ){ //your condition eg. $(this).children("td:eq(1)").val() == sth
        $(this).css("background":COLOR);
    }
});

@Ashish its very difficult using only sapui5. @仅使用sapui5很难做到。 you need to use jquery in that case. 在这种情况下,您需要使用jquery。 If that condition is true get the row's index and have a selector of that and then use like 如果条件成立,则获取行的索引并为其选择一个索引,然后使用like

$('.myTable tr:nth-child('+rowindex+')').css("background-color","red")

Try this. 尝试这个。 not sure 不确定

In UI5, I'm not sure if you can do this for a row at once, but you can certainly do this for a single cell using the valueState property: 在UI5中,我不确定是否可以一次连续执行此操作,但是肯定可以使用valueState属性对单个单元格执行此valueState

var oConditionalColumn = new sap.ui.table.Column({
    label: new sap.ui.commons.Label({
        text: "Some label"
    }),
    template: (new sap.ui.commons.TextField({
        value     : "{myfield}",
        valueState : {
            parts : [myfield],
            formatter : function(oValue) {
                return (oValue === undefined || oValue == null || oValue == "" || isNaN(oValue) || parseInt(oValue) == 0) ? sap.ui.core.ValueState.Error : sap.ui.core.ValueState.None;
            }
        },
    }))
});

Or, when you load the model, set an extra calculated property in advance based on your condition, and use that property to render each cell in your row in a different color for the current row context with a mior modification of the above code. 或者,当您加载模型时,请根据您的条件预先设置一个额外的计算属性,并使用该属性对当前行上下文使用不同的颜色来渲染行中的每个单元格,并对上述代码进行较小的修改。

Instead of using CSS we can use sap.m.ObjectStatus to apply colors. 代替使用CSS,我们可以使用sap.m.ObjectStatus来应用颜色。

var iDtemplate = new sap.m.ColumnListItem("idTemplate", {
  type: "Navigation",
  visible: true,
  selected: true,
  cells: [
    new sap.m.ObjectStatus({
      text: "{SlNo}",
    }).bindProperty("state", "number", function(value) {
      return getStatusColor(value);
    }),


    new sap.m.ObjectStatus({
      text: "{Name}",
    }).bindProperty("state", "number", function(value) {
      return getStatusColor(value);
    }),
  ],
  pressListMethod: function(event) {
    var bindingContext = event.getSource().getBindingContext();

  }
});

function getStatusColor(status) {
  if (status === '') {
    return "Error";
  } else {
    return "Success";
  }
}

Based on the number field we are giving colors to columns Slno and Name . 基于数字字段,我们为SlnoName列提供颜色。

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

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