Manipulate icon properties for the last row of table

落花浮王杯 提交于 2020-06-17 15:45:43

问题


In XML view for i have :

<Table id="testtable" xmlns="sap.ui.table"
  rows="{/testdata}"
  alternateRowColors="true">
  <columns>
    <Column hAlign="Center" label="Col1">
      <template>
        <m:Text text="{dataX}" wrapping="false" />
      </template>
    </Column>
    <Column hAlign="Center" label="Col2">
      <template>
        <m:Text text="{dataY}" wrapping="false" />
      </template>
    </Column>
    <Column label="Col3">
      <template>
        <m:HBox>
<core:Icon src="sap-icon://show" color="{ parts : [ 'test'], formatter: '.setIconColour'}" />
<core:Icon src="sap-icon://edit" color="{ parts : [ 'test' ], formatter: '.setIconColour'}" />
<core:Icon src="sap-icon://print" color="{ parts : [ 'test' ], formatter: '.setIconColour'}" />
        </m:HBox>
      </template>
    </Column>
  </columns>
</Table>

In controller (formatter function is as):

setIconColour: function (value) {
 if (value === 1) {
    return "#007bff";
  } else if (value === 2) {
    return "Positive";
  } else if (value === 3) {
    return "Negative";
  } 
}

The sample data is as:

{"testdata": [
    { "dataX": 1, "dataY": "testdata", "test": 0},
    { "dataX": 2, "dataY": "testdata", "test": 2},
    { "dataX": 3, "dataY": "testdata", "test": 3},
    { "dataX": 4, "dataY": "testdata", "test": 1}
]}

This changes color of Icon Properties based on test value ,after this may i know how can i change the color of only last row icons or (only row) if only one row exists (rest all have same properties as per formatter function)

I am trying to do this as :

      var tabItems = this.byId("testtable").getRows(); 

      var cells = tabItems[testdata.length-1].getCells(); // get last row cells

      cells[8].mAggregations.items[0].setColor(
      "#000000");  // at this path i have all 3 icons and trying to set color here (items[0],items[1],items[2] --> 3 icons)

But this approach doesn't work as expected giving weird results until page refresh , hope there would be a better way , any help or guiding links are much appreciated TIA


回答1:


Maybe you can add index your JSON then you can give index and length of JSON to your formatter.

JSON like this:

{"testdata": [
    { index: 1, "dataX": 1, "dataY": "testdata", "test": 0},
    { index: 2, "dataX": 2, "dataY": "testdata", "test": 2},
    { index: 3, "dataX": 3, "dataY": "testdata", "test": 3},
    { index: 4, "dataX": 4, "dataY": "testdata", "test": 1}
]}

Formatter:

setIconColour: function (value, index, length) {
 // 
}


来源:https://stackoverflow.com/questions/61868565/manipulate-icon-properties-for-the-last-row-of-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!