付延余
2022-12-16 f0f8ee8c4a945adbc742d9bab69382b28ad311fb
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
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>jsGrid - Data Manipulation</title>
    <link rel="stylesheet" type="text/css" href="demos.css" />
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:300,600,400' rel='stylesheet' type='text/css'>
 
    <link rel="stylesheet" type="text/css" href="../css/jsgrid.css" />
    <link rel="stylesheet" type="text/css" href="../css/theme.css" />
 
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.2/themes/cupertino/jquery-ui.css">
    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
    <script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
 
    <script src="db.js"></script>
 
    <script src="../src/jsgrid.core.js"></script>
    <script src="../src/jsgrid.load-indicator.js"></script>
    <script src="../src/jsgrid.load-strategies.js"></script>
    <script src="../src/jsgrid.sort-strategies.js"></script>
    <script src="../src/jsgrid.field.js"></script>
    <script src="../src/fields/jsgrid.field.text.js"></script>
    <script src="../src/fields/jsgrid.field.number.js"></script>
    <script src="../src/fields/jsgrid.field.select.js"></script>
    <script src="../src/fields/jsgrid.field.checkbox.js"></script>
    <script src="../src/fields/jsgrid.field.control.js"></script>
 
    <style>
        .ui-widget *, .ui-widget input, .ui-widget select, .ui-widget button {
            font-family: 'Helvetica Neue Light', 'Open Sans', Helvetica;
            font-size: 14px;
            font-weight: 300 !important;
        }
 
        .details-form-field input,
        .details-form-field select {
            width: 250px;
            float: right;
        }
 
        .details-form-field {
            margin: 30px 0;
        }
 
        .details-form-field:first-child {
            margin-top: 10px;
        }
 
        .details-form-field:last-child {
            margin-bottom: 10px;
        }
 
        .details-form-field button {
            display: block;
            width: 100px;
            margin: 0 auto;
        }
 
        input.error, select.error {
            border: 1px solid #ff9999;
            background: #ffeeee;
        }
 
        label.error {
            float: right;
            margin-left: 100px;
            font-size: .8em;
            color: #ff6666;
        }
    </style>
</head>
<body>
    <h1>Data Manipulation</h1>
    <div id="jsGrid"></div>
 
    <div id="detailsDialog">
        <form id="detailsForm">
            <div class="details-form-field">
                <label for="name">Name:</label>
                <input id="name" name="name" type="text" />
            </div>
            <div class="details-form-field">
                <label for="age">Age:</label>
                <input id="age" name="age" type="number" />
            </div>
            <div class="details-form-field">
                <label for="address">Address:</label>
                <input id="address" name="address" type="text" />
            </div>
            <div class="details-form-field">
                <label for="country">Country:</label>
                <select id="country" name="country">
                    <option value="">(Select)</option>
                    <option value="1">United States</option>
                    <option value="2">Canada</option>
                    <option value="3">United Kingdom</option>
                    <option value="4">France</option>
                    <option value="5">Brazil</option>
                    <option value="6">China</option>
                    <option value="7">Russia</option>
                </select>
            </div>
            <div class="details-form-field">
                <label for="married">Is Married</label>
                <input id="married" name="married" type="checkbox" />
            </div>
            <div class="details-form-field">
                <button type="submit" id="save">Save</button>
            </div>
        </form>
    </div>
 
    <script>
        $(function() {
 
            $("#jsGrid").jsGrid({
                height: "70%",
                width: "100%",
                editing: true,
                autoload: true,
                paging: true,
                deleteConfirm: function(item) {
                    return "The client \"" + item.Name + "\" will be removed. Are you sure?";
                },
                rowClick: function(args) {
                    showDetailsDialog("Edit", args.item);
                },
                controller: db,
                fields: [
                    { name: "Name", type: "text", width: 150 },
                    { name: "Age", type: "number", width: 50 },
                    { name: "Address", type: "text", width: 200 },
                    { name: "Country", type: "select", items: db.countries, valueField: "Id", textField: "Name" },
                    { name: "Married", type: "checkbox", title: "Is Married", sorting: false },
                    {
                        type: "control",
                        modeSwitchButton: false,
                        editButton: false,
                        headerTemplate: function() {
                            return $("<button>").attr("type", "button").text("Add")
                                .on("click", function () {
                                    showDetailsDialog("Add", {});
                                });
                        }
                    }
                ]
            });
 
            $("#detailsDialog").dialog({
                autoOpen: false,
                width: 400,
                close: function() {
                    $("#detailsForm").validate().resetForm();
                    $("#detailsForm").find(".error").removeClass("error");
                }
            });
 
            $("#detailsForm").validate({
                rules: {
                    name: "required",
                    age: { required: true, range: [18, 150] },
                    address: { required: true, minlength: 10 },
                    country: "required"
                },
                messages: {
                    name: "Please enter name",
                    age: "Please enter valid age",
                    address: "Please enter address (more than 10 chars)",
                    country: "Please select country"
                },
                submitHandler: function() {
                    formSubmitHandler();
                }
            });
 
            var formSubmitHandler = $.noop;
 
            var showDetailsDialog = function(dialogType, client) {
                $("#name").val(client.Name);
                $("#age").val(client.Age);
                $("#address").val(client.Address);
                $("#country").val(client.Country);
                $("#married").prop("checked", client.Married);
 
                formSubmitHandler = function() {
                    saveClient(client, dialogType === "Add");
                };
 
                $("#detailsDialog").dialog("option", "title", dialogType + " Client")
                    .dialog("open");
            };
 
            var saveClient = function(client, isNew) {
                $.extend(client, {
                    Name: $("#name").val(),
                    Age: parseInt($("#age").val(), 10),
                    Address: $("#address").val(),
                    Country: parseInt($("#country").val(), 10),
                    Married: $("#married").is(":checked")
                });
 
                $("#jsGrid").jsGrid(isNew ? "insertItem" : "updateItem", client);
 
                $("#detailsDialog").dialog("close");
            };
 
        });
    </script>
</body>
</html>