0

I have this code here where it can generate a list from the values in the database. The problem is it only displays 1 column. How can I display 2 columns in a list by correcting this code?

desired display:

   (from column1)   (from column2)
○ (some booktitle) (some bookauthor)
○ (some booktitle) (some bookauthor)
○ (some booktitle) (some bookauthor)

code: (fldBookTitle is the Column name and the second is fldBookAuthor)

function listBooks(){
            db.transaction(function (tx){
                tx.executeSql('SELECT * FROM tblBooks', [],          function(tx, results){
                    var len = results.rows.length, i;
                    var listContainer = document.createElement("div");
                    document.getElementsByTagName("body")[0].appendChild(listContainer);
                    var listElement = document.createElement("ul");
                    listContainer.appendChild(listElement);
                    for(i=0;i<len;++i){
                        var listItem = document.createElement("li");
                        listItem.innerHTML = results.rows.item(i).fldBookTitle;
                        listElement.appendChild(listItem);
                    }   

                });
                //console.log("table created");
            });



        }

thanks for any help.

4

3 回答 3

0

这应该这样做。它创建一个包含表格行和两列的表格。

function listBooks(){
            db.transaction(function (tx){
                tx.executeSql('SELECT * FROM tblBooks', [],          function(tx, results){
                    var len = results.rows.length, i;
                    var listContainer = document.createElement("div");
                    document.getElementsByTagName("body")[0].appendChild(listContainer);
                    var listElement = document.createElement("table");
                    listContainer.appendChild(listElement);
                    for(i=0;i<len;++i){
                        var listItem = document.createElement("tr");
                        var titleItem = document.createElement("td").innerHTML = results.rows.item(i).fldBookTitle;
                        var authorItem = document.createElement("td").innerHTML = results.rows.item(i).fldBookAuthor;
                        listItem.appendChild(titleItem);
                        listItem.appendChild(authorItem);
                        listElement.appendChild(listItem);
                    }   

                });
                //console.log("table created");
            });



        }

第二种解决方案

function listBooks(){
            db.transaction(function (tx){
                tx.executeSql('SELECT * FROM tblBooks', [],          function(tx, results){
                    var len = results.rows.length, i;
                    var listContainer = document.createElement("div");
                    document.getElementsByTagName("body")[0].appendChild(listContainer);
                    var listElement = document.createElement("ul");
                    listContainer.appendChild(listElement);
                    for(i=0;i<len;++i){
                        var listItem = document.createElement("li");
                        listItem.innerHTML = results.rows.item(i).fldBookTitle + " " + results.rows.item(i).fldBookAuthor;
                        listElement.appendChild(listItem);
                    }   

                });
                //console.log("table created");
            });



        }
于 2013-07-10T17:04:00.330 回答
0

If you are looking for more than one column, a table seems more appropriate than a ul. Otherwise you can add both items to each row (li) but the second values won't reliably match up vertically like a column.

于 2013-07-10T16:52:30.687 回答
0

您最好的选择(除了表格)是使用字典列表dl。字典列表中的每个项目都有一个字典术语dt和一个字典定义dd

于 2013-07-10T16:57:32.497 回答