Showing posts with label sort. Show all posts
Sort and filter in sapui5 smart table

Sort and filter in sapui5 smart table

Sanjo Thomas•08:30:00

In SAPUI5, smart table is a powerful tool that allows you to display and manipulate large amounts of data. One of its key features is the ability to sort and filter data based on a specific field. In this blog, we will explore how to sort and filter data in a smart table using SAPUI5.



Prerequisites:

Before we start, make sure you have a basic understanding of SAPUI5 and smart table. Also, you should have a basic knowledge of JavaScript, HTML, and CSS.


Example:

Let's say we have a smart table with the following fields: Name, Age, Gender, and City. We want to be able to sort the data based on the Age field and filter the data based on the City field.


Step 1: Create a Smart Table in SAPUI5

First, we need to create a smart table in SAPUI5. We can do this by using the SmartTable control in our XML view. Here is an example:

```<smartTable:SmartTable id="smartTable" tableType="ResponsiveTable" entitySet="Employees" ...>```


Step 2: Add Sorting and Filtering Capabilities

Next, we need to add the sorting and filtering capabilities to our smart table. We can do this by using the Sorter and Filter control in our controller. Here is an example:

```

onInit: function() {

    var oSmartTable = this.byId("smartTable");

    var oTable = oSmartTable.getTable();

    

    var oSorter = new sap.ui.model.Sorter("Age", true);

    oTable.getBinding("items").sort(oSorter);

    

    var oFilter = new sap.ui.model.Filter("City", sap.ui.model.FilterOperator.EQ, "New York");

    oTable.getBinding("items").filter(oFilter);

}

```

In the above code, we first get a reference to the smart table and its table control. Then, we create a sorter object and apply it to the binding of the table to sort the data based on the Age field. Similarly, we create a filter object and apply it to the binding of the table to filter the data based on the City field.


Step 3: Display the Smart Table

Finally, we need to display the smart table in our view. We can do this by adding the smart table control to our XML view. Here is an example:


```<mvc:View xmlns:smartTable="sap.ui.comp.smarttable" ...>

    <smartTable:SmartTable id="smartTable" tableType="ResponsiveTable" entitySet="Employees" ...>

    </smartTable:SmartTable>

</mvc:View>

```


Method 2 - Using beforeRebindTable 


Step 1: Create a Smart Table in SAPUI5

First, we need to create a smart table in SAPUI5. We can do this by using the SmartTable control in our XML view. Here is an example:


```<smartTable:SmartTable id="smartTable" tableType="ResponsiveTable" entitySet="Employees" ...>```


Step 2: Add Sorting and Filtering Capabilities

Next, we need to add the sorting and filtering capabilities to our smart table using the HandleBeforeBind function. We can do this by defining the function in our controller and then adding it to the smart table control in our XML view. Here is an example:


Controller:


```

onInit: function() {

    var oSmartTable = this.byId("smartTable");

    oSmartTable.attachBeforeRebindTable(function(oEvent) {

        var oBindingParams = oEvent.getParameter("bindingParams");

        oBindingParams.sorter = new sap.ui.model.Sorter("Age", true);

        oBindingParams.filters = [ new sap.ui.model.Filter("City", sap.ui.model.FilterOperator.EQ, "New York") ];

    });

}

```


In the above code, we first get a reference to the smart table control and then define the HandleBeforeBind function. This function is called before data is retrieved from the backend and allows us to modify the binding parameters. In this case, we create a sorter object and assign it to the sorter property of the binding parameters to sort the data based on the Age field. Similarly, we create a filter object and assign it to the filters property of the binding parameters to filter the data based on the City field.


XML View:

```<mvc:View xmlns:smartTable="sap.ui.comp.smarttable" ...>

    <smartTable:SmartTable id="smartTable" tableType="ResponsiveTable" entitySet="Employees"

        beforeRebindTable=".onBeforeRebindTable" ...>

    </smartTable:SmartTable>

</mvc:View>

```


In the above code, we add the HandleBeforeBind function to the smart table control using the beforeRebindTable property.


Step 3: Display the Smart Table

Finally, we need to display the smart table in our view. We can do this by adding the smart table control to our XML view. Here is an example:


```<mvc:View xmlns:smartTable="sap.ui.comp.smarttable" ...>

    <smartTable:SmartTable id="smartTable" tableType="ResponsiveTable" entitySet="Employees"

        beforeRebindTable=".onBeforeRebindTable" ...>

    </smartTable:SmartTable>

</mvc:View>

```


Sorting and filtering data in a smart table is an important feature that can greatly enhance the user experience. By using the Sorter and Filter controls in SAPUI5, we can easily implement this functionality. With the example provided above, you should now be able to sort and filter data in your own smart tables.

