How to set table row class value dynamically in openui5 / sapui5? - sapui5

There is a openui5 offical example about table.
Is there anyway to set class attribute dynamically for table row in the Table.view.xml.
<ColumnListItem>
<cells>
<ObjectIdentifier
title="{Name}"
text="{ProductId}"/>
<Text
text="{SupplierName}" />
<Text
text="{Width} x {Depth} x {Height} {DimUnit}" />
<ObjectNumber
number="{WeightMeasure}"
unit="{WeightUnit}"
state="{
path: 'WeightMeasure',
formatter: 'sap.m.sample.Table.Formatter.weightState'
}" />
<ObjectNumber
number="{
parts:[{path:'Price'},{path:'CurrencyCode'}],
type: 'sap.ui.model.type.Currency',
formatOptions: {showMeasure: false}
}"
unit="{CurrencyCode}" />
</cells>
</ColumnListItem>
Eg.
row 1
<ColumnListItem class="bg-gray">
row 2
<ColumnListItem class="bg-blue">
row 3
<ColumnListItem class="bg-green">
The following code does not meet the requirements:
<ColumnListItem class="{rowStyle}">

Unfortunately, 'class' cannot be bound to a property. But there are easy alternatives.
It involves below steps.
Create a property (Using CustomData) in the DOM using binding.
<ColumnListItem type="Active">
<customData>
<core:CustomData key="background" value="{Country}" writeToDom="true" />
</customData>
<cells>
<ObjectIdentifier title="{CustomerID}"/>
<Text text="{CompanyName}"/>
<Text text="{Address}"/>
<Text text="{Country}"/>
</cells>
</ColumnListItem>
Use Attribute-Value CSS selectors to select the above
written DOM and apply color
tr[data-background="Mexico"] {
background-color: #eaa6a6 !Important;
}
I have written a blog here.
https://blogs.sap.com/2016/12/02/binding-based-dynamic-background-colors-for-sap.m.table-rows/
JS Bin

You can use formatter, function will be like this :
setColour : function(Condition){
if(Condition){
var cellId = this.getId();
$("#"+cellId).parent().parent().parent().css("background-color","Blue");
}
}
your column code will be like this :
<Input value="{path:'Condition',formatter:'formmater_path.setColour'}" />

Related

SAPUI5 table in list

I have a view:
<List
items="{data>/Stages}">
<CustomListItem>
<Panel>
<headerToolbar>
<Toolbar>
<content>
<Button
visible="{= ${data>/Stages/length} > 1}" />
</content>
</Toolbar>
</headerToolbar>
<content>
<Table
growing="true"
growingScrollToLoad="false"
items="{
path: 'data>FieldWorks',
templateShareable: true
}"
class="blueTable originTable techTable">
<columns>
<Column
visible="true"
vAlign="Middle"
width="15px" />
</columns>
<items>
<ColumnListItem>
<cells>
<core:Icon
visible="{ ???}"
src="sap-icon://customfont/moving"
size="2.1rem"
color="#14c6c9" />
</cells>
</ColumnListItem>
</items>
</Table>
</content>
</Panel>
</CustomListItem>
</List>
I have a model (example):
data: {
Stages: [{
FieldWorks: [{}, ....]
}, .....]
}
I hide my button (in my toolBar) by expression binding as you can see.
What is the best way to hide my Icon (in CustomListItem) by condition FieldWorks.length > 1
If you are using a JSON Model just add boolean property to your JSONModel to the FieldWorks entity and bind the visible property to it. Then one time you have to set it when data is loaded or created and that's it.
You can define a new property in your json Model under the FieldWorks property in order to bind the visibility of the icon.
you can use Object.defineProperty (SPEC MDN) to define your calculated new property.

Time Format and value mapping Not Working in the fragment UI5

