Project

General

Profile

Bug #5882 » table_test.cpp

Kayra Akman, 08/18/2017 07:55 PM

 
1
#include <Wt/WServer>
2
#include <Wt/WIOService>
3
#include <Wt/WApplication>
4
#include <Wt/WEnvironment>
5
#include <Wt/WTabWidget>
6
#include <Wt/WTableView>
7
#include <Wt/WStandardItem>
8
#include <Wt/WStandardItemModel>
9
#include <Wt/WPushButton>
10
#include <Wt/WLabel>
11
#include <Wt/WLineEdit>
12
#include <Wt/WBootstrapTheme>
13
#include <Wt/WCssDecorationStyle>
14
#include <Wt/WBreak>
15

    
16
struct RowData {
17
    RowData(){}
18
    RowData(const std::string &s, const std::string &d, const std::string &c, const std::string &u, unsigned int i, int t, int r) :
19
        cell1(s),
20
        cell2(d),
21
        cell3(c),
22
        cell4(u),
23
        value1(i), value2(t), value3(r)
24
    {}
25

    
26
    std::string cell1;
27
    std::string cell2;
28
    std::string cell3;
29
    std::string cell4;
30
    unsigned int value1;
31
    int value2;
32
    int value3;
33
};
34

    
35
class DataTable : public Wt::WContainerWidget
36
{
37
public:
38
    DataTable(WContainerWidget *parent = 0);
39

    
40
private:
41
    Wt::WTableView *table;
42
    Wt::WStandardItemModel *model;
43
    Wt::WPushButton *first;
44

    
45
    Wt::WLineEdit *searchLineEdit;
46
    Wt::WPushButton *search;
47

    
48
    void initializeModel();
49
    void firstPage();
50

    
51
    void insertData(const std::vector<RowData> &data);
52

    
53
    void searchText();
54
    void onRowSelected();
55
    void onClicked(const Wt::WModelIndex &index, const Wt::WMouseEvent &);
56
    void setText(const Wt::WString &t);
57

    
58
    enum { ROWS_PER_PAGE = 40 };
59
};
60

    
61
std::vector<RowData> dummyData(int n)
62
{
63
    std::vector<RowData> data;
64
    data.reserve(n);
65

    
66
    static const RowData le{"Cell 1", "Cell 2", "Cell 3", "Cell 4", 0, 0, 14};
67

    
68
    for (int i = 0; i < n; ++i)
69
        data.push_back(le);
70

    
71
    return data;
72
}
73

    
74
DataTable::DataTable(WContainerWidget *parent)
75
    : Wt::WContainerWidget(parent)
76
    , table(new Wt::WTableView)
77
    , model(0)
78
    , first(new Wt::WPushButton(" << "))
79
    , searchLineEdit(new Wt::WLineEdit)
80
    , search(new Wt::WPushButton("Search"))