Read more

operations on Internal Table

Sanjo Thomas•07:51:00

Ex on Internal Table operations
Types: Begin of  TY_KNA1,
KUNNR  TYPE  KNA1-KUNNR,
NAME1  TYPE  KNA1-NAME1,
LAND1    TYPE  KNA1-LAND1,
ORT01    TYPE  KNA1-ORT01,
End Of  TY_KNA1.

DATA :  I_KNA1   TYPE  TABLE OF  TY_KNA1.
DATA :  WA_KNA1  TYPE  TY_KNA1.

SELECT   KUNNR   NAME1  LAND1  ORT01
FROM  KNA1  INTO  TABLE  I_KNA1
WHERE  LAND1  =  'US'.

LOOP  AT  I_KNA1  INTO  WA_KNA1.
WRITE:/   WA_KNA1-KUNNR,
WA_KNA1-NAME1,
WA_KNA1-LAND1,
WA_KNA1-ORT01.
ENDLOOP.



Ex on APPEND
  Wa_kna1-kunnr = ‘0000001011’.
  Wa_kna1-land1 = ‘US’.
  Wa_kna1-name1 = ‘AAA’.
  Wa_kna1-ort01 = ‘NEWYORK’.
  Append wa_kna1 to i_kna1.



LOOP AT i_kna1 INTO wa_kna1.
WRITE: / wa_kna1-kunnr,
wa_kna1-name1,
wa_kna1-land1,
wa_kna1-ort01.
ENDLOOP.
  Save->Act->test

Ex on INSERT
  Wa_kna1-kunnr = ‘0000001022’.
  Wa_kna1-land1 = ‘US’.
  Wa_kna1-name1 = ‘BBB’.
  Wa_kna1-ort01 = ‘NEWYORK’.
  Insert wa_kna1 into i_kna1 index 5.
  LOOP AT i_kna1 INTO wa_kna1.
WRITE: / wa_kna1-kunnr,
wa_kna1-name1,
wa_kna1-land1,
wa_kna1-ort01.
ENDLOOP.
  Save->Act->test


Ex on SORT
*sort i_kna1 by kunnr.
*sort i_kna1 by land1 descending.
Sort i_kna1.


LOOP AT i_kna1 INTO wa_kna1.
WRITE: / wa_kna1-kunnr,
wa_kna1-name1,
wa_kna1-land1,
wa_kna1-ort01.
ENDLOOP.
Save->Act->test

Ex on Describe table:

  Data v_lines type I.
  Describe table i_kna1 lines v_lines.
  Write: / ‘total customers are:’, v_lines color 1.
  Save->Act->test





Read table with index:
  Read table i_kna1 into wa_kna1 index 5.
  WRITE: / wa_kna1-kunnr,
wa_kna1-name1,
wa_kna1-land1,
wa_kna1-ort01.
  Save->Act->test

Read table with KEY:
  Read table i_kna1 into wa_kna1 with key kunnr = ‘0000001011’
  binary search.
  WRITE: / wa_kna1-kunnr,
wa_kna1-name1,
wa_kna1-land1,
wa_kna1-ort01.

Save->Act->test

Ex to Modify LAND1 from US to USA:
  Sort i_kna1 by kunnr.
  Read table i_kna1 into wa_kna1 with key kunnr = ‘0000001011’                                 
 binary search.
  Wa_kna1-land1 = ‘USA’.
  Modify i_kna1 from wa_kna1 index sy-tabix transporting land1.
  
  LOOP AT i_kna1 INTO wa_kna1.
WRITE: / wa_kna1-kunnr,
wa_kna1-name1,
wa_kna1-land1,
wa_kna1-ort01.
ENDLOOP.
  Save->Act->test
  Ex2: (modifying All or multiple records from US to USA)
  Wa_kna1-land1 = ‘USA’.
  Modify i_kna1 from wa_kna1 transporting land1
  where land1 = ‘US’.
  LOOP AT i_kna1 INTO wa_kna1.
WRITE: / wa_kna1-kunnr,
wa_kna1-name1,
wa_kna1-land1,
wa_kna1-ort01.
ENDLOOP.
  Save->Act->test



Ex on Delete:
  Ex1: (deleting single record)
  Delete i_kna1 index 5.
  Loop …. Endloop.
  Save->Act->test
  Ex2: (deleting single record)
  Delete i_kna1 where kunnr = ‘0000001011’.
  Loop…. Endloop.
  Save->Act->test