I have fragment as mentioned below. I am mapping from a model --> myTable.
The columns are visible accept Start Time. I tried different approach for that column. Can any one find the mistake in the code. Below is the JSON Structure :
<core:FragmentDefinition xmlns="sap.m"
xmlns:core="sap.ui.core">
<Page id="jobTableDisplayPage" showHeader="false" enableScrolling="true">
<content>
<Table id="jobTable" items="{myTable>/d/results/}">
<columns>
<Column width="15em">
<Text text="Job Name" />
</Column>
<Column width="5em">
<Text text="User Name" />
</Column>
<Column width="5em">
<Text text="Job Status" />
</Column>
<Column width="5em">
<Text text="Start Date" />
</Column>
<Column width="5em">
<Text text="Start Time" />
</Column>
<Column width="5em">
<Text text="End Time" />
</Column>
<Column width="5em">
<Text text="Spool Number" />
</Column>
</columns>
<items>
<ColumnListItem>
<cells>
<Text text="{myTable>Jobname}" />
<Text text="{myTable>Usern}" />
<Text text="{myTable>Status}" />
<Text text="{myTable>Startdate}" />
<Text
text="{ path: '{myTable>Starttime}',
type: 'sap.ui.model.type.Time',
formatOptions: {
relative: true,
relativeScale: 'auto'
}
}" />
<Text text="{myTable>Endtime}" />
<Text text="{myTable>Spool}" />
</cells>
</ColumnListItem>
</items>
</Table>
</content>
</Page>
</core:FragmentDefinition>
The columns are visible accept Start Time. I tried different approach for that column. Can any one find the mistake in the code. Below is the JSON Structure :
{
"d":{
"results":[
{
"__metadata":{
"id":"blablabla",
"uri":"blablabla",
"type":"blablabla"
},
"Jobname":"JOB1",
"Usern":"BC-BATCH",
"Status":"F",
"Startdate":"10/27/2017",
"Starttime":"PT03H00M49S",
"Endtime":"PT03H31M12S",
"Spool":"0000033977"
},
{
"__metadata":{
"id":"blablabla",
"uri":"blablabla",
"type":"blablabla"
},
"Jobname":"JOB2",
"Usern":"BC-BATCH",
"Status":"F",
"Startdate":"10/27/2017",
"Starttime":"PT03H00M49S",
"Endtime":"PT03H31M12S",
"Spool":"0000033977"
}
]
}
}
I believe path doesn't need the curly brackets.
Try:
<Text text="{ path: 'myTable>Starttime', type: 'sap.ui.model.type.Time',
formatOptions: { relative: true, relativeScale: 'auto' } }" />
As mentioned in this answer, you need to use the odata binding type instead of the regular one.
So, in case of the odata type Edm.Time (Starttime and Startdate in your case):
type: 'sap.ui.model.type.Time', --> type: 'sap.ui.model.odata.type.Time',
And yes, the additional curly brackets around the path are invalid. So it should be:
<Text text="{
path: 'myTable>Starttime',
type: 'sap.ui.model.odata.type.Time',
formatOptions: {
relative: true
}
}"/>

Format Date field in Smart Table SAP Ui5

How to format the Date field in Smart Table?
Oct 17, 2017 to 17/10/2017
The only way I found was by making it a customData and formmatting it
<customData>
<core:CustomData key="p13nData" value='\{"columnKey": "Erdat", "leadingProperty": "Erdat"}' />
</customData>
then
<Text text="{parts: ['Erdat'],formatter: '.formatDate'} " />
this way I was able to format my data
You may also use sap annotation sap:display-format='Date' for this:
<Property
sap:label="Test Date"
Name="ZDATE"
Type="Edm.DateTime"
sap:display-format="Date"
Precision="0"/>
which results in
Or via customData and composite binding with constraints option of sap.ui.model.odata.type.DateTime which allows you not to create custom formatter:
<Table>
<columns>
<Column hAlign="Begin">
<customData>
<core:CustomData key="p13nData"
value='\{"columnKey": "TEST_DATE",
"columnIndex":"9",
"leadingProperty": "ZDATE",
"width": "10%"
}'/>
</customData>
<Text text="Test Date Custom"/>
</Column>
</columns>
<items>
<ColumnListItem>
<cells>
<Text text="{
path: 'ZDATE',
type: 'sap.ui.model.odata.type.DateTime',
constraints: { displayFormat: 'Date' }
}"/>
</cells>
</ColumnListItem>
</items>
</Table>

SAPUI5 XML View Table Color

