Przykład jest oparty na Frameworku7 w wersji piątej.

Plik app.js

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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import $$ from 'dom7';
import Framework7 from 'framework7/framework7.esm.bundle.js';

// Import F7 Styles
import 'framework7/css/framework7.bundle.css';

// Import Icons and App Custom Styles
import '../css/icons.css';
import '../css/app.css';
// Import Cordova APIs
import cordovaApp from './cordova-app.js';
// Import Routes
import routes from './routes.js';


var database = {
  db: null,
  shortName: "BazaFX",
  version: "1.0",
  displayName: "BazaFX",
  maxSize: 65535,

  // wywoływana kiedy pojawi się błąd w połączeniu do bazy:
  errorHandler: function (error) {
    //alert("Błąd: " + error.message + " kod błędu: " + error.code);
    console.log(error);
  },

  // Funkcja wywoływana po udanej transakcji z bazą
  successCallBack: function () {
    //alert("Debuger: sukces!");
    console.log("db ok");
  },

  nullHandler: function () {},
 
  //Funkcja wywoływana po starcie apki
  init: function () {
    if (!window.openDatabase) {
      app.dialog.alert("Twoje urządzenie nie obsługuje SQLite!");
      return;
    }
    this.db = openDatabase(
      this.shortName,
      this.version,
      this.displayName,
      this.maxSize
    ); //tworzy połączenie z bazą
    this.db.transaction(
      function (tx) {
        tx.executeSql(
          "CREATE TABLE IF NOT EXISTS Cars(id INTEGER NOT NULL PRIMARY KEY, marka TEXT NOT NULL, model TEXT NOT NULL, kolor TEXT NOT NULL, rocznik INTEGER NOT NULL, cena INTEGER NOT NULL);",
          //"DROP TABLE Cars;",
          [],
          this.nullHandler,
          this.errorHandler
        );
      }.bind(this),
      this.errorHandler,
      this.successCallBack
    );
    this.db.transaction(
      function (tx) {
        tx.executeSql(
          "CREATE TABLE IF NOT EXISTS Drivers(id INTEGER NOT NULL PRIMARY KEY, pesel TEXT NOT NULL, imie TEXT NOT NULL, nazwisko TEXT NOT NULL, adres TEXT NOT NULL);",
          //"DROP TABLE Drivers;",
          [],
          this.nullHandler,
          this.errorHandler
        );
      }.bind(this),
      this.errorHandler,
      this.successCallBack
    );
    this.db.transaction(
      function (tx) {
        tx.executeSql(
          "CREATE TABLE IF NOT EXISTS Posiada(id INTEGER NOT NULL PRIMARY KEY, pesel TEXT NOT NULL, idCar INTEGER NOT NULL, FOREIGN KEY (pesel) REFERENCES Drivers(pesel), FOREIGN KEY (idCar) REFERENCES Cars(id));",
          //"DROP TABLE Posiada;",
          [],
          this.nullHandler,
          this.errorHandler
        );
      }.bind(this),
      this.errorHandler,
      this.successCallBack
    );
  },
 
  GetCars: function(callback) {
   if (!window.openDatabase) {
    alert('To urządzenie nie obsługuje SQLite!');
    return;
   }
   this.db.transaction(function(transaction) {
     transaction.executeSql('SELECT * FROM Cars;', [],
       function(transaction, result) {
         callback(result);
       }.bind(this),this.errorHandler);
   }.bind(this),this.errorHandler,this.nullHandler);
   return;
  },

  GetDrivers: function(callback) {
    if (!window.openDatabase) {
     alert('To urządzenie nie obsługuje SQLite!');
     return;
    }
    this.db.transaction(function(transaction) {
      transaction.executeSql('SELECT * FROM Drivers;', [],
        function(transaction, result) {
          callback(result);
        }.bind(this),this.errorHandler);
    }.bind(this),this.errorHandler,this.nullHandler);
    return;
   },
 
  AddCar: function(marka,model,kolor,rocznik,cena) {
   if (!window.openDatabase) {
     alert('To urządzenie nie obsługuje SQLite!');
     return;
   }
 
   this.db.transaction(function(transaction) {
     transaction.executeSql('INSERT INTO Cars(marka,model,kolor,rocznik,cena) VALUES (?,?,?,?,?)',[marka,model,kolor,rocznik,cena],
     this.nullHandler,this.errorHandler);
   }.bind(this));
 
   this.GetCars(function(){});
   return false;
  },

  AddDriver: function(pesel,imie,nazwisko,adres) {
    if (!window.openDatabase) {
      alert('To urządzenie nie obsługuje SQLite!');
      return;
    }
   
    this.db.transaction(function(transaction) {
      transaction.executeSql('INSERT INTO Drivers(pesel,imie,nazwisko,adres) VALUES (?,?,?,?)',[pesel,imie,nazwisko,adres],
      this.nullHandler,this.errorHandler);
    }.bind(this));
   
    this.GetDrivers(function(){});
    return false;
  },

  AddConnect: function(pesel, idCar) {
    if (!window.openDatabase) {
      alert('To urządzenie nie obsługuje SQLite!');
      return;
    }
   
    this.db.transaction(function(transaction) {
      transaction.executeSql('INSERT INTO Posiada(pesel,idCar) VALUES (?,?)',[pesel, idCar],
      this.nullHandler,this.errorHandler);
    }.bind(this));
   
    this.GetDrivers(function(){});
    return false;
  },

  GetConnects: function(callback) {
    if (!window.openDatabase) {
      alert('To urządzenie nie obsługuje SQLite!');
      return;
     }

     this.db.transaction(function(transaction) {
      transaction.executeSql('SELECT * FROM Posiada;', [],
        function(transaction, result) {
          console.log(result);
        }.bind(this),this.errorHandler);
     }.bind(this),this.errorHandler,this.nullHandler);

     this.db.transaction(function(transaction) {
       transaction.executeSql('SELECT * FROM Posiada INNER JOIN cars ON Posiada.idCar = Cars.id INNER JOIN Drivers ON Posiada.pesel = Drivers.pesel;', [],
         function(transaction, result) {
           callback(result);
         }.bind(this),this.errorHandler);
     }.bind(this),this.errorHandler,this.nullHandler);
     return;
  }
}


