回答編集履歴

1

コードを掲載します

2016/09/14 00:45

投稿

退会済みユーザー
test CHANGED
@@ -10,11 +10,131 @@
10
10
 
11
11
 
12
12
 
13
- `alertShowForBarcode0Event``console.log`に置換えて
13
+ - Chumiさんのコード一部変更して動かしてみたものをはっておますね、私の勘違いもあるかもしれませんので。
14
14
 
15
- CSVから読み込んだものとして仮に`csvArray = [[1,'a','A'],[2,'b','B']]`を行った後、
16
15
 
16
+
17
+ ```html
18
+
19
+ <html>
20
+
21
+ <head>
22
+
23
+ <script>
24
+
25
+ function itemCheckTest(barcode1, barcode2) {
26
+
27
+
28
+
29
+ csvArray = [[1,'a','A'],[2,'b','B']]
30
+
31
+
32
+
33
+ //1.indexedDB関連オブジェクトの取得
34
+
35
+ var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
36
+
37
+ var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.mozIDBTransaction || window.msIDBTransaction;
38
+
39
+ var IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.mozIDBKeyRange || window.msIDBKeyRange;
40
+
41
+ var IDBCursor = window.IDBCursor || window.webkitIDBCursor;
42
+
43
+ //2.indexedDBを開く
44
+
45
+ var idbReq = indexedDB.open("ItemDB", 1);
46
+
47
+ //3.DBの新規作成時、またはバージョン変更時に実行するコード
48
+
49
+ idbReq.onupgradeneeded = function (event) {
50
+
51
+ var db = event.target.result;
52
+
53
+ var itemStore = db.createObjectStore("item", { keyPath: "item_id" });
54
+
55
+ itemStore.createIndex("bc", ["bc1","bc2"], { unique: false });
56
+
57
+ console.log("インデックス追加しました");
58
+
59
+ //データの追加
60
+
61
+ for (var i = 0, len = csvArray.length; i < len; i++) {
62
+
63
+ var csvArrayIn = csvArray[i];
64
+
65
+ itemStore.add({ item_id: csvArrayIn[0], bc1:csvArrayIn[1], bc2:csvArrayIn[2]});
66
+
67
+ csvArrayIn = [];
68
+
69
+ }
70
+
71
+ console.log("データ追加しました");
72
+
73
+ }
74
+
75
+ //4-1.DBオープン失敗時の処理
76
+
77
+ idbReq.onerror = function (event) {
78
+
79
+ console.log("error");
80
+
81
+ };
82
+
83
+ //4-2.DBオープン成功時の処理
84
+
85
+ var db;
86
+
87
+ idbReq.onsuccess = function (event) {
88
+
89
+ db = idbReq.result;
90
+
91
+ //読み書き権限付きで使用することを宣言
92
+
93
+ var transaction = db.transaction(["item"], "readwrite");
94
+
95
+ //各オブジェクトストアの取り出し
96
+
97
+ var itemStore = transaction.objectStore("item");
98
+
99
+ var indexBc = itemStore.index("bc");
100
+
101
+ var range = IDBKeyRange.only([barcode1,barcode2]);
102
+
103
+ indexBc.openCursor(range).onsuccess = function (event) {
104
+
105
+ var cursor = event.target.result;
106
+
107
+ if (cursor == null) {
108
+
109
+ console.log("NG:" + cursor);
110
+
111
+ } else {
112
+
113
+ console.log("OK:" + cursor);
114
+
115
+ cursor.continue();
116
+
117
+ }
118
+
119
+ }
120
+
121
+ }
122
+
123
+ }
124
+
17
- `itemCheckTest('a','A');`を呼び出すと次のようになりました。
125
+ itemCheckTest('a','A');
126
+
127
+ </script>
128
+
129
+ </head>
130
+
131
+ </html>
132
+
133
+ ```
134
+
135
+
136
+
137
+ Chrome53では次のような結果でした。
18
138
 
19
139
 
20
140