Project

General

Profile

WItemDelegate and editable tables

Added by Mark Petryk about 3 years ago

Perhaps I'm misusing WItemDelegate but I am trying to replace an 'id' column value with a 'text' column value for that id.

The 'id' is provided by the underlying table. I want to take that 'id' value, look up the item it belongs to and display some other text value from that record. But, I have editing enabled on the table, and if I open an editor and then close it, on any other column, the table-view updates the 'id' column with the original ID value and not the replaced text value. However, also, the WItemDelegate::update function never gets called when I update some other column.

(see attached image)

My code for the delegate is here;

  88     virtual std::unique_ptr<Wt::WWidget> update( Wt::WWidget * widget, const Wt::WModelIndex & index, Wt::WFlags< Wt::ViewItemRenderFlag > flags )
  89     {
  90       auto w =
  91         Wt::WItemDelegate::update( widget, index, flags );
  92 
  93       Wt::WText * text = static_cast<Wt::WText *>(w.get());
  94 
  95       if( text )
  96       {
  97 
  98         auto value = Wt::asNumber( index.model()-> data( index.row(), index.column() ) );
  99 
 100         std::string newvalue;
 101 
 102         if( value > 0 )
 103         {
 104           auto item = Rtm::Dbo::Bol::load( value );
 105           newvalue = item-> bolNumber();
 106         }
 107 
 108         text-> setText( newvalue );
 109 
 110       } // endif( text )
 111 
 112       return w;
 113 
 114     } // endupdate(...)

I would appreciate any comments on this.


Replies (3)

RE: WItemDelegate and editable tables - Added by Mark Petryk about 3 years ago

Interestingly... when browsing through the Wt source, I came across a file that does not appear to be in the documentation;

"wt/Dbo/QueryColumn"

Further, this object "QueryColumn" appears in QueryModel tagged as "later", so as I read through the source, it looks like perhaps this is the beginning of hooks for a combo-box solution to the table views?

That would be cool!

RE: WItemDelegate and editable tables - Added by Mark Petryk about 3 years ago

When I said

"However, also, the WItemDelegate::update function never gets called when I update some other column."

That's not true. update() is being called, when I click on some other column.

RE: WItemDelegate and editable tables - Added by Mark Petryk about 3 years ago

Ok, Sorry for the noise! I have sorted out my issues.

This is my new update function, and it does appear to work correctly;

 108     virtual std::unique_ptr<Wt::WWidget> update
 109     (
 110      Wt::WWidget * widget,
 111      const Wt::WModelIndex & index,
 112      Wt::WFlags< Wt::ViewItemRenderFlag > flags
 113     )
 114     {
 115       std::unique_ptr< Wt::WWidget > retVal;
 116 
 117       if( !widget )
 118       {
 119         retVal = Wt::WItemDelegate::update( widget, index, flags );
 120 
 121         widget = retVal.get();
 122       }
 123 
 124       Wt::WText * text = dynamic_cast<Wt::WText *>( widget );
 125 
 126       if( text )
 127       {
 128         auto value = Wt::asNumber( index.model()-> data( index.row(), index.column() ) );
 129 
 130         std::string newvalue;
 131 
 132         if( value > 0 )
 133         {
 134           auto item = Rtm::Dbo::Bol::load( value );
 135           newvalue = item-> bolNumber();
 136         }
 137 
 138         text-> setText( newvalue );
 139 
 140       } // endif( text )
 141 
 142       return retVal;
 143 
 144     } // endupdate(...)
    (1-3/3)