Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate a List based on a Table and its rows in Dart?

I want to populate a list of Objects based on a HTML Table. Let's say I have the following class:

class Employee
{
  String name;
  String department;
  num salary;

  ...methods
}

And in my HTML, I have the following table:

<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>

So, how do I query the table, get its rows, then get its cells to fill my List of Employees (in this case)?

I tried to use something like:

    TableElement table = query("#employeesTable");
    Element tableBody = query("#employeesTableBody");

But I couldn't find a proper method in TableElement or Element to return TableRowElement, or maybe the cells of it. I tried to get the children nodes also, but without sucess.

A pseudo-algorithm to fulfil this task would be something like this:

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
like image 362
Eduardo Copat Avatar asked Dec 04 '25 12:12

Eduardo Copat


1 Answers

Here the 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>

Here's the Dart:

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);
}

If you approve of this answer, please remember to accept it. My boss feeds me a slice of bacon every time I successfully answer a question ;)

like image 117
Shannon -jj Behrens Avatar answered Dec 06 '25 00:12

Shannon -jj Behrens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!