81
{
82
    first->clicked().connect(this, &DataTable::firstPage);
83

    
84
    search->clicked().connect(this, &DataTable::searchText);
85
    searchLineEdit->enterPressed().connect(this, &DataTable::searchText);
86

    
87

    
88
    initializeModel();
89
    table->setMinimumSize(850, Wt::WLength::Auto);
90
    table->setModel(model);
91
    table->setColumnWidth(4, 240);
92
    table->setHeaderClickSortEnabled(true);
93
    table->setSelectionMode(Wt::SingleSelection);
94
    table->setSelectionBehavior(Wt::SelectRows);
95
    table->clicked().connect(this, &DataTable::onClicked);
96
    table->setAlternatingRowColors(true);
97
    table->decorationStyle().setBorder(Wt::WBorder(), Wt::All);
98

    
99
    auto form = new Wt::WContainerWidget;
100
    form->setStyleClass("form-horizontal");
101
    auto controlGroup = new Wt::WContainerWidget;
102
    controlGroup->setStyleClass("control-group");
103

    
104
    controlGroup->addWidget(first);
105

    
106
    controlGroup->addWidget(searchLineEdit);
107
    controlGroup->addWidget(search);
108
    form->addWidget(controlGroup);
109
    addWidget(form);
110

    
111
    addWidget(table);
112

    
113
    setContentAlignment(Wt::AlignLeft);
114

    
115
    firstPage();
116
}
117

    
118
void DataTable::initializeModel()
119
{
120
    if (model == 0) {
121
        model = new Wt::WStandardItemModel(0, 5, this);
122
        std::vector<std::string> headerData;
123
        headerData.push_back(std::string("Title 1"));
124
        headerData.push_back(std::string("Title 2"));
125
        headerData.push_back(std::string("Title 3"));
126
        headerData.push_back(std::string("Title 4"));
127
        headerData.push_back(std::string("Title 5"));
128

    
129
        for (std::vector<std::string>::size_type i = 0; i < headerData.size(); ++i)
130
            model->setHeaderData(i, boost::any(headerData.at(i)));
131
    }
132
}
133

    
134

    
135
void DataTable::firstPage()
136
{
137
    searchLineEdit->setText(Wt::WString::Empty);
138
    insertData(dummyData(ROWS_PER_PAGE));
139
}
140

    
141
void DataTable::insertData(const std::vector<RowData> &data)
142
{
143
    model->removeRows(0, model->rowCount());
144
    for (std::vector<RowData>::size_type i = 0; i < data.size(); ++i) {
145

    
146
        const RowData &e = data.at(i);
147
        std::vector<Wt::WStandardItem*> items;
148
        items.reserve(5);
149

    
150
        items.push_back(new Wt::WStandardItem(Wt::WString(e.cell1)));
151
        items.push_back(new Wt::WStandardItem(Wt::WString(e.cell2)));
152
        items.push_back(new Wt::WStandardItem(Wt::WString(e.cell3)));
153
        items.push_back(new Wt::WStandardItem(Wt::WString(e.cell4)));
154
        items.push_back(new Wt::WStandardItem(Wt::WString("{1}:{2}").arg(e.value2).arg(e.value3)));
155

    
156
        model->appendRow(items);
157
    }
158
}
159

    
160
void DataTable::searchText()
161
{
162
    insertData(dummyData(1000));
163
    first->setEnabled(true);
164
}
165

    
166
void DataTable::onRowSelected()
167
{
168
    Wt::WModelIndexSet indexes = table->selectedIndexes();
169
    if (!indexes.empty()) {
170
        Wt::WModelIndex i = *indexes.begin();
171
        const Wt::WString sn = model->item(i.row(), i.column())->text();
172
        searchLineEdit->setText(sn);
173
        searchLineEdit->setFocus();
174
    }
175
}
176

    
177
void DataTable::onClicked(const Wt::WModelIndex &index, const Wt::WMouseEvent &)
178
{
179
    const Wt::WString t = model->item(index.row(), 0)->text();
180
    setText(t);
181
}
182

    
183
void DataTable::setText(const Wt::WString &t)
184
{
185
    if (!t.empty()) {
186
        searchLineEdit->setText(t);
187
        searchLineEdit->setFocus();
188
    }
189
}
190

    
191

    
192

    
193
class Application : public Wt::WApplication
194
{
195
public:
196
    Application(const Wt::WEnvironment &env)
197
        : Wt::WApplication(env)
198
        , tabs(0)
199
        , dataTable(0)
200
    {
201
        addMetaHeader(Wt::MetaName,"viewport", "width=device-width, initial-scale=1");
202
        Wt::WBootstrapTheme *theme = new Wt::WBootstrapTheme();
203
        theme->setVersion(Wt::WBootstrapTheme::Version2);
204
        theme->setResponsive(true);
205
        setTheme(theme);
206

    
207
        root()->addStyleClass("container");
208
        loadData();
209
        enableUpdates();
210
    }
211

    
212
    void update()
213
    {
214
        delete tabs->parent();
215
        tabs = 0;
216
        dataTable = 0;
217
        loadData();
218
        triggerUpdate();
219
    }
220

    
221
private:
222
    Wt::WTabWidget *tabs;
223
    DataTable *dataTable;
224

    
225
    void loadData()
226
    {
227
        auto *c = new Wt::WContainerWidget(root());
228
        tabs = new Wt::WTabWidget(c);
229
        tabs->setMaximumSize(850, Wt::WLength::Auto);
230
        dataTable = new DataTable;
231
        tabs->addTab(dataTable, Wt::WString("Tab"));
232
    }
233
};
234

    
235
Wt::WApplication *createApplication(const Wt::WEnvironment& env)
236
{
237
    return new Application(env);
238
}
239

    
240
int main(int argc, char **argv)
241
{
242
    Wt::WRun(argc, argv, createApplication);
243
}
244

    
(2-2/2)