在 Javascript 中从对象数组创建 HTML 表

发布于 2024-07-12 11:55:47 字数 1728 浏览 6 评论 0原文

我正在从 javascript 调用 Web 方法。 Web 方法从 Northwind 数据库返回一组客户。 我正在使用的示例如下: Calling Web Services with ASP.NET AJAX

我不知道如何编写这个 javascript 方法: CreateCustomersTable

这将创建 html 表来显示返回的数据。 任何帮助,将不胜感激。

我的 javascript

function GetCustomerByCountry() {
  var country = $get("txtCountry").value;
  AjaxWebService.GetCustomersByCountry(country, OnWSRequestComplete, OnWSRequestFailed);
}
function OnWSRequestComplete(results) {
    if (results != null) {
        CreateCustomersTable(results);
        //GetMap(results);
    }
}
function CreateCustomersTable(result) {
    alert(result);
if (document.all) //Filter for IE DOM since other browsers are limited
{
   // How do I do this?
    }
}
else { 
$get("divOutput").innerHTML = "RSS only available in IE5+"; }
}

我的 web 方法

    [WebMethod]
    public Customer[] GetCustomersByCountry(string country) 
    {
        NorthwindDALTableAdapters.CustomersTableAdapter adap =
           new NorthwindDALTableAdapters.CustomersTableAdapter();
        NorthwindDAL.CustomersDataTable dt = adap.GetCustomersByCountry(country);

        if (dt.Rows.Count <= 0)
        {
            return null;
        }

        Customer[] customers = new Customer[dt.Rows.Count];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            NorthwindDAL.CustomersRow row = (NorthwindDAL.CustomersRow)dt.Rows[i];
            customers[i] = new Customer();
            customers[i].CustomerId = row.CustomerID;
            customers[i].Name = row.ContactName;
        }
        return customers;
    }

I am calling a web Method from javascript. The web method returns an array of customers from the northwind database. The example I am working from is here: Calling Web Services with ASP.NET AJAX

I dont know how to write this javascript method: CreateCustomersTable

This would create the html table to display the data being returned. Any help would be appreciated.

My javascript

function GetCustomerByCountry() {
  var country = $get("txtCountry").value;
  AjaxWebService.GetCustomersByCountry(country, OnWSRequestComplete, OnWSRequestFailed);
}
function OnWSRequestComplete(results) {
    if (results != null) {
        CreateCustomersTable(results);
        //GetMap(results);
    }
}
function CreateCustomersTable(result) {
    alert(result);
if (document.all) //Filter for IE DOM since other browsers are limited
{
   // How do I do this?
    }
}
else { 
$get("divOutput").innerHTML = "RSS only available in IE5+"; }
}

My web Method

    [WebMethod]
    public Customer[] GetCustomersByCountry(string country) 
    {
        NorthwindDALTableAdapters.CustomersTableAdapter adap =
           new NorthwindDALTableAdapters.CustomersTableAdapter();
        NorthwindDAL.CustomersDataTable dt = adap.GetCustomersByCountry(country);

        if (dt.Rows.Count <= 0)
        {
            return null;
        }

        Customer[] customers = new Customer[dt.Rows.Count];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            NorthwindDAL.CustomersRow row = (NorthwindDAL.CustomersRow)dt.Rows[i];
            customers[i] = new Customer();
            customers[i].CustomerId = row.CustomerID;
            customers[i].Name = row.ContactName;
        }
        return customers;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别挽留 2024-07-19 11:55:47

尝试查看调试模式下的结果变量值是多少。 如果结构看起来像我想象的结构,那么这样的事情可以工作:

function CreateCustomersTable(result) {
    var str = '<table>'; 
    str += '<tr><th>Id</th><th>Name</th></tr>';
    for ( var i=0; i< result.length; i++){
        str += '<tr><td>' + result[i].CustomerId + '</td><td>' + result[i].Name + '</td></tr>';
    }
    str += '</table>';
    return str;    
}

然后你可以做这样的事情:

var existingDiv = document.getElementById('Id of an existing Div');
existingDiv.innerHTML = CreateCustomersTable(result);

我希望这对你有帮助。

Try to look what is the result variable value in debug mode. If the structure seems the structure that i'm imagining, something like this could work:

function CreateCustomersTable(result) {
    var str = '<table>'; 
    str += '<tr><th>Id</th><th>Name</th></tr>';
    for ( var i=0; i< result.length; i++){
        str += '<tr><td>' + result[i].CustomerId + '</td><td>' + result[i].Name + '</td></tr>';
    }
    str += '</table>';
    return str;    
}

And then You can do somethig like this:

var existingDiv = document.getElementById('Id of an existing Div');
existingDiv.innerHTML = CreateCustomersTable(result);

I wish this help you.

记忆で 2024-07-19 11:55:47

类似这样,假设您在“结果”值中返回了 JSON。 “container”是一个id为“container”的div。 我克隆节点是为了节省内存,但如果您想将一些基类分配给“基”元素,也是如此。

var table = document.createElement('table');
var baseRow = document.createElement('tr');
var baseCell = document.createElement('td');
var container = document.getElementById('container');

for(var i = 0; i < results.length; i++){
  //Create a new row
  var myRow = baseRow.cloneNode(false);

  //Create a new cell, you could loop this for multiple cells
  var myCell = baseCell.cloneNode(false);
  myCell.innerHTML = result.value;

  //Append new cell
  myRow.appendChild(myCell);

  //Append new row
  table.appendChild(myRow);
}

container.appendChild(table);

Something like this, assuming you have JSON returned in the "result" value. The "container" is a div with id of "container". I'm cloning nodes to save memory, but also if you wanted to assign some base classes to the "base" elements.

var table = document.createElement('table');
var baseRow = document.createElement('tr');
var baseCell = document.createElement('td');
var container = document.getElementById('container');

for(var i = 0; i < results.length; i++){
  //Create a new row
  var myRow = baseRow.cloneNode(false);

  //Create a new cell, you could loop this for multiple cells
  var myCell = baseCell.cloneNode(false);
  myCell.innerHTML = result.value;

  //Append new cell
  myRow.appendChild(myCell);

  //Append new row
  table.appendChild(myRow);
}

container.appendChild(table);
怪异←思 2024-07-19 11:55:47

您应该将数组作为 JSON 或 XML 传递,而不仅仅是它的 toString() 值(除非返回 JSON 或 XML)。 请注意,JSOn 更适合 javascript,因为它是 javascript 本机格式。
另外,那些告诉你 IE 以外的浏览器不能进行 DOM 操作的人可能也对他/她做了可怕的事情。

如果您的格式是 JSON,您只需 for 循环它们并创建元素并打印它们。 (一旦您弄清楚您的服务返回的格式,我们就可以更好地帮助您。)

You should pass the array as JSON or XML instead of just the toString() value of it (unless that offcourse is returns either JSON oR XML). Note that JSOn is better for javascript since it is a javascript native format.
Also the person who told you that browser other then IE can not do DOM manipulation should propably have done horrible things to him/her.

If your format is JSON you can just for-loop them and create the elements and print them. (once you figured out what format your service returns we can help you better.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文