jxdnsong
2020-10-23 a7929e6b3ec9ac17233f39e55a2b8ac63ea75f42
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Test Tree Grid</title>
        <meta name="viewport" content="width=570">
        <style>
            @import "../../dojo/resources/dojo.css";
            @import "../css/dgrid.css";
            @import "../css/skins/claro.css";
            .dgrid {
                width: 700px;
                margin: 10px;
            }
 
            .field-bool {
                width: 4em;
            }
 
            #treeGrid .field-type {
                width: 5em;
            }
 
            #treeSelector .selector {
                width: 2em;
            }
        </style>
        <script src="../../dojo/dojo.js"
            data-dojo-config="async: true"></script>
        <script>
            require(["dojo/on", "dgrid/OnDemandGrid","dgrid/Tree","dgrid/Editor", "dgrid/Keyboard",
                    "dgrid/Selection", "dgrid/Selector", "dgrid/ColumnSet", "dojo/_base/declare",
                    "dgrid/test/data/createHierarchicalStore", "dgrid/test/data/hierarchicalCountryData",
                    "dgrid/test/data/testTopHeavyHierarchicalStore", "dojo/domReady!"],
                function(
                    on, Grid, Tree, Editor, Keyboard, Selection, Selector, ColumnSet, declare,
                    createHierarchicalStore, hierarchicalCountryData, testTopHeavyHierarchicalStore
                    ){
                    var count = 0; // for incrementing edits from button under 1st grid
 
                    function byId(id){
                        return document.getElementById(id);
                    }
 
                    function nbspFormatter(value){
                        // returns "&nbsp;" for blank content, to prevent cell collapsing
                        return value === undefined || value === "" ? "&nbsp;" : value;
                    }
 
                    var StandardGrid = declare([Grid, Keyboard, Selection, Selector, Editor, Tree]);
                    var testCountryStore = createHierarchicalStore({ data: hierarchicalCountryData }, true);
 
                    var treeGrid = window.treeGrid = new StandardGrid({
                        collection: testCountryStore,
                        columns: [
                            {renderExpando: true, label: "Name", field:"name", sortable: false},
                            {label: "Visited", field: "bool", sortable: false, editor: "checkbox"},
                            {label:"Type", field:"type", sortable: false},
                            {label:"Population", field:"population"},
                            {label:"Timezone", field:"timezone"}
                        ]
                    }, "treeGrid");
 
                    on(byId("filter"), "click", function(){
                        var collection = testCountryStore.root.filter({ name: /a$/ });
                        collection.root = collection;
                        treeGrid.set("collection", collection.getRootCollection());
                    });
                    on(byId("clearFilter"), "click", function(){
                        treeGrid.set("collection", testCountryStore);
                    });
 
                    on(byId("save"), "click", function(){
                        treeGrid.save();
                    });
                    on(byId("revert"), "click", function(){
                        treeGrid.revert();
                    });
                    on(byId("add-country"), "click", function(){
                        testCountryStore.put({ id: 'GR', name:'Greece', type:'country', parent: 'EU'});
                    });
                    on(byId("remove-country"), "click", function(){
                        testCountryStore.remove('GR');
                    });
                    on(byId("add-continent"), "click", function(){
                        testCountryStore.put({ id: 'NE', name:'My New Europe', type:'continent'});
                        testCountryStore.put({ id: 'NI', name:'My New Italy', type:'country', parent: 'NE'});
                    });
                    on(byId("delete"), "click", function(){
                        for(var id in treeGrid.selection){
                            console.log("id: ", id);
                            testCountryStore.remove(id);
                        }
                    });
                    on(byId("edit-continent"), "click", function(){
                        var item = testCountryStore.getSync("EU");
                        item.name = "Europe Edit " + (++count);
                        testCountryStore.put(item);
                    });
                    on(byId("move-rome-na"), "click", function(){
                        var item = testCountryStore.getSync("Rome");
                        item.parent = "NA";
                        testCountryStore.put(item);
                    });
 
                    function getTreeSelectorColumns(){
                        return [
                            {selector: 'checkbox', className: "selector"},
                            {
                                renderExpando: true,
                                label: "Name",
                                field: "name",
                                formatter: function(value){
                                    return "<strong>" + value + "</strong>";
                                },
                                sortable: false
                            },
                            {label: "Visited", field: "bool", sortable: false, editor: "checkbox"},
                            {label:"Type", field:"type", sortable: false},
                            {label:"Population", field:"population"},
                            {label:"Timezone", field:"timezone"}
                        ];
                    }
 
                    window.treeSelector = new StandardGrid({
                        collection: testCountryStore,
                        selectionMode: "none",
                        allowSelectAll: true,
                        columns: getTreeSelectorColumns()
                    }, "treeSelector");
 
                    // Test resetting column definition on this grid, since it exercises
                    // one of each type of OOTB column plugin.
                    on(byId("resetColumns"), "click", function(){
                        treeSelector.set("columns", getTreeSelectorColumns());
                    });
 
                    window.treeTopHeavy = new StandardGrid({
                        collection: testTopHeavyHierarchicalStore,
                        columns: {
                            abbreviation: { renderExpando: true, label: "Abbreviation", field: "abbreviation" },
                            name: "Name"
                        }
                    }, "treeTopHeavy");
 
                    window.treeSubRows = new StandardGrid({
                        collection: testCountryStore,
                        subRows: [[
                            {renderExpando: true, label:"Name", field:"name", sortable: false},
                            {label:"Type", field:"type", sortable: false}
                        ], [
                            {label:"Population", field:"population"},
                            {label:"Timezone", field:"timezone"}
                        ]]
                    }, "treeSubRows");
 
                    window.treeColumnSets = new (declare([Grid, Keyboard, Selection, ColumnSet, Tree]))({
                        collection: testCountryStore,
                        columnSets: [[
                            [ {renderExpando: true, label:"Name", field:"name", sortable: false} ],
                            [ {label:"Type", field:"type", sortable: false} ]
                        ], [
                            [
                                {label:"Population", field:"population", formatter:nbspFormatter},
                                {label:"Timezone", field:"timezone", formatter:nbspFormatter}
                            ], [
                                {label:"Area", field:"area", colSpan:2, formatter:nbspFormatter}
                            ]
                        ]]
                    }, "treeColumnSets");
                });
        </script>
    </head>
    <body class="claro">
        <h2>Lazy-loading tree grid, with store functionality</h2>
        <div id="treeGrid"></div>
        <div>
            <button id="filter">Show only entries ending in "a"</button>
            <button id="clearFilter">Show all</button>
        </div>
        <div>
            <button id="save">Save</button>
            <button id="revert">Revert</button>
            <button id="add-continent">Add Continent</button>
            <button id="add-country">Add Greece To Europe</button>
            <button id="remove-country">Remove Greece</button>
            <button id="delete">Delete Selected</button>
            <button id="edit-continent">Edit Europe</button>
            <button id="move-rome-na">Move Rome to North America</button>
        </div>
 
        <h2>Tree grid with formatter on tree column, and a checkbox selector to test select-all on children</h2>
        <div id="treeSelector"></div>
        <button id="resetColumns">Reset Columns</button>
        <h2>Tree grid with more parents than children</h2>
        <div id="treeTopHeavy"></div>
        <h2>Tree grid with subRows</h2>
        <div id="treeSubRows"></div>
        <h2>Tree grid with columnSets</h2>
        <div id="treeColumnSets"></div>
    </body>
</html>