原理:
设置css,为不同数据的行使用不同的颜色
<!-- CSS colurs for country colors -->
<style type="text/css">
tr[data-background="Mexico"] {
background-color: #eaa6a6 !Important;
}
tr[data-background="Germany"] {
background-color: #7dc0dc !Important;
}
在xml view里面,sap.m.Table的行定义
<ColumnListItem type="Active">
<!-- This is the most important part. This will result in writing country to DOM -->
<customData>
<core:CustomData key="background" value="{Country}" writeToDom="true" />
</customData>
<cells>
这里使用customData - https://sapui5.hana.ondemand.com/#docs/api/symbols/sap.ui.core.CustomData.html
setWriteToDom(bWriteToDom): sap.ui.core.CustomData
Sets a new value for property writeToDom.
If set to “true” and the value is of type “string” and the key conforms to the documented restrictions, this custom data is written to the HTML root element of the control as a “data-*” attribute. If the key is “abc” and the value is “cde”, the HTML will look as follows:
<SomeTag ... data-abc="cde" ... >
Thus the application can provide stable attributes by data binding which can be used for styling or identification purposes. ATTENTION: use carefully to not create huge attributes or a large number of them.
When called with a value of null or undefined, the default value of the property will be restored.
Default value is false.
参考:
https://blogs.sap.com/2016/12/02/binding-based-dynamic-background-colors-for-sap.m.table-rows/