· 7 years ago · Nov 25, 2018, 10:06 AM
1CREATE TABLE IF NOT EXISTS `a_table` (
2 `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
3 `name` VARCHAR(190) NOT NULL DEFAULT 'New name',
4 `anInteger` INT NOT NULL DEFAULT '123',
5 `aBoolean` BOOLEAN NOT NULL DEFAULT TRUE
6 PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`));"
7
8<template>
9 <el-form
10 :model="aTable"
11 ref="tableForm">
12 <el-row>
13 <el-col :span="4">
14 <el-form-item
15 label="Name"
16 prop="name">
17 <el-input
18 v-model="aTable.name">
19 </el-input>
20 </el-form-item>
21 </el-col>
22 <el-col :span="2">
23 <el-form-item label="BoolVal" prop="aBoolean">
24 <el-switch v-model="aTable.aBoolean"></el-switch>
25 </el-form-item>
26 </el-col>
27 <el-col :span="8">
28 <el-form-item label="NumberVal" prop="anInteger">
29 <el-input-number v-model="aTable.anInteger"></el-date-picker>
30 </el-form-item>
31 </el-col>
32 </el-row>
33 </el-form>
34</template>
35
36<script>
37export default {
38 props: {
39 aTable: {
40 type: Object,
41 default: function() {
42 return {};
43 }
44 }
45 },
46 data() {
47 return {
48 };
49 },
50 methods: {
51 }
52};
53</script>
54
55//AJAX and other glue code omitted
56
57static function ajax_get_table()
58{
59 global $wpdb;
60 $response = $wpdb->get_results("SELECT * FROM `a_table` WHERE 1=1 ORDER BY `id`");
61
62 // MYSQLI_OPT_INT_AND_FLOAT_NATIVE Hack
63 // mysql is returning each and every integer/boolean as a string
64 // This hack is needed to retrieve correct data type
65 array_walk($response, function(&$item, $key) {
66 array_walk($item, function(&$subitem, $subkey) {
67 // Integers
68 if (in_array($subkey, ['id', 'anInteger'])) $subitem = intval($subitem);
69 // Boolenas
70 if (in_array($subkey, ['aBoolean'])) $subitem = boolval($subitem);
71 });
72 });
73
74 // AJAX termination
75 wp_send_json($response);
76 die();
77}