Ejemplo n.º 1
0
 /**
  * Performs databinding to populate datagrid items from data source.
  * This method is invoked by {@link dataBind()}.
  * You may override this function to provide your own way of data population.
  * @param Traversable the bound data
  */
 protected function performDataBinding($data)
 {
     $this->reset();
     $keys = $this->getDataKeys();
     $keys->clear();
     $keyField = $this->getDataKeyField();
     // get all columns
     if ($this->getAutoGenerateColumns()) {
         $columns = new TList($this->getColumns());
         $autoColumns = $this->createAutoColumns($data);
         $columns->mergeWith($autoColumns);
     } else {
         $columns = $this->getColumns();
     }
     $this->_allColumns = $columns;
     $items = $this->getItems();
     $index = 0;
     $allowPaging = $this->getAllowPaging() && $data instanceof TPagedDataSource;
     $dsIndex = $allowPaging ? $data->getFirstIndexInPage() : 0;
     $this->setViewState('DataSourceIndex', $dsIndex, 0);
     if ($columns->getCount()) {
         foreach ($columns as $column) {
             $column->initialize();
         }
         $selectedIndex = $this->getSelectedItemIndex();
         $editIndex = $this->getEditItemIndex();
         foreach ($data as $key => $row) {
             if ($keyField !== '') {
                 $keys->add($this->getDataFieldValue($row, $keyField));
             } else {
                 $keys->add($key);
             }
             if ($index === 0) {
                 if ($allowPaging) {
                     $this->_topPager = $this->createPager();
                 }
                 $this->_header = $this->createItemInternal(-1, -1, TListItemType::Header, true, null, $columns);
             }
             if ($index === $editIndex) {
                 $itemType = TListItemType::EditItem;
             } else {
                 if ($index === $selectedIndex) {
                     $itemType = TListItemType::SelectedItem;
                 } else {
                     if ($index % 2) {
                         $itemType = TListItemType::AlternatingItem;
                     } else {
                         $itemType = TListItemType::Item;
                     }
                 }
             }
             $items->add($this->createItemInternal($index, $dsIndex, $itemType, true, $row, $columns));
             $index++;
             $dsIndex++;
         }
         if ($index > 0) {
             $this->_footer = $this->createItemInternal(-1, -1, TListItemType::Footer, true, null, $columns);
             if ($allowPaging) {
                 $this->_bottomPager = $this->createPager();
             }
         }
     }
     $this->setViewState('ItemCount', $index, 0);
     if (!$dsIndex && $this->_emptyTemplate !== null) {
         $this->_useEmptyTemplate = true;
         $this->_emptyTemplate->instantiateIn($this);
         $this->dataBindChildren();
     }
 }