var app = new Framework7({
  root: '#app', // App root element
  id: 'io.framework7.myapp', // App bundle ID
  name: 'test', // App name
  theme: 'auto', // Automatic theme detection
  // App root data
  data: function () {
    return {
      user: {
        firstName: 'John',
        lastName: 'Doe',
      },

    };
  },
  // App root methods
  methods: {
    helloWorld: function () {
      app.dialog.alert('Hello World!');
    },
  },
  // App routes
  routes: routes,
  // Enable panel left visibility breakpoint
  panel: {
    leftBreakpoint: 960,
  },


  // Input settings
  input: {
    scrollIntoViewOnFocus: Framework7.device.cordova && !Framework7.device.electron,
    scrollIntoViewCentered: Framework7.device.cordova && !Framework7.device.electron,
  },
  // Cordova Statusbar settings
  statusbar: {
    overlay: Framework7.device.cordova && Framework7.device.ios || 'auto',
    iosOverlaysWebView: true,
    androidOverlaysWebView: false,
  },
  on: {
    init: function () {
      var f7 = this;
      if (f7.device.cordova) {
        // Init cordova APIs (see cordova-app.js)
        cordovaApp.init(f7);
      }

      database.init();
    },
  },
});

//Dodaj samochód
$$(document).on('page:init', '.page[data-name="dodajS"]', function (e) {

  $$('#dodaj').on('click', () => {
    let marka = $$('#marka').val();
    let model = $$('#model').val();
    let kolor = $$('#kolor').val();
    let rocznik = $$('#rocznik').val();
    let cena = $$('#cena').val();
    database.AddCar(marka,model,kolor,rocznik,cena);
    app.dialog.alert('Dodano');
  });
});


//Dodaj kierowce
$$(document).on('page:init', '.page[data-name="dodajK"]', function (e) {

  $$('#dodajK').on('click', () => {
    let pesel = $$('#pesel').val();
    let imie = $$('#imie').val();
    let nazwisko = $$('#nazwisko').val();
    let adres = $$('#adres').val();
    database.AddDriver(pesel,imie,nazwisko,adres);
    app.dialog.alert('Dodano');
  });
});

