cancel
Showing results for 
Search instead for 
Did you mean: 

How to get column binding in JavaScript

sebastianraemsch
Active Participant
0 Kudos

Hi,

I try to get the binding information of columns of a sap.m.table in JavaScript but I can´t figure out how to do that.

This is my table definition (shortened example):


<Table id="tblSystemSelection" inset="false" items="{/Systems}" mode="SingleSelectMaster" width="auto">

  <columns>

  <Column>

  <Label text="Column A"/>

  </Column>

  <Column>

  <Label text="Column B"/>

  </Column>

  </columns>

  <items>

  <ColumnListItem>

  <Text text="{MyColumnA}"/>

  <Text text="{MyColumnB}"/>

  </ColumnListItem>

  </items>

</Table>

Now, in JavaScript I would like to get the binding paths "MyColumnA" and "MyColumnB".

I can get the table object and the binding info for the "items" collection:


var oTable = sap.ui.getCore().byId("tblSystemSelection");

var oItemsBinding = oTable.getBinding("items");

I can also get the cells of the item template:


oBindingInfo.template.getCells()

But nowhere I can find the binding path information "MyColumnA" and "MyColumnB".

Does anyone know how to get them?

Thanks and best regards,

Sebastian

Accepted Solutions (1)

Accepted Solutions (1)

former_member183518
Active Participant
0 Kudos

You can try something like this.


var aBindingInfos = oTable.getBindingInfo('items').template.getCells().map(function(oCell){

     return oCell.getBindingInfo('text').parts[0].path;

});

console.log(aBindingInfos) // ["MyColumnA","MyColumnB"]

Answers (1)

Answers (1)

saivellanki
Active Contributor
0 Kudos

Hi Sebastian,

I could see in your xml view that you haven't done any columns binding with model. They are hard-coded as "Column A" and "Column B" for text property using label control. If you just wanted to get hold of the columns, you can just say like this -


var oTable = this.getView().byId("yourTableID");               //Your Table ID

oTable.getColumns();          //This will fetch you the columns array and you can do [0] [1] ....... to get hold of specific column

If you're looking on something about the columns binding and retrieving them using the method oTable.getBinding("columns"), then will this JSBin help? JS Bin - Collaborative JavaScript Debugging


Click on the Columns Binding button, you will get the binding path and the model associated with that path as alert. Please correct if my understanding is wrong with regard to your requirement.


Regards,

Sai Vellanki.

sebastianraemsch
Active Participant
0 Kudos

Hi Sai Vellanki,

I got already to that point. But I don´t know how to fetch the property value "MyColumnA". In your JS Bin the value of oBindingPath is "/modelData/columnsData". But this is not what I want to retrieve. In your example I would like to retrieve "Product" and "Weight":


                    <cells>

                      <Text text="{Product}" />

                      <Text text="{Weight}" />

                    </cells>

Do you have an idea how to do that?

Thanks, Sebastian

sebastianraemsch
Active Participant
0 Kudos

...sorry, one additional comment:

line


alert(JSON.stringify(sap.ui.getCore().getModel().getProperty(oBindingPath)));

deliveres what I want. But: You set the properties already for the model. I don´t have them in the model but only in the view definition.

Regards, Sebastian

saivellanki
Active Contributor
0 Kudos

Hi Sebastian,

How about this one? JS Bin - Collaborative JavaScript Debugging

Regards,

Sai Vellanki.

sebastianraemsch
Active Participant
0 Kudos

Hi Sai Vellanki,


Thanks for your quick answer. However, it doesn´t help me here because this only seem to work for JSON model and not for oData model and second I can´t retrieve these information from the model because the table might only contain a subset of all available columns. But in the end I would like to get all columns (all binding paths of them) of the table defined in xml view. If this is not possible I have to do it statically in JavaScript - possible but not so nice.


Best regards,

Sebastian


saivellanki
Active Contributor
0 Kudos

Hi Sebastian,

The last option I would suggest: JS Bin - Collaborative JavaScript Debugging

Regards,

Sai Vellanki.

sebastianraemsch
Active Participant
0 Kudos

Sorry, but in this case I would get "Column A" (the text of the label) but not "MyColumnA".

Br, Sebastian

saivellanki
Active Contributor
0 Kudos

Yeah my bad.

Any ways, I have ran out of ideas. Let us wait and check if any expert responds on this.

Regards,

Sai Vellanki.

former_member182862
Active Contributor
0 Kudos

maybe this will shed some lights

JS Bin - Collaborative JavaScript Debugging