Edu*_*pat 3 html html-table list dart
我想基于HTML表填充对象列表.假设我有以下课程:
class Employee
{
String name;
String department;
num salary;
...methods
}
Run Code Online (Sandbox Code Playgroud)
在我的HTML中,我有下表:
<table class="table" id="employeeTable">
<thead>
<tr>
<th>Name
<th>Departament
<th>Salary
<tbody id="employeeTableBody">
<tr>
<td> John
<td> 1
<td> 1500
<tr>
<td> Mary
<td> 2
<td> 2500
...etc
</table>
Run Code Online (Sandbox Code Playgroud)
那么,我如何查询表,获取其行,然后让它的单元格填充我的员工列表(在这种情况下)?
我尝试使用类似的东西:
TableElement table = query("#employeesTable");
Element tableBody = query("#employeesTableBody");
Run Code Online (Sandbox Code Playgroud)
但我找不到TableElement或Element中的正确方法来返回TableRowElement,或者可能是它的单元格.我试图获得子节点,但没有成功.
完成此任务的伪算法将是这样的:
1. Get the table
2. For each row of the table
2.a Create a new Employee object based on the value of each cell of the row.
2.b Append this object to the Employee List.
3. End
Run Code Online (Sandbox Code Playgroud)
这里是HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Scratchweb</title>
<link rel="stylesheet" href="scratchweb.css">
</head>
<body>
<table id="employeeTable">
<tr>
<th>Name</th>
<th>Departament</th>
<th>Salary</th>
</tr>
<tr>
<td>John</td>
<td>1</td>
<td>1500</td>
</tr>
<tr>
<td>Mary</td>
<td>2</td>
<td>2500</td>
</tr>
</table>
<script type="application/dart" src="web/scratchweb.dart"></script>
<script src="https://dart.googlecode.com/svn/branches/bleeding_edge/dart/client/dart.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是飞镖:
import 'dart:html';
import 'dart:math';
class Employee {
String name;
String department;
num salary;
Employee({this.name, this.department, this.salary});
String toString() => '<employee name="$name" department="$department" salary="$salary">';
}
void main() {
var employees = new List<Employee>();
var table = query("table#employeeTable");
for (TableRowElement row in table.rows) {
if (row.cells.length != 3) {
print("Malformed row: $row");
continue;
}
if ((row.cells[0] as TableCellElement).tagName == "TH") {
print("Skipping header");
continue;
}
var cells = row.cells;
var employee = new Employee(
name: cells[0].text,
department: cells[1].text,
salary: parseDouble(cells[2].text));
employees.add(employee);
}
print(employees);
}
Run Code Online (Sandbox Code Playgroud)
如果您赞同这个答案,请记得接受.每当我成功回答问题时,我的老板就给我一块培根;)
| 归档时间: |
|
| 查看次数: |
1101 次 |
| 最近记录: |