I´ve started with my first SAPUI5 application and build a responsive table.
Now I have the requirement to color specific rows depends on a value in the model.
I´m using a XML-View.
Could I define a method in my controller for that? (How it should be working?)
Home.view.xml
<Table id="idMachineTable"
inset="false"
items="{
path: 'machinemodel>/collection'
}">
<headerToolbar>
<Toolbar>
<Title text="Header" level="H2"/>
</Toolbar>
</headerToolbar>
<columns>
<Column
width="12em">
<Text text="Product" />
</Column>
<Column
hAlign="Right">
<Text text="Price" />
</Column>
</columns>
<items>
<ColumnListItem>
<cells>
<ObjectIdentifier
title="{machinemodel>test}"
text="{machinemodel>test}"/>
<Text
text="{machinemodel>test}" />
</cells>
</ColumnListItem>
</items>
</Table>
You can do that with a customdata attribute that creates a dom attribute. Then you can select the rows you want to color via css.
<ColumnListItem>
<customData>
<core:CustomData key="mydata" value="{machinemodel>status}" writeToDom="true" />
</customData>
<cells>
...
<html:style type="text/css">
tr[data-mydata="B"] {
background-color: #faa !important;
}
</html:style>
Full example on jsbin.
I like the answer #schnoebel provided
here is an alternate way (jsbin), in the Items binding define a change handler
items="{
path: 'machinemodel>/collection',
events: {
change: '.onItemsChange'
}
}"
then in the handler add your style class
onItemsChange: function(oEvent){
var oTable = this.byId("idMachineTable");
oTable.getItems().forEach(function(oItem){
var oContext = oItem.getBindingContext("machinemodel");
if (oContext && oContext.getObject().status === 'A'){
oItem.addStyleClass("overdue");
}
});
}

Icon in table incompatible with mergeDuplicates="true"

I have a Table and I want to merge duplicates in the first column:
<Column mergeDuplicates="true">
<Text text="Society"/>
</Column>
<Column>
<Text text="Ref2"/>
</Column>
...
I want to have a complex element in the first column: An icon that shows the flag of society.
<items>
<ColumnListItem>
<cells>
<StandardListItem title="{model>society/description}"
icon="{path: 'model>society/code',
formatter: 'ui5bp.Formatter.iconGeneral'}"/>
<!--<Text text="{model>society/description}"/>-->
<Text text="{model>ref2}"/>
</cells>
</ColumnListItem>
</items>
But if I set the StandardListItem instead the simple text the mergeDuplicates="true" does not work.
Are complex column items and the mergeDuplicates property incompatible?
Now StandardListItem has this result:
How can I create a correct "MyCustomColumnListItem" to show the flag on the left and descriprion on the right without space up and down?
The data in your example is not mergeDuplicate ready (from what you show, nothing would be merged), but I took a guess and set up some test data that might be similar. You can perhaps look at using an ObjectAttribute which has an icon and a text, as shown in this snippet.
sap.ui.xmlview("main", {
viewContent: jQuery("#view-main").html()
})
.setModel(new sap.ui.model.json.JSONModel({
records : [
{ icon : "http://www.flags.net/images/smallflags/ANTA0001.GIF", text : "Delete", comment : "delete" },
{ icon : "http://www.flags.net/images/smallflags/ANBA0001.GIF", text : "Delete", comment : "negative" },
{ icon : "http://www.flags.net/images/smallflags/ANDR0001.GIF", text : "Remove", comment : "sys-minus" }
]
}))
.placeAt("content");
<script src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js" id="sap-ui-bootstrap" data-sap-ui-libs="sap.m" data-sap-ui-theme="sap_bluecrystal" data-sap-ui-xx-bindingSyntax="complex"></script>
<div id="content"></div>
<script id="view-main" type="ui5/xmlview">
<mvc:View
displayBlock="true"
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m">
<Table
items="{/records}">
<columns>
<Column mergeDuplicates="true"><Label text="Icon" /></Column>
<Column><Label text="Comment" /></Column>
</columns>
<items>
<ColumnListItem>
<cells>
<ObjectStatus
text="{text}"
icon="{icon}" />
<Text text="{comment}" />
</cells>
</ColumnListItem>
</items>
</Table>
</mvc:View>
</script>
Not sure if a custom control or ListItem will work...
As an alternative, you could also use two columns, each set with mergeDuplicates="true" and display an Image and Text separately. That should definitely work
I.e.:
<Table id="tbl" items="{model>/yourData}">
<columns>
<Column mergeDuplicates="true" mergeFunctionName="getSrc">
<Text text="Society" />
</Column>
<Column mergeDuplicates="true" />
</columns>
<items>
<ColumnListItem>
<cells>
<Image src="{model>society/flagImg}"/>
<Text text="{model>society/description}" />
</cells>
</ColumnListItem>
</items>
</Table>