Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    ListGridField getHidden throws NullPointer

    SmartGWT pro 2017-12-17 6.0


    I am trying to get a list of the currently visible fields in a listgrid, since the user might have hidden some. The only way i've found is to iterate through the grid's fields and call getHidden()- on them.

    Unfortunately that method throws a nullpointer.

    Code:
     
     ListGridField[] fields = grid.getFields(); for (ListGridField field : fields) {     if(field != null){         System.out.println("Checking field "+field.getName());         if (field.getHidden()) {//throws Nullpointer!
    Not sure why this can happen? The field is clearly not null since i print out its name on the row before. Thoughts appreciated.

    #2
    Hi mathias,

    the method returns a java.lang.Boolean not simple type boolean. So perhaps you are effectively doing a if (null).
    Try if (Boolean.TRUE.equals(field.getHidden()).

    If it does not work: I'm pretty sure this is the wrong API anyway for your use case, as this should configure the start-state. Changes are done via hide() / show() on the ListGrid and I also have this code in my application:
    Code:
            private List<String> getVisibleFieldList(ListGrid lg) {
                List<String> visibleFieldList = new ArrayList<String>();
                for (ListGridField lgf : lg.getFields())
                    if (lg.fieldIsVisible(lgf.getName()) && (lgf.getCanExport() == null || lgf.getCanExport()))
                        visibleFieldList.add(lgf.getName());
                return visibleFieldList;
            }
    Best regards
    Blama

    Comment


      #3
      Right, missed it was a Boolean object. Doing a nullcheck first works. But you are right on the api as well, so your code looks great, thanks!

      Comment


        #4
        Comment: Had to do "getAllFields" rather than 'getFields' as in your example, otherwise only the visible fields are returned.
        Last edited by mathias; 28 May 2018, 07:14.

        Comment


          #5
          Hi mathias,

          could not believe this, but can confirm using this sample, hiding some columns via context menu and running dsListGrid.getFields() in the Developer Console "Evaluate JS Expression" window.
          The code sample is from a seldom used area of my application. It gets the currently visible fields of the LG and later and adds fields, that definitely must be in the Export, to that list.

          Best regards
          Blama

          Comment


            #6
            I don't think the API is buggy, but perhaps the documentation is not exact. If so, the API you are looking for is missing most likely.
            An array of field objects, specifying the order, layout, formatting, and sorting behavior of each field in the listGrid object. In ListGrids, the fields array specifies columns. Each field in the fields array is a ListGridField object. Any listGrid that will display data should have at least one visible field.
            Best regards
            Blama

            Comment


              #7
              This is why there's a getAllFields() API to get the complete fields. It looks like the docs don't interlink these two, so we'll do that.

              Comment


                #8
                Hi Isomorphic,

                ah, yes. Good to know.

                mathias: I just had a brainstorm. If you can't guess the method name like here neither you or me could, you can also search the docs for the suspected return type, here ListGridField[].

                Best regards
                Blama

                Comment


                  #9
                  There was actually a clearer doc here, but it was suppressed due to a glitch in formatting, but it is corrected now. Please try the next nightly build, dated June 01.

                  Regards
                  Isomorphic Software

                  Comment


                    #10
                    Great stuff. Small comment: If i was an API designer at Isomorphic, i would have returned all fields from the 'GetFields()' method, and have a getVisibleFields() method to get only the visible ones. To me it sets up the expectations better.

                    Comment

                    Working...
                    X