user2644620
user2644620

Reputation: 199

SAP UI5 - How to disable a row in sap.m.Table Multiselect table based on row status

I have a bit a tricky problem. I want to disable the row selection is sap.m.Table. But the Table is available in Panel Content

<VBox items="{dataModel>/Products}">
   <Panel expandable="true" id="testPanel" expanded="false" width="auto" class="sapUiResponsiveMargin">
      <headerToolbar>
         <Toolbar style="Clear">
            <Text text="{dataModel>productText}"></Text>
         </Toolbar>
      </headerToolbar>
      <content>
         <Table id="productsTable" items="{dataModel>productsList}" id="skillsTable" visible="true" sticky="ColumnHeaders"
            mode="MultiSelect">
            <columns>
               <Column>
                  <Text text="Product Name"/>
               </Column>
               <Column>
                  <Text text="Product No"/>
               </Column>
               <Column>
                  <Text text="Asset"/>
               </Column>
               <Column>
                  <Text text="Check Duplicate"/>
               </Column>
            </columns>
            <items>
               <ColumnListItem >
                  <cells>
                     <Text text="{dataModel>productName}"/>
                     <Text text="{dataModel>productNo}"/>
                     <Text text="{dataModel>asset}"/>
                     <Text text="{dataModel>checkDuplicate}"/>
                  </cells>
               </ColumnListItem>
            </items>
         </Table>
      </content>
   </Panel>
</VBox> 

If you see the above Code, There are 3 points... 1)VBox has an array of Products 2)Inside VBox there is Panel 3)Inside Panel, in Panel Content, I have given sap.m.Table, It means like there might be multiple tables will get generated based on the data coming from backend.

Now my requirement is like how can I disable a row based on the boolean value in checkDuplicate?

Below is my controller logic, what I'm trying...

var tbl = self.getView().byId('productsTable');
var header = tbl.$().find('thead');
var selectAllCb = header.find('.sapMCb');
selectAllCb.remove();

tbl.getItems().forEach(function (r) {
    var obj = r.getBindingContext("dataModel").getObject();
    var oStatus = obj.checkDuplicate;
    var cb = r.$().find('.sapMCb');
    var oCb = sap.ui.getCore().byId(cb.attr('id'));
    if (oStatus == "true") {
        oCb.setEnabled(true);
    } else {
        oCb.setEnabled(false);
    }
});

The problem with the above logic is, if there is only one Table, then based on the Table id, I can get the row data and I can disable the row. But here multiple Tables will get generated dynamically, So I can't give ID for the Table.

Can someone please help me how can I disable the row based on checkDuplicate boolean Value?

Thank you in advance

Upvotes: 1

Views: 7303

Answers (1)

Voyager
Voyager

Reputation: 840

Add an event listener for the event modelContextChange to your table.

Fired when models or contexts are changed on this object (either by calling setModel/setBindingContext or due to propagation)

<Table id="productsTable" 
       modelContextChange=".onModelContextChange"
       items="{dataModel>productsList}"
       sticky="ColumnHeaders"
       mode="MultiSelect"
>
...
</Table>

The event contains the parameter "id" of the current table. This way, you can retrieve the current table by its complete id and re-use your code with a minor change.

onModelContextChange: function(oEvent) {
    var sId = oEvent.getParameter("id");
    var tbl = sap.ui.getCore().byId(sId);
    var header = tbl.$().find('thead');
    var selectAllCb = header.find('.sapMCb');
    selectAllCb.remove();

    tbl.getItems().forEach(function (r) {
        var obj = r.getBindingContext("dataModel").getObject();
        var oStatus = obj.checkDuplicate; 
        var cb = r.$().find('.sapMCb');
        var oCb = sap.ui.getCore().byId(cb.attr('id'));
        if (oStatus == "true") {
            oCb.setEnabled(true);
        } else {
            oCb.setEnabled(false);
        }
    });
}

Upvotes: 5

Related Questions