46 #include <Wt/WAbstractItemModel.h> 47 #include <Wt/WComboBox.h> 49 #include <Wtx/Core/Core.h> 50 #include <Wtx/Util/Util.h> 51 #include <Wtx/Dbo/TableDef.h> 53 #include "FieldDefLookupCombo.h" 56 :
public Wt::WComboBox
65 setNoSelectionEnabled(
true);
66 changed().connect(
this, &SmartComboEdit::on_changed );
67 focussed().connect(
this, &SmartComboEdit::on_focussed );
75 int get_id(
const std::string & value )
79 for(
int i=0; i < m_model-> rowCount(); i++ )
81 auto id = Wt::asNumber(m_model-> index( i, 0 ).data());
82 auto ky = Wt::asString(m_model-> index( i, 1 ).data()).toUTF8();
99 std::string get_string(
int index )
101 if( m_model && index > -1 )
103 for(
int i=0; i < m_model-> rowCount(); i++ )
105 auto id = Wt::asNumber(m_model-> index( i, 0 ).data());
106 auto ky = Wt::asString(m_model-> index( i, 1 ).data()).toUTF8();
137 void loadModel(
int id )
139 if( !m_loaded && m_getModel )
141 setNoSelectionEnabled(
true);
142 m_model = m_getModel( m_sourceID,
id,
"", m_session );
150 std::cout << __FILE__ <<
":" << __LINE__
151 <<
" Loading Model: " <<
id 152 <<
" loaded: " << m_loaded
153 <<
" " << m_sourceID <<
" " << m_targetID
168 setValueText( m_valueText );
180 auto value = Wt::WComboBox::valueText().toUTF8();
189 m_targetID = get_id( value );
193 auto dialog = addChild( std::make_unique<Wt::WDialog>() );
194 dialog-> rejectWhenEscapePressed();
195 dialog-> contents()-> addNew<Wt::WText>(
"cannot put in false data");
203 Wt::WString valueText()
const 205 auto retVal = Wt::WString(
"[null]");
207 if( m_targetID != -1 )
208 retVal = Wt::WString(
"{1}: {2}]").arg( m_table ).arg( m_targetID );
215 void setValueText(
const Wt::WString & value )
217 m_valueText = value.toUTF8();
224 if( m_valueText ==
"[null]" )
229 if( m_valueText.find(
":") != std::string::npos )
231 auto v = Wtx::Core::split( m_valueText,
':' );
233 m_targetID = std::stoi(v.at(1));
234 loadModel(m_targetID);
235 m_valueText = get_string( m_targetID );
241 std::cout << __FILE__ <<
":" << __LINE__
242 <<
"\n loaded: " << m_loaded
243 <<
"\n table: " << m_table
244 <<
"\n value: " << value
245 <<
"\n valueText:" << m_valueText
250 Wt::WComboBox::setValueText( m_valueText );
254 std::shared_ptr<Wt::WAbstractItemModel> (*m_getModel)(
int sid,
int tid,
const std::string & filter,
Wtx::Dbo::Session & session) =
nullptr;
255 bool m_loaded =
false;
260 std::string m_valueText;
262 std::shared_ptr<Wt::WAbstractItemModel> m_model;
298 std::unique_ptr<Wt::WWidget> Wtx::Dbo::FieldDefLookupCombo::createEditWidget(
int sid,
Wtx::Dbo::Session & session )
const 300 auto retVal = std::make_unique< SmartComboEdit >( sid, getLookupModel, session );
301 retVal-> m_table =
"[" + targetTable;
302 retVal-> m_field = fieldName();
305 retVal-> on_focussed();
FieldDefLookupCombo(TableDef &td, const char *fn, int w=-1, int h=-1, Flags f=Flags::None, std::string lb="", std::string ph="", std::string inf="", std::string tt="", int ht=-1)
ctor
Flags
Field Definition Flags.