Create table dynamically using Javascript to displ

2019-09-20 23:47发布

问题:

I have the following code with me:

<!doctype html>
<html>
<title>Search</title>
<script type="text/javascript">

function query() {
    var adVarWChar = 202;
    var adParamInput = 1;
    var pad = "C:\\Users\\azi!z\\Desktop\\Project\\Test.accdb";
    var cn = new ActiveXObject("ADODB.Connection");
    var strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pad;
    cn.Open(strConn);
    var cm = new ActiveXObject("ADODB.Command");
    cm.ActiveConnection = cn;
    cm.CommandText = "SELECT * FROM ImportFromExcel where TestCase LIKE ?";
    cm.Parameters.Append(cm.CreateParameter(
            "?", 
            adVarWChar, 
            adParamInput, 
            255, 
            "%" + document.getElementById("searchTerm").value + "%"));
    var rs = cm.Execute();  // returns ADODB.Recordset object
    if (rs.EOF) {
        document.write("<p>No data found.</p>");
    } else {
        while (!rs.EOF) {
        document.write("<p>" + rs.fields(0).value + ", ");
        document.write(rs.fields(1).value + ", ");
        document.write(rs.fields(2).value + ", ");
        document.write(rs.fields(3).value + ", ");
        document.write(rs.fields(4).value + ", ");
        document.write(rs.fields(5).value + ", ");
        document.write(rs.fields(6).value + ", ");
        document.write(rs.fields(7).value + ", ");
        document.write(rs.fields(8).value + ", ");
        document.write(rs.fields(9).value + ", ");
        document.write(rs.fields(10).value + ", ");
        document.write(rs.fields(11).value + ", ");
        document.write(rs.fields(12).value + ", ");
        document.write(rs.fields(13).value + ", ");
        document.write(rs.fields(14).value + ", ");
        document.write(rs.fields(15).value + ".</p>");
        var row = row.parentNode.rows[ ++idx ];
            document.write(rs.fields(1).value + ".</p>");
            rs.MoveNext();
        }
    }
    rs.Close();
    cn.Close();
}
</script>
</head>

<body>
<form method="get" name="SearchEngine" target="_blank">
<p style="text-align: center;"><span style="font-family:times new roman,times,serif;"><span style="font-size: 36px;"><strong>EA Search Engine</strong></span></span></p>
<p style="text-align: center;">&nbsp;</p>
<p style="text-align: center;"><input maxlength="300" id="searchTerm" name="KeywordSearch" size="100" type="text" value="Enter Your Keyword Here" /></p>
<p style="text-align: center;">&nbsp;</p>
<p style="text-align: center;"><input name="Search" type="button" value="Search" onclick="query();" /></p>
</form>
</body>
</html>

Please help me modify the code in such a way that on clicking the Search button, a new table has to be created with 16 columns with appropriate column header and the search result should be properly inserted in the table rows.

回答1:

Here's one way to accomplish, build a string with the HTML table...
Also, I loop the fields collection to get the column names and values.

function query() {
    var adVarWChar = 202;
    var adParamInput = 1;
    var pad = "C:\\Users\\azi!z\\Desktop\\Project\\Test.accdb";
    var cn = new ActiveXObject("ADODB.Connection");
    var strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pad;
    cn.Open(strConn);
    var cm = new ActiveXObject("ADODB.Command");
    cm.ActiveConnection = cn;
    cm.CommandText = "SELECT * FROM ImportFromExcel where TestCase LIKE ?";
    cm.Parameters.Append(cm.CreateParameter(
            "?",
            adVarWChar,
            adParamInput,
            255,
            "%" + document.getElementById("searchTerm").value + "%"));
    var rs = cm.Execute();  // returns ADODB.Recordset object

    var table = '';
    var tableRow = '';
    var headings = true;

    if (rs.EOF) {
        document.write("<p>No data found.</p>");
    } else {
        while (!rs.EOF) {
          if (headings) {
            tableRow = '';
            for (var i = 0; i < rs.fields.count; i++) {
              switch (i + 1) {
                case 1:
                case 3:
                case 4:
                case 6:
                case 8:
                case 9:
                  tableRow = tableRow + '<td>' + rs.fields(i).name + '</td>';
                  break;
              }
            }
            tableRow = '<tr>' + tableRow + '</tr>';
            table = table + tableRow;
            headings = false;
          }
          tableRow = '';
          for (var i = 0; i < rs.fields.count; i++) {
              switch (i + 1) {
                case 1:
                case 3:
                case 4:
                case 6:
                case 8:
                case 9:
                  tableRow = tableRow + '<td>' + rs.fields(i).value + '</td>';
                  break;
              }
          }
          tableRow = '<tr>' + tableRow + '</tr>';
          table = table + tableRow;
          rs.MoveNext();
        }
        document.write('<table>' + table + '</table>');
    }
    rs.Close();
    cn.Close();
}