//Dodaj połączenie
$$(document).on('page:init', '.page[data-name="connect"]', function (e) {

  let drivers = document.getElementById("drivers");
  let cars = document.getElementById("cars");

  database.GetDrivers(function(result) {
    console.log(result);
    let html = "";
    for(let i=0;i<result.rows.length;i++) {
      let row = result.rows.item(i);
      html += `<option value="${row.pesel}">${row.imie} ${row.nazwisko}</option>`;
    }

    drivers.innerHTML = html;
  });


  database.GetCars(function(result) {
    console.log(result);
    let html = "";
    for(let i=0;i<result.rows.length;i++) {
      let row = result.rows.item(i);
      html += `<option value="${row.id}">${row.marka} ${row.model}</option>`;
    }

    cars.innerHTML = html;
  });

  $$('#polacz').on('click', () => {
    let pesel = $$('#drivers').val();
    let idCar = $$('#cars').val();
    database.AddConnect(pesel, idCar);
    app.dialog.alert('Dodano');
  });
});

//Przeglądaj
$$(document).on('page:init', '.page[data-name="view"]', function (e) {
  console.log("PAGE = cars")
  database.GetCars(function(result) {
    console.log(result);
    let html = "";
    for (let i = 0; i < result.rows.length; i++) {
      let row = result.rows.item(i);
      html += `<li>Marka: ${row.marka}, model: ${row.model}, kolor: ${row.kolor}, rocznik: ${row.rocznik}, cena: ${row.cena}</li>`;
    }

    $$("#cars-list").html(html);
  });


  database.GetDrivers(function(result) {
    console.log(result);
    let html = "";
    for (let i = 0; i < result.rows.length; i++) {
      let row = result.rows.item(i);
      html += `<li>PESEL: ${row.pesel}, imię: ${row.imie}, nazwisko: ${row.nazwisko}, adres: ${row.adres}</li>`;
    }

    $$("#drivers-list").html(html);
  });


  database.GetConnects(function(result) {
    console.log(result);
    let html = "";
    for (let i = 0; i < result.rows.length; i++) {
      let row = result.rows.item(i);
      html += `<li>Osoba (pesel: ${row.pesel}) ${row.imie} ${row.nazwisko} posiada ${row.marka} ${row.model}</li>`;
    }

    $$("#connect-list").html(html);
  });
});

Strona z dodawaniem kierowcy:

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
<template>
  <div class="page" data-name="dodajK">
    <div class="navbar">
      <div class="navbar-inner sliding">
        <div class="left">
          <a href="#" class="link back">
            <i class="icon icon-back"></i>
            <span class="if-not-md">Back</span>
          </a>
        </div>
        <div class="title">Dodaj kierowce</div>
      </div>
    </div>
    <div class="page-content">
      <div class="block-title">Podaj dane</div>
      <div class="list no-hairlines-md">
        <form id="dodajForm">
          <ul>
            <li>
              <div class="item-content item-input">
                <div class="item-inner">
                  <div class="item-title item-label">PESEL</div>
                  <div class="item-input-wrap">
                    <input type="text" name="pesel" placeholder="PESEL" id="pesel" />
                  </div>
                </div>
              </div>
            </li>
            <li>
              <div class="item-content item-input">
                <div class="item-inner">
                  <div class="item-title item-label">Imię</div>
                  <div class="item-input-wrap">
                    <input type="text" name="imie" placeholder="Imię" id="imie" />
                  </div>
                </div>
              </div>
            </li>
            <li>
              <div class="item-content item-input">
                <div class="item-inner">
                  <div class="item-title item-label">Nazwisko</div>
                  <div class="item-input-wrap">
                    <input type="text" name="nazwisko" placeholder="Nazwisko" id="nazwisko" />
                  </div>
                </div>
              </div>
            </li>
            <li>
              <div class="item-content item-input">
                <div class="item-inner">
                  <div class="item-title item-label">Adres</div>
                  <div class="item-input-wrap">
                    <input type="text" name="adres" placeholder="Adres" id="adres" />
                  </div>
                </div>
              </div>
            </li>
          </ul>
          <p class="row" style="width:90%;margin:0 auto;">
            <a href="#" class="col button button-fill" id="dodajK">Dodaj</a>
          </p>
        </form>
      </div>
    </div>
  </div>
</template>
<script>
  export default {};
</script>

