Announcement

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

    Databound Menu - Display Field Problem

    I want to create a menu which is bound to a DataSource. The DataSource doesn't have a field named "id", "title" or "name"; which I read -can't remember where- are looked for to display its value in the MenuItem label.
    I've tried to use setTitleField method and even specifying a ListGridField with DisplayField attribute set (commented below) but I have had no success. When I use the setTitleField method the labels in menu items are empty, when I set the ListGridField; labels have correct titles but the border and background of the menu is not shown. Any help is appreciated, thanks.

    Menu
    Code:
    		MenuButton btnMenu=new MenuButton("Show Actions");
    
    		Menu mActions = new Menu();
    		mActions.setDataSource(ActionsDS.getInstance());
    		mActions.setTitleField("actionTitle");
    //		ListGridField fldName=new ListGridField("name");
    //		fldName.setDisplayField("actionTitle");
    //		fldName.setValueField("actionId");
    		mActions.setFields(fldName);
    
    		mActions.addItemClickHandler(new ItemClickHandler() {
    			public void onItemClick(ItemClickEvent event) {
    				MenuItem item = event.getItem();
    				SC.say("You picked the \""
    						+ item.getAttributeAsString("actionTitle") + "\" action.");
    			}
    		});
    
    		btnMenu.setMenu(mActions);
    		btnMenu.draw();
    DataSource
    Code:
    public class ActionsDS extends DataSource {
    
    	private static ActionsDS instance = null;
    
    	public static ActionsDS getInstance() {
    		if (instance == null) {
    			instance = new ActionsDS("actionsDS");
    		}
    		return instance;
    	}
    
    	public ActionsDS(String id) {
    		setID(id);
    		setRecordXPath("/response/data");
    
    		DataSourceTextField fldId = new DataSourceTextField("actionId");
    		fldId.setPrimaryKey(true);
    		DataSourceTextField fldTitle = new DataSourceTextField("actionTitle");
    
    		setFields(fldId, fldTitle);
    
    		setDataFormat(DSDataFormat.JSON);
    
    		setDataURL(GWT.getModuleBaseURL()+"data/json/actions.json");
    	}
    
    }
    Data (actions.json)
    Code:
    {"response":{
    	"status":200,
    	"data":[
    		{"actionId":"1","actionTitle":"Aprove"},
    		{"actionId":"2","actionTitle":"Reject"},
    		{"actionId":"3","actionTitle":"Route"}
    	],
    	"endRow":2,
    	"startRow":0,
    	"totalRows":3
    	}
    }
    I'm using SmartGWT 2.4, Firefox 3.6.16, GWT 2.0.4, Eclipse Plugin, Eclipse Helios, Ubuntu 10.10

    #2
    The Menu doesn't support using alternate field names. If you can't change the JSON response, you can transform the data in transformResponse().

    Comment


      #3
      Originally posted by Isomorphic
      The Menu doesn't support using alternate field names. If you can't change the JSON response, you can transform the data in transformResponse().
      Thanks for your reply. Here's the snippet that solved the problem.

      Code:
      @Override
      protected void transformResponse(DSResponse response, DSRequest request,
      			Object data) {
      	for(Record rec:response.getData()){
      		rec.setAttribute("name", rec.getAttribute("actionTitle"));
      	}
      	super.transformResponse(response, request, data);
      }

      Comment

      Working...
      X