Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- Yii::import("zii.widgets.grid.CGridView");
- Yii::import("application.widgets.DataColumn");
- class GridView extends CGridView {
- public $navegacao = true;
- public $paginacaoDefault = true;
- public $url = null;
- public $attributeToUpdate = null;
- public $fieldToFocus = null;
- public $attributeToSend = array();
- protected $rowSetKeys = array();
- public $rowIdPrefix = 'gr_';
- public function init() {
- $this->summaryText = '';
- $this->enableSorting = true;
- $this->emptyText = $this->emptyText === '' ? 'Nenhum resultado encontrado.' : $this->emptyText;
- if ($this->paginacaoDefault) {
- $this->pager = array(
- 'cssFile' => Yii::app()->baseUrl . '/css/gridView.css',
- 'class' => 'CLinkPager',
- 'maxButtonCount' => 10,
- 'nextPageLabel' => yii::t('core', 'Próximo'),
- 'prevPageLabel' => yii::t('core', 'Anterior'),
- 'firstPageLabel' => yii::t('core', 'Primeiro'),
- 'lastPageLabel' => yii::t('core', 'Último'),
- 'header' => '',
- );
- }
- $this->cssFile = Yii::app()->baseUrl . '/css/gridView.css';
- if ($this->navegacao) {
- array_push($this->columns, array(
- 'class' => 'CButtonColumn',
- 'viewButtonImageUrl' => Yii::app()->baseUrl . '/images/grid/ico_visualizar.png',
- 'updateButtonImageUrl' => Yii::app()->baseUrl . '/images/grid/ico_editar.png',
- 'deleteButtonImageUrl' => Yii::app()->baseUrl . '/images/grid/ico_excluir.png',
- 'viewButtonUrl' => 'Yii::app()->getController()->createUrl("visualizar", array("id" => $data["id"]))',
- 'deleteButtonUrl' => 'Yii::app()->getController()->createUrl("deletar", array("id" => $data["id"]))',
- 'updateButtonUrl' => 'Yii::app()->getController()->createUrl("editar", array("id" => $data["id"]))',
- ));
- }
- if ($this->url != null) {
- $script = 'function(id) {';
- foreach ($this->attributeToSend as $att => $value) {
- if ($att == '0') {
- $script .= 'var ' . $value . ' = $("#' . $value . '").val();';
- } else {
- $script .= 'var ' . $att . ' = $("#' . $value . '").val();';
- }
- }
- $script .= '
- var paramId = jQuery.fn.yiiGridView.getSelection(id);
- console.log(paramId);
- if(paramId.length>0) {
- $.ajax({
- url: "' . $this->url . '",
- data: "id=" + paramId';
- foreach ($this->attributeToSend as $att => $value) {
- if ($att == '0') {
- $script .= '+ "&' . $value . '=" + ' . $value . '';
- } else {
- $script .= '+ "&' . $att . '=' . $value . '"';
- }
- }
- $script .= ', success: function(data) {
- $("#' . $this->attributeToUpdate . '").html(data);';
- if (isset($this->fieldToFocus)) {
- $script .= '
- $("#' . $this->fieldToFocus . '").focus();';
- }
- $script .= '
- },
- error: function() {
- alert("Requisição Inválida!");
- }
- });
- }
- }';
- $this->selectionChanged = $script;
- }
- // if ($this->dataProvider->getKeys()) {
- $this->rowSetKeys = $this->dataProvider->getKeys();
- // } else {
- // throw new CException("Seu model " . $this->dataProvider->getId() . " está sem chave primária!");
- // }
- parent::init();
- }
- public function renderTableFooter() {
- $hasFilter = $this->filter !== null && $this->filterPosition === self::FILTER_POS_FOOTER;
- $hasFooter = $this->getHasFooter();
- if ($hasFilter || $hasFooter) {
- echo "<tfoot>\n";
- if ($hasFooter) {
- $totalDeColunas = 0;
- //verifica o total de colunas em todas as colunas
- foreach ($this->columns as $column) {
- if (isset($column->multipleFooter)) {
- if (sizeof($column->multipleFooter) > $totalDeColunas) {
- $totalDeColunas = sizeof($column->multipleFooter);
- }
- }
- }
- if ($totalDeColunas >= 1) {
- for ($cont = 0; $cont < $totalDeColunas; $cont++) {
- echo "<tr>\n";
- foreach ($this->columns as $column) {
- if (isset($column->multipleFooter)) {
- echo $column->renderMultiFooterCell($cont);
- } else {
- //printando na primeira linha
- if ($cont == 0) {
- $column->renderFooterCell();
- } else {
- echo "<td>" . $this->blankDisplay . "</td>\n";
- }
- }
- }
- echo "</tr>\n";
- }
- } else {
- echo "<tr>\n";
- foreach ($this->columns as $column)
- $column->renderFooterCell();
- echo "</tr>\n";
- }
- }
- if ($hasFilter)
- $this->renderFilter();
- echo "</tfoot>\n";
- }
- }
- /**
- * Mesma coisa do CGridView, a diferença é a $column['class']
- */
- protected function initColumns() {
- if ($this->columns === array()) {
- if ($this->dataProvider instanceof CActiveDataProvider)
- $this->columns = $this->dataProvider->model->attributeNames();
- else if ($this->dataProvider instanceof IDataProvider) {
- // use the keys of the first row of data as the default columns
- $data = $this->dataProvider->getData();
- if (isset($data[0]) && is_array($data[0]))
- $this->columns = array_keys($data[0]);
- }
- }
- $id = $this->getId();
- foreach ($this->columns as $i => $column) {
- if (is_string($column))
- $column = $this->createDataColumn($column);
- else {
- if (!isset($column['class'])) {
- //Alteração realizada aqui!
- $column['class'] = 'application.widgets.DataColumn';
- }
- $column = Yii::createComponent($column, $this);
- }
- if (!$column->visible) {
- unset($this->columns[$i]);
- continue;
- }
- if ($column->id === null)
- $column->id = $id . '_c' . $i;
- $this->columns[$i] = $column;
- }
- foreach ($this->columns as $column)
- $column->init();
- }
- /**
- * Mesma coisa do CGridView, a diferença é a DataColumn
- */
- protected function createDataColumn($text) {
- if (!preg_match('/^([\w\.]+)(:(\w*))?(:(.*))?$/', $text, $matches))
- throw new CException(Yii::t('zii', 'The column must be specified in the format of "Name:Type:Label", where "Type" and "Label" are optional.'));
- $column = new DataColumn($this);
- $column->name = $matches[1];
- if (isset($matches[3]) && $matches[3] !== '')
- $column->type = $matches[3];
- if (isset($matches[5]))
- $column->header = $matches[5];
- return $column;
- }
- /**
- * Adicionando o RowId nos TR
- */
- public function renderTableRow($row) {
- $rowId = $this->rowIdPrefix . $this->rowSetKeys[$row];
- if ($this->rowCssClassExpression !== null) {
- $data = $this->dataProvider->data[$row];
- echo '<tr class="' . $this->evaluateExpression($this->rowCssClassExpression, array('row' => $row, 'data' => $data)) . '" id="' . $rowId . '">';
- } else if (is_array($this->rowCssClass) && ($n = count($this->rowCssClass)) > 0)
- echo '<tr class="' . $this->rowCssClass[$row % $n] . '" id="' . $rowId . '">';
- else
- echo '<tr id="' . $rowId . '">';
- foreach ($this->columns as $column)
- $column->renderDataCell($row);
- echo "</tr>\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement