javajco

JCo | How to iterate column wise


The data from SAP is returned as a JCo.Table. However, we don't want to display ALL the columns in the VIEW. So, what we have done is, we have created a file called display.xml which has the JCO.Table columns to be displayed.

The display.xml is converted to a List<DisplayFieldDto> and each field is verified if it is present in the display list (see the code below) which is redundant from second row onwards, as shown in the code below.

What is the better way to filter the fields in JCo? Can I iterate column wise? Thank you :)

final Table outputTable = jcoFunction.getTableParameterList().
                    getTable("OUTPUT_TABLE");
    final int numRows = outputTable.getNumRows();
    for (int i = 0; i < numRows; i++) {         
        final FieldIterator fields = outputTable.fields();
        while (fields.hasNextFields()) {
           final JCO.Field recordField = fields.nextField();
           final String sapFieldName = recordField.getName();
           final DisplayFieldDto key = new DisplayFieldDto(sapFieldName);
           if (displayFields.contains(key)) {
               System.out.println("recordField.getName() = " 
                          + recordField.getName());
               final String sapFieldName = (String)recordField.getValue();      
           } else {
            // ignore the field.
           }
         }
    }

Solution

  • Instead of getting all fields of the table record and checking them against fields present in displayFields, you could take only the fields of the record that are in displayFields. The following code suppose that displayFields is a List of String

    for (int i = 0; i < ouputTable.getNumRows(); i++) {
       outputTable.setRow(i);
       Iterator it = displayField.iterator();
       // get fields of current record
       while(it.hasNext()){
          String fieldName = (String)it.next();
          String value = outputTable.getString(fieldName);
          // do what you need to do with the field and value
       }
    }
    

    hope this helps,
    Regards,
    Guillaume