2

Im trying to fetch the data in a html table inside that a table which contains a text box and the data but when iam trying to create a html table multiple tables are created inside a div when the data is fetched is there a way to crate a html table and the text box and fetch all the data ta in the same table

JS Fiddel
HTML:

<input type="hidden" id="id" />First name:
    <input type="text" id="firstName" />
<br />
Last name:
    <input type="text" id="lastName" />
<br />
Phone:
    <input type="text" id="phone" />
<br />
<button class="reset">Reset Form</button>
<button class="update">Update</button>
<button class="insert">Insert</button>
<button class="drop">Drop Table</button>
<div id="results"></div>

as we can see in the below image there are multiple table are created for each fetched result that should be one in my scenario. enter image description here

JS:

var results = $('#results')[0];
var id = $('#id')[0];
var firstName = $('#firstName')[0];
var lastName = $('#lastName')[0];
var phone = $('#phone')[0];

var createStatement = "CREATE TABLE IF NOT EXISTS Contacts (id INTEGER PRIMARY KEY AUTOINCREMENT, firstName TEXT, lastName TEXT, phone TEXT)";
var selectAllStatement = "SELECT * FROM Contacts";
var insertStatement = "INSERT INTO Contacts (firstName, lastName, phone) VALUES (?, ?, ?)";
var updateStatement = "UPDATE Contacts SET firstName = ?, lastName = ?, phone = ? WHERE id = ?";
var deleteStatement = "DELETE FROM Contacts WHERE id=?";
var dropStatement = "DROP TABLE Contacts";

var db = openDatabase("Book", "1.0", "Address Book", 200000);
var dataset;
createTable();

function onError(tx, error) {
    alert(error.message);
}

function showRecords() {
    results.innerHTML = '';
    db.transaction(function (tx) {
        tx.executeSql(selectAllStatement, [], function (tx, result) {
            dataset = result.rows;
            for (var i = 0, item = null; i < dataset.length; i++) {
                item = dataset.item(i);
                results.innerHTML += '<table><tr><td><input type="text" value="' + item['lastName'] + '"></input> </td>, <td><input type="text" value="' + item['firstName'] + '"></input></td></tr><table>';
            }
        });
    });
}

function createTable() {
    db.transaction(function (tx) {
        tx.executeSql(createStatement, [], showRecords, onError);
    });
}

function insertRecord() {
    db.transaction(function (tx) {
        tx.executeSql(insertStatement, [firstName.value, lastName.value, phone.value], loadAndReset, onError);
    });
}

function loadRecord(i) {
    var item = dataset.item(i);
    firstName.value = item['firstName'];
    lastName.value = item['lastName'];
    phone.value = item['phone'];
    id.value = item['id'];
}

function updateRecord() {
    db.transaction(function (tx) {
        tx.executeSql(updateStatement, [firstName.value, lastName.value, phone.value, id.value], loadAndReset, onError);
    });
}

function deleteRecord(id) {
    db.transaction(function (tx) {
        tx.executeSql(deleteStatement, [id], showRecords, onError);
    });
    resetForm();
}

function dropTable() {
    db.transaction(function (tx) {
        tx.executeSql(dropStatement, [], showRecords, onError);
    });
    resetForm();
}

function loadAndReset() {
    resetForm();
    showRecords();
}

function resetForm() {
    firstName.value = '';
    lastName.value = '';
    phone.value = '';
    id.value = '';
}

$('.reset').on('click', resetForm);
$('.update').on('click', updateRecord);
$('.insert').on('click', insertRecord);
$('.drop').on('click', dropTable);
5
  • What is your question? Are you trying to create just one table with multiple rows? Commented Aug 17, 2015 at 8:19
  • yes absolutely results.innerHTML += '<table><tr><td><input type="text" value="' + item['lastName'] + '"></input> </td>, <td><input type="text" value="' + item['firstName'] + '"></input></td></tr><table>'; in this line i have made a change but doesn't work Commented Aug 17, 2015 at 8:22
  • with that line you are adding a new table for each fetched row Commented Aug 17, 2015 at 8:33
  • The easiest way to fix this would be to make #results a <table> not a <div>, and take the <table> + </table> out of the result string. Commented Aug 17, 2015 at 8:35
  • then how to create a single table and multiple rows for each data Commented Aug 17, 2015 at 8:36

1 Answer 1

1

The easiest fix would be to change the div to a table and add the rows to that instead of adding a table each iteration of the for-loop to the innerhtml of #result.

HTML:

<input type="hidden" id="id" />First name:
    <input type="text" id="firstName" />
<br />
Last name:
    <input type="text" id="lastName" />
<br />
Phone:
    <input type="text" id="phone" />
<br />
<button class="reset">Reset Form</button>
<button class="update">Update</button>
<button class="insert">Insert</button>
<button class="drop">Drop Table</button>
<table id="results"></table><!-- this is the only line that changed -->

And since you are using jQuery you can use .append() instead of setting the innerhtml each iteration.
JS:

function showRecords() {
    db.transaction(function (tx) {
        tx.executeSql(selectAllStatement, [], function (tx, result) {
            dataset = result.rows;
            for (var i = 0, item = null; i < dataset.length; i++) {
                item = dataset.item(i);
                $('#results').append('<tr><td><input type="text" value="' + item['lastName'] + '"></input> </td>, <td><input type="text" value="' + item['firstName'] + '"></input></td></tr>');
            }
        });
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.