Ex3: (deleting multiple records)
  Delete i_kna1 where land1 = ‘USA’
  Loop…. Endloop
  Save->Act->test
  Ex on Delete adjacent duplicates:
  Wa_kna1-kunnr = ‘0000001033’.
  Wa_kna1-land1 = ‘US’.
  Wa_kna1-name1 = ‘CCC’.
  Wa_kna1-ort01 = ‘NEWYORK’.
  Insert wa_kna1 into i_kna1 index 5.


Insert wa_kna1 into i_kna1 index 8.
  Insert wa_kna1 into i_kna1 index 3.
  Sort i_kna1 by kunnr.
  Delete adjacent duplicates from i_kna1 comparing all fields.
  LOOP AT i_kna1 INTO wa_kna1.
WRITE: / wa_kna1-kunnr,
wa_kna1-name1,
wa_kna1-land1,
wa_kna1-ort01.
ENDLOOP.
  Save->Act->test
  Ex on Clear:
  Wa_kna1-kunnr = ‘0000001044’.
  Wa_kna1-land1 = ‘US’.
  Wa_kna1-name1 = ‘ARJUN4’.
  Wa_kna1-ort01 = ‘NEWYORK’.
  Append wa_kna1 to i_kna1.
  Clear wa_kna1.


Ex on Refresh:
  Refresh i_kna1 .
  LOOP AT i_kna1 INTO wa_kna1.
WRITE: / wa_kna1-kunnr,
wa_kna1-name1,
wa_kna1-land1,
wa_kna1-ort01.
ENDLOOP.
  Save->Act->test
 Ex on FREE:
  FREE i_kna1 .
  LOOP AT i_kna1 INTO wa_kna1.
WRITE: / wa_kna1-kunnr,
wa_kna1-name1,
wa_kna1-land1,
wa_kna1-ort01.
ENDLOOP.
  Save->Act->test

 Ex on Append lines of
  Data: i_kna1 type table of ty_kna1.
  Data: i_kna1_tmp type table of ty_kna1.
  Data: wa_kna1 type ty_kna1.
  Select kunnr name1 land1
  From kna1 into table i_kna1 Up to 10 rows.


Append lines of i_kna1 from 3 to 5 to i_kna1_tmp.
  Loop at i_kna1_tmp into wa_kna1.
   WRITE: / wa_kna1-kunnr,
wa_kna1-name1,
wa_kna1-land1,
wa_kna1-ort01.
ENDLOOP.
  Save->Act->test
  Ex on INSERT lines of
  Insert lines of i_kna1 from 6 to 8 into i_kna1_tmp index 2.
  


  Loop at i_kna1_tmp into wa_kna1.
   WRITE: / wa_kna1-kunnr,
wa_kna1-name1,
wa_kna1-land1,
wa_kna1-ort01.
ENDLOOP.
  Save->Act->test
  Ex on Move itab1 to itab2:
  Refresh i_kna1_tmp.
  I_kna1_tmp [ ] = i_kna1 [ ].
  Loop at i_kna1_tmp into wa_kna1.
   WRITE: / wa_kna1-kunnr,
wa_kna1-name1,
wa_kna1-land1,
wa_kna1-ort01.
ENDLOOP.


(Or)
  Loop at i_kna1 into wa_kna1.
  Append wa_kna1 to i_kna1_tmp.
  Endloop.
  
  Loop at i_kna1_tmp into wa_kna1.
   WRITE: / wa_kna1-kunnr,
wa_kna1-name1,
wa_kna1-land1,
wa_kna1-ort01.
ENDLOOP.



  What is the output of program?
  Loop at i_kna1 into wa_kna1.
  Write: / wa_kna1-kunnr,
  wa_kna1-name1,
  wa_kna1-land1.
  Append wa_kna1 to i_kna1.
  Endloop.


Ex on COLLECT
  Types: begin of ty_kna1,
     Kunnr type kna1-kunnr,
     Name1 type kna1-name1,
     Amount type I,
     End of ty_kna1.
  Data: i_kna1 type table of ty_kna1.
  Data: wa_kna1 type ty_kna1.
  
  Wa_kna1-kunnr = ‘1011’.
  Wa_kna1-name1 = ‘reliance’.
  Wa_kna1-amount = ‘100’.
  Collect wa_kna1 into i_kna1.
  
  Wa_kna1-kunnr = ‘1022’.
  Wa_kna1-name1 = ‘hero motor corp’.
  Wa_kna1-amount = ‘200’.
  Collect wa_kna1 into i_kna1.

  Wa_kna1-kunnr = ‘1011’.

Wa_kna1-name1 = ‘reliance’.
  Wa_kna1-amount = ‘50’.
  Collect wa_kna1 into i_kna1.

  Loop at i_kna1 into wa_kna1.
  Write: / wa_kna1-kunnr, wa_kna1-name1, wa_kna1-amount.
  Endloop.





Read more