jesicadev18
jesicadev18

Reputation: 3162

Rows binding without fieldname into table

I have a Json model in my SAPUI5 app and I want to bind it to a table. I have the table already with columns and items: lets say 3 columns and 3 rows.

<table>
<columns>
<column></column><column></column><column></column>
</columns>
<items> <ColumnListItem> <cells></cells><cells></cells><cells></cells>
</ColumnListItem>
<ColumnListItem> <cells></cells><cells></cells><cells></cells>
</ColumnListItem>
<ColumnListItem> <cells></cells><cells></cells><cells></cells>
</ColumnListItem>
</items>
</table>

I would like to bind dinamically without having to specify text="{/field}" because my Json has 3 arrays that go each to 1 row and the field names aren't the same. Is this possible ?

For example:

var data = {
0: [ {firstname:"AA", lastname: "BB", Phone: "123"}, ],
1: [ {firstnameParent:"AB", lastnameParent: "BC", Phone: "456"},],
2: [ {firstnameGtParent:"ABC", lastnameGtParent: "CC", Phone: "555"},]}

Right now I'm placing for each cell text="{/0/firstname}"text="{/0/lastname}"text="{/0/phone}" and next row text="{/1/firstnameParent}"text="{/1/lastnameParent}"text="{/1/phone}" but the example has 3 cells and if I had more it doesn't sound like a good idea .

Is it possible ? to bind without specifying the fieldname? I've tried different options but without any success.

Expected table output

col1 | col2 | col3
AA | BB | 123
AB | BC | 456
ABC | CC | 555

Upvotes: 0

Views: 226

Answers (1)

Ji aSH
Ji aSH

Reputation: 3457

If your data really looks like your example you can trick it with formatters

bind your rows as follow

items="{'/'}"

bind your cells as follow

text="{ path: '', formatter: 'formatFirstname' }"
text="{ path: '', formatter: 'formatLastname' }"
text="{phone}"

and add the formatters to your controllers

function formatFirstname(obj) {
    for (let v in obj) {
        if(obj.hasOwnProperty(v) && v.startsWith('firstname')) {
            return obj[v]
        }
    }
}

function formatLastname(obj) {
    for (let v in obj) {
        if(obj.hasOwnProperty(v) && v.startsWith('lastname')) {
            return obj[v]
        }
    }
}

(code not tested) You got the idea ? this should give you the result you expect

Upvotes: 0

Related Questions