Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <input class="btn-square" type="button" value="データ1追加(Bill)" onclick="addData(1);" /><br />
  <input class="btn-square" type="button" value="データ2追加(Alec)" onclick="addData(2);" /><br />
  <input class="btn-square" type="button" value="データ3追加(Eliana)" onclick="addData(3);" /><br />
  <input class="btn-square-so-pop" type="button" value="データ全追加" onclick="allAddData();" /><br /><br />
  <input class="btn-square" type="button" value="データ1削除(Bill)" onclick="deleateData(1);" /><br />
  <input class="btn-square" type="button" value="データ2削除(Alec)" onclick="deleateData(2);" /><br />
  <input class="btn-square" type="button" value="データ3削除(Eliana)" onclick="deleateData(3);" /><br />
  <div id="output"></div>
              
            
!

CSS

              
                /*
参考:https://saruwakakun.com/html-css/reference/buttons
*/

.btn-square {
  display: inline-block;
  padding: 0.5em 1em;
  text-decoration: none;
  background: #668ad8;
  /*ボタン色*/
  color: #FFF;
  border-bottom: solid 4px #627295;
  border-radius: 3px;
}

.btn-square:active {
  /*ボタンを押したとき*/
  -webkit-transform: translateY(4px);
  transform: translateY(4px);
  /*下に動く*/
  border-bottom: none;
  /*線を消す*/
}

.btn-square-so-pop {
  position: relative;
  display: inline-block;
  padding: 0.25em 0.5em;
  text-decoration: none;
  color: #FFF;
  background: #fd9535;
  /*色*/
  border-radius: 4px;
  /*角の丸み*/
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.2), inset 0 -2px 0 rgba(0, 0, 0, 0.05);
  font-weight: bold;
  border: solid 2px #d27d00;
  /*線色*/
}

.btn-square-so-pop:active {
  /*押したとき*/
  box-shadow: 0 0 2px rgba(0, 0, 0, 0.30);
}
              
            
!

JS

              
                "use strict";

const dbName = 'sampleDB'
const objectName = 'humans'

const customerData = [{
        name: "Bill",
        sex: '男'
    },
    {
        name: "Alec",
        sex: '男'
    },
    {
        name: "Eliana",
        sex: '女'
    },
];

function allAddData() {
    addData(1)
    addData(2)
    addData(3)
}

function addData(value) {
    var request = window.indexedDB.open(dbName);
    request.onerror = function(event) {
        alert('DBのアクセスに失敗');
    };
    request.onsuccess = function(event) {
        var db = event.target.result;
        var index = db.transaction(objectName, "readwrite").objectStore(objectName).index("name");
        index.get(customerData[value - 1].name).onerror = function(event) {
            db.close();
            alert('インデックス取得失敗');
        }
        index.get(customerData[value - 1].name).onsuccess = function(event) {
            if (event.target.result === undefined) {
                var customerObjectStore = db.transaction(objectName, "readwrite").objectStore(objectName);
                customerObjectStore.add(customerData[value - 1]);
                allGetData();
            } else {
                db.close();
            }
        }
    };
    request.onupgradeneeded = function(event) {
        var db = event.target.result;
        var objectStore = db.createObjectStore(objectName, {
            keyPath: 'my_id',
            autoIncrement: true
        });
        objectStore.createIndex("name", "name", {
            unique: true
        }); //本来は名前はuniqueってことはないと思います!
    };
}

function allGetData() {
    var request = window.indexedDB.open(dbName);
    request.onerror = function(event) {
        alert('DBのアクセスに失敗');
    };
    request.onsuccess = function(event) {
        var db = event.target.result;
        var customerObjectStoreRequest = db.transaction(objectName, "readonly").objectStore(objectName).getAll();
        customerObjectStoreRequest.onerror = function(event) {
            db.close();
            alert('オブジェクト取得失敗');
        };
        customerObjectStoreRequest.onsuccess = function(event) {
            var datas = event.target.result;
            dataShow(datas);
            db.close();
        }
    }
    request.onupgradeneeded = function(event) {
        var db = event.target.result;
        var objectStore = db.createObjectStore(objectName, {
            keyPath: 'my_id',
            autoIncrement: true
        });
        objectStore.createIndex("name", "name", {
            unique: true
        }); //本来は名前はuniqueってことはないと思います!
    };
}

function deleateData(value) {
    var request = window.indexedDB.open(dbName);
    request.onerror = function(event) {
        alert('DBのアクセスに失敗');
    };
    request.onsuccess = function(event) {
        var db = event.target.result;

        var index = db.transaction(objectName, "readwrite").objectStore(objectName).index("name");
        index.get(customerData[value - 1].name).onerror = function(event) {
            db.close();
            alert('インデックス取得失敗');
        }
        index.get(customerData[value - 1].name).onsuccess = function(event) {
            if (event.target.result === undefined) {
                db.close();
            } else {
                console.log(event.target.result);
                var customerObjectStoreRequest = db.transaction(objectName, "readwrite").objectStore(objectName).delete(event.target.result.my_id);
                customerObjectStoreRequest.onerror = function(event) {
                    db.close();
                    alert('オブジェクト削除失敗');
                };
                customerObjectStoreRequest.onsuccess = function(event) {
                    db.close();
                    allGetData();
                }
            }

        };
    };
    request.onupgradeneeded = function(event) {
        var db = event.target.result;
        var objectStore = db.createObjectStore(objectName, {
            keyPath: 'my_id',
            autoIncrement: true
        });
        objectStore.createIndex("name", "name", {
            unique: true
        }); //本来は名前はuniqueってことはないと思います!

    };
}

function dataShow(values) {
    const num = values.length;
    const target = document.getElementById('output');

    if (num === 0) {
        target.innerHTML = '<p>データがありません</p>';
    } else {
        const map1 = values.map(item => JSON.stringify(item));
        let li = '<ul><li>' + map1.join('</li><li>') + '</li></ul>'
        target.innerHTML = li;
    }
}
              
            
!
999px

Console