Ejemplo n.º 1
0
    public function test_render()
    {
        $collection = Jam::all('test_city')->load_fields(array(array('id' => 1, 'name' => 'London', 'population' => 10), array('id' => 2, 'name' => 'New York', 'population' => 15)));
        $name = new Tart_Column();
        $name->sort(FALSE);
        $population = new Tart_Column();
        $population->sort(FALSE);
        $columns = array('name' => $name, 'population' => $population);
        $table = new Tart_Table($collection, $columns);
        $table->selected(FALSE);
        $expected = <<<HTML
<table class="table table-striped table-hover">
  <thead>
    <th>Name</th>
    <th>Population</th>
  </thead>
  <tbody>
    <tr class="test_city-1">
      <td>London</td>
      <td>10</td>
    </tr>
    <tr class="test_city-2">
      <td>New York</td>
      <td>15</td>
    </tr>
  </tbody>
</table>
HTML;
        $this->assertSame($expected, $table->render());
        $table->selected(array(1));
        $expected = <<<HTML_SELECTED
<table class="table table-striped table-hover">
  <thead>
    <th width="10">
      <input type="checkbox" name="all" value="1"/>
    </th>
    <th>Name</th>
    <th>Population</th>
  </thead>
  <tbody>
    <tr class="test_city-1">
      <td>
        <input type="checkbox" name="id[]" value="1" checked="1"/>
      </td>
      <td>London</td>
      <td>10</td>
    </tr>
    <tr class="test_city-2">
      <td>
        <input type="checkbox" name="id[]" value="2"/>
      </td>
      <td>New York</td>
      <td>15</td>
    </tr>
  </tbody>
</table>
HTML_SELECTED;
        $this->assertSame($expected, $table->render());
    }
Ejemplo n.º 2
0
 public function test_getters_setters()
 {
     $items = array('name_param' => new Tart_Column(), 'name2' => new Tart_Column());
     $group = new Tart_Table();
     $group->items($items);
     $this->assertSame($items['name_param'], $group->items('name_param'));
     $this->assertEquals('name_param', $group->items('name_param')->name());
     $this->assertEquals('Name param', $group->items('name_param')->label());
     $this->assertSame($items['name2'], $group->items('name2'));
     $this->assertEquals('name2', $group->items('name2')->name());
     $this->assertEquals('Name2', $group->items('name2')->label());
 }
Ejemplo n.º 3
0
 public function render()
 {
     $html = Tart::html($this, function ($h, $self) {
         $h('table', $self->attributes(), function ($h, $self) {
             $h('thead', function ($h, $self) {
                 if ($self->selected() !== NULL and $self->selected() !== FALSE) {
                     $h('th', array('width' => 10), function ($h, $self) {
                         $h('input', array('type' => 'checkbox', 'name' => 'all', 'value' => '1', 'checked' => array_diff($self->collection()->ids(), $self->selected()) ? NULL : 'checked'));
                     });
                 }
                 foreach ($self->columns() as $column) {
                     $attributes = array('width' => $column->width());
                     if ($column->sort()) {
                         $attributes['nowrap'] = 'nowrap';
                     }
                     $h('th', $attributes, $column->sort() ? $column->sort_anchor() : $column->label());
                 }
             });
             $h('tbody', function ($h, $self) {
                 foreach ($self->collection() as $item) {
                     $h('tr', array('class' => Tart_Table::item_class_name($item)), function ($h, $self) use($item) {
                         if ($self->selected() !== NULL and $self->selected() !== FALSE) {
                             $h('td', function ($h, $self) use($item) {
                                 $h('input', array('type' => 'checkbox', 'name' => 'id[]', 'value' => Jam_Form::list_id($item), 'checked' => in_array(Jam_Form::list_id($item), $self->selected()) ? TRUE : NULL));
                             });
                         }
                         foreach ($self->columns() as $column) {
                             $h('td', $column->item($item)->render());
                         }
                     });
                 }
             });
             if ($self->footer()) {
                 $h('tfoot', $self->footer());
             }
         });
     });
     return $html->render();
 }