45 #include <Wt/WAbstractItemModel.h> 46 #include <Wt/WContainerWidget.h> 47 #include <Wt/WDialog.h> 48 #include <Wt/WLineEdit.h> 49 #include <Wt/WString.h> 50 #include <Wt/WSuggestionPopup.h> 53 #include <Wtx/Core/Core.h> 54 #include <Wtx/Util/Util.h> 56 #include "FieldDefLookupSuggest.h" 77 :
public Wt::WSuggestionPopup
81 SmartSuggest(
const Wt::WSuggestionPopup::Options & options )
82 : Wt::WSuggestionPopup( options )
90 :
public Wt::WLineEdit
97 EventSignalBase& b = edit->keyPressed();
99 connectObjJS(b,
"editKeyDown");
100 connectObjJS(edit->keyWentDown(),
"editKeyDown");
101 connectObjJS(edit->keyWentUp(),
"editKeyUp");
102 connectObjJS(edit->blurred(),
"delayHide");
104 if (triggers.test(PopupTrigger::Editing))
105 edit->addStyleClass(
"Wt-suggest-onedit");
107 if (triggers.test(PopupTrigger::DropDownIcon)) {
108 edit->addStyleClass(
"Wt-suggest-dropdown");
109 EventSignalBase& c = edit->clicked();
110 connectObjJS(c,
"editClick");
111 connectObjJS(edit->mouseMoved(),
"editMouseMove");
123 changed().connect(
this, &SmartLineEdit::on_changed );
124 textInput().connect(
this, &SmartLineEdit::on_textInput );
126 setAutoComplete(
false );
132 std::cout << __FILE__ <<
":" << __LINE__ <<
" " << std::endl;
142 if( m_model-> rowCount() < 100 )
144 std::cout << __FILE__ <<
":" << __LINE__ <<
" " << std::endl;
146 Wt::WSuggestionPopup::Options options =
157 removeChild( m_popup );
159 m_popup = addChild( std::make_unique<Wt::WSuggestionPopup>( options ) );
160 m_popup-> forEdit(
this );
162 m_popup-> setModel( m_model );
163 m_popup-> setModelColumn(1);
166 m_popup-> activated().connect( [=](
int index, Wt::WFormWidget * widget )
168 std::cout << __FILE__ <<
":" << __LINE__ <<
" " << std::endl;
170 m_targetID = Wt::asNumber( m_model-> index( index, 0 ).data() );
171 m_valueText = Wt::asNumber( m_model-> index( index, 1 ).data() );
182 int get_id(
const std::string & value )
184 std::cout << __FILE__ <<
":" << __LINE__ <<
" " << value << std::endl;
188 std::cout << __FILE__ <<
":" << __LINE__
189 <<
" " << m_model-> rowCount()
193 for(
int i=0; i < m_model-> rowCount(); i++ )
195 auto id = Wt::asNumber(m_model-> index( i, 0 ).data());
196 auto ky = Wt::asString(m_model-> index( i, 1 ).data()).toUTF8();
198 std::cout << __FILE__ <<
":" << __LINE__ <<
" " << ky << std::endl;
208 std::cout << __FILE__ <<
":" << __LINE__ <<
" " << std::endl;
217 std::string get_string(
int index )
219 std::cout << __FILE__ <<
":" << __LINE__
220 <<
"\n field: " << m_field
221 <<
"\n index: " << index
222 <<
"\n model: " << (
void*)m_model.get()
225 if( m_model && index > -1 )
227 for(
int i=0; i < m_model-> rowCount(); i++ )
229 auto id = Wt::asNumber(m_model-> index( i, 0 ).data());
230 auto ky = Wt::asString(m_model-> index( i, 1 ).data()).toUTF8();
241 std::cout << __FILE__ <<
":" << __LINE__ <<
" " << std::endl;
253 auto value = Wt::WLineEdit::valueText().toUTF8();
257 m_model = m_getModel( m_sourceID, -1, value, m_session );
261 std::cout << __FILE__ <<
":" << __LINE__
262 <<
"\n value:" << value
263 <<
"\n targetID:" << m_targetID
264 <<
"\n valueText:" << m_valueText
265 <<
"\n model rows:" << m_model-> rowCount()
273 std::cout << __FILE__ <<
":" << __LINE__ <<
" " << std::endl;
275 auto value = Wt::WLineEdit::valueText().toUTF8();
283 removeChild( m_popup );
290 m_targetID = get_id( value );
293 std::cout << __FILE__ <<
":" << __LINE__
294 <<
"\n targetID:" << m_targetID
295 <<
"\n valueText:" << m_valueText
296 <<
"\n model rows:" << m_model-> rowCount()
301 auto dialog = addChild( std::make_unique<Wt::WDialog>() );
302 dialog-> rejectWhenEscapePressed();
303 dialog-> contents()-> addNew<Wt::WText>(
"cannot put in false data");
310 Wt::WString valueText()
const 312 auto retVal = Wt::WString(
"[null]");
314 if( m_targetID != -1 )
315 retVal = Wt::WString(
"{1}: {2}]").arg( m_table ).arg( m_targetID );
317 std::cout << __FILE__ <<
":" << __LINE__
318 <<
"\n field" << m_field
319 <<
"\n retVal" << retVal
340 void setValueText(
const Wt::WString & value )
342 m_valueText = value.toUTF8();
349 if( m_valueText ==
"[null]" )
361 if( m_valueText.find(
":") != std::string::npos )
363 auto v = Wtx::Core::split( m_valueText,
':' );
365 m_targetID = std::stoi(v.at(1));
366 m_model = m_getModel( m_sourceID, m_targetID,
"", m_session );
367 m_valueText = get_string( m_targetID );
375 Wt::WLineEdit::setValueText( m_valueText );
383 std::string m_valueText;
385 std::shared_ptr<Wt::WAbstractItemModel> (*m_getModel)(
int sid,
int tid,
const std::string & filter,
Wtx::Dbo::Session & session) =
nullptr;
386 std::shared_ptr<Wt::WAbstractItemModel> m_model =
nullptr;
387 Wt::WSuggestionPopup * m_popup =
nullptr;
392 std::unique_ptr<Wt::WWidget> Wtx::Dbo::FieldDefLookupSuggest::createEditWidget(
int sid,
Wtx::Dbo::Session & session )
const 394 auto retVal = std::make_unique<SmartLineEdit>( sid, getLookupModel, session );
396 retVal-> m_field = fieldName();
FieldDefLookupSuggest(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.