Strona z dodawaniem samochodu

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
<template>
  <div class="page" data-name="dodajS">
    <div class="navbar">
      <div class="navbar-inner sliding">
        <div class="left">
          <a href="#" class="link back">
            <i class="icon icon-back"></i>
            <span class="if-not-md">Back</span>
          </a>
        </div>
        <div class="title">Dodaj samochód</div>
      </div>
    </div>
    <div class="page-content">
      <div class="block-title">Podaj dane</div>
      <div class="list no-hairlines-md">
        <form id="dodajForm">
          <ul>
            <li>
              <div class="item-content item-input">
                <div class="item-inner">
                  <div class="item-title item-label">Marka</div>
                  <div class="item-input-wrap">
                    <input type="text" name="marka" placeholder="Marka" id="marka" />
                  </div>
                </div>
              </div>
            </li>
            <li>
              <div class="item-content item-input">
                <div class="item-inner">
                  <div class="item-title item-label">Model</div>
                  <div class="item-input-wrap">
                    <input type="text" name="model" placeholder="Model" id="model" />
                  </div>
                </div>
              </div>
            </li>
            <li>
              <div class="item-content item-input">
                <div class="item-inner">
                  <div class="item-title item-label">Kolor</div>
                  <div class="item-input-wrap">
                    <input type="text" name="kolor" placeholder="Kolor" id="kolor" />
                  </div>
                </div>
              </div>
            </li>
            <li>
              <div class="item-content item-input">
                <div class="item-inner">
                  <div class="item-title item-label">Rocznik</div>
                  <div class="item-input-wrap">
                    <input type="number" name="rocznik" placeholder="Rocznik" id="rocznik" />
                  </div>
                </div>
              </div>
            </li>
            <li>
              <div class="item-content item-input">
                <div class="item-inner">
                  <div class="item-title item-label">Cena</div>
                  <div class="item-input-wrap">
                    <input type="number" name="cena" placeholder="Cena" id="cena" />
                  </div>
                </div>
              </div>
            </li>
          </ul>
          <p class="row" style="width:90%;margin:0 auto;">
            <a href="#" class="col button button-fill" id="dodaj">Dodaj</a>
          </p>
        </form>
      </div>
    </div>
  </div>
</template>
<script>
  export default {};
</script>

Strona z tworzeniem połączenia

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
<template>
  <div class="page" data-name="connect">
    <div class="navbar">
      <div class="navbar-inner sliding">
        <div class="left">
          <a href="#" class="link back">
            <i class="icon icon-back"></i>
            <span class="if-not-md">Back</span>
          </a>
        </div>
        <div class="title">Stwórz połączenie</div>
      </div>
    </div>
    <div class="page-content">
      <div class="list no-hairlines-md">
        <form id="dodajForm">
          <ul>
            <li>
              <div class="item-content item-input">
                <div class="item-inner">
                  <div class="item-title item-label">Kierowcy</div>
                  <div class="item-input-wrap">
                    <select name="drivers" id="drivers">
                      <option value="volvo">Volvo</option>
                      <option value="saab">Saab</option>
                      <option value="mercedes">Mercedes</option>
                      <option value="audi">Audi</option>
                    </select>
                  </div>
                </div>
              </div>
            </li>
            <li>
              <div class="item-content item-input">
                <div class="item-inner">
                  <div class="item-title item-label">Samochody</div>
                  <div class="item-input-wrap">
                    <select name="cars" id="cars">
                      <option value="volvo">Volvo</option>
                      <option value="saab">Saab</option>
                      <option value="mercedes">Mercedes</option>
                      <option value="audi">Audi</option>
                    </select>
                  </div>
                </div>
              </div>
            </li>
          </ul>
          <p class="row" style="width:90%;margin:0 auto;">
            <a href="#" class="col button button-fill" id="polacz">Połącz</a>
          </p>
        </form>
      </div>
    </div>
  </div>
</template>
<script>
  export default {};
</script>

Strona z dodanymi informacjami

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
<template>
<div class="page" data-name="view">
  <div class="navbar">
    <div class="navbar-inner sliding">
      <div class="left">
        <a href="#" class="link back">
          <i class="icon icon-back"></i>
          <span class="if-not-md">Back</span>
        </a>
      </div>
      <div class="title">Dodane samochody</div>
    </div>
  </div>
  <div class="page-content">
    <div class="block">
     
      <div class="list">
        <h2>Samochody</h2>
        <ul id="cars-list"></ul>
      </div>

      <div class="list">
        <h2>Kierowcy</h2>
        <ul id="drivers-list"></ul>
      </div>

      <div class="list">
        <h2>Posiada</h2>
        <ul id="connect-list"></ul>
      </div>

    </div>
  </div>
</div>
</template>
<script>
export default {};
</script>

Tagged in: