wenzheng.yang
2023-04-27 31e64e0e144815e6176c1b6b81eae8dc68688570
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<template>
    <view class="pages">
        <view class="tools">
            <view class="tools-item" @tap="init()">
                {{isConnect?'已连接':'未连接'}}
            </view>
        </view>
        <view class="tab-list-visual">
            <view class="tab-list" >
                <view class="tab-item" :class="thisTableIndex==tableIndex?'this':''" v-for="tableItem,tableIndex in tableList" :key="tableIndex" @click="changeTable(tableIndex, tableItem.name)">
                    {{tableItem.name}}
                </view>
                <view class="tab-item">
                    +
                </view>
            </view>
        </view>
        <view class="toolsbar">
            <view class="tools-item" @tap="addData()">
                增加
            </view>
            <view class="tools-item">
                自定义SQL
            </view>
        </view>
        <view class="table-content-visual">
            <table class="table-content">
                <td class="header" v-for="(cols,cIndex) in currentTableData[0]" :key="'cols-header-'+cIndex" @tap="tdTap(rIndex, cIndex, cols)">
                    <view class="">
                        {{cIndex=='usql_id'?'':cIndex}}
                    </view>
                </td>
                <tr v-for="(row,rIndex) in currentTableData" :key="'rows-'+rIndex">
                    <td :class="cIndex=='usql_id'?'id':'content'" v-for="(cols,cIndex) in row" :key="'cols-'+rIndex+'-'+cIndex" @tap="tdTap(rIndex, cIndex, cols)">
                        <view class="data">
                            <view :class="(rIndex==thisrIndex&&cIndex==thiscIndex&&cIndex!='usql_id')?'hied':'show'">
                                {{cols}}
                            </view>
                            <input :class="(rIndex==thisrIndex&&cIndex==thiscIndex&&cIndex!='usql_id')?'show':'hied'" type="text" v-model="thisValue" @blur="changeValue(rIndex, cIndex, thisValue)">
                        </view>
                    </td>
                </tr>
            </table>
        </view>
    </view>
</template>
<script>
    import { usqlite } from '@/uni_modules/onemue-USQLite/js_sdk/usqlite.js'
    export default {
        data() {
            return {
                isConnect: false,
                consoleText: '',
                tableList: [],
                thisTableIndex: 0,
                thisTableName: '',
                thisSqlModel: null,
                thisrIndex: null,
                thiscIndex: null,
                thisValue: null,
                currentTableData: [],
            }
        },
        onShow() {
            let self = this;
            self.consoleText = usqlite;
        },
        methods:{
            init(){
                usqlite.connect(this.dbOptions, function(e, r){
                if(e){
                    if(e.code == -1402){
                        self.isConnect = true;
                        self.getTable();
                    }else{
                        console.log('this is error');
                        console.log(e); 
                        self.consoleText = e;
                    }
                    return;
                }
                self.getTable();
                self.isConnect = true;
                console.log('链接成功');
            })
            },
            addData(){
                this.currentTableData.push({})
            },
            tdTap(rows, cols, content){
                console.log(rows, cols, content);
                this.thiscIndex = cols;
                this.thisrIndex = rows;
                this.thisValue = content;
            },
            changeTable(index, name){
                this.thisTableIndex = index;
                this.thisTableName = name;
                this.getTableData();
            },
            changeValue(rows, cols, content){
                this.thiscIndex = null;
                this.thisrIndex = null;
                this.thisValue = null;
                let option = {};
                if(this.currentTableData[rows][cols]==content){
                    console.log('no change');
                    return;
                }
                let options = this.currentTableData[rows];
                let str = '';
                let self = this;
                for(var key in options){
                    str += `${key} = '${options[key]}' AND `
                }
                str = str.replace(/AND $/, '');
                option[cols] = content;
                
                this.thisSqlModel.update(str, option, function(e, r){
                    if(e){
                        return;
                    }
                    self.getTableData();
                });
            },
            getTable(){
                let self = this;
                plus.sqlite.selectSql({
                    name: this.dbOptions.name,
                    sql: "select * FROM sqlite_master where type='table'",
                    success(e) {
                        self.tableList = e;
                        if(!self.thisTableName){
                            self.thisTableName = e[self.thisTableIndex].name;
                            self.getTableData();
                        }
                        console.log(e);
                    },
                    fail(e) {
                        console.log(e)
                    }
                })
            },
            getTableData(){
                let self = this;
                let thisTableName = self.thisTableName;
                console.log(self.thisTableName);
                let thisSqlModel = usqlite.model(thisTableName)
                this.thisSqlModel = thisSqlModel;
                this.thisSqlModel.find(function(err, r) {
                    if (err) {
                        console.log(err);
                        return;
                    }
                    
                    self.currentTableData = r;
                    console.log(self.currentTableData);
                })
            }
        }
    }
</script>
<style scoped>
    .tools{
        display: flex;
        position: sticky;
        top: 0;
        line-height: 2em;
        height: 2em;
        background: #FFFFFF;
    }
    .tab-list-visual{
        position: sticky;
        top: 2em;
        line-height: 2em;
        height: 2em;
        background: #FFFFFF;
    }
    .tab-list-visual{
        overflow: auto;
        border: #333333 1px solid;
        border-width: 1px 0  1px 0;
    }
    .tab-list{
        display: flex;
    }
    .tab-list .tab-item{
        padding: 0 4px;
        line-height: 2em;
        border-right: #eee 1px solid;
        white-space: nowrap;
    }
    .tab-list .tab-item:last-child{
        border-right: #eee 1px none;
    }
    .tab-list .tab-item.this{
        background: #eee;
    }
    .toolsbar{
        width: 100%;
        border: #333 1px solid;
        border-top: none;
        display: flex;
    }
    .toolsbar .tools-item{
        padding: 0 4px;
        line-height: 2em;
        border-right: #eee 1px solid;
        white-space: nowrap;
    }
    .toolsbar .tools-item:last-child{
        border-right: #eee 1px none;
    }
    .table-content-visual{
        overflow: auto;
    }
    .table-content {border-collapse:collapse;}
    .table-content td.content,
    .table-content td.header{
        padding: 2px;
        border: #eee 1px solid;
        margin: 0;
        white-space: nowrap;
    }
    .table-content td.content:hover{
        background: #eee;
    }
    .data .show{
        display: block;
    }
    .data .hied{
        display: none;
    }
</style>