dudapiotr / ZfTable

*Not supported*ZfTable 3.1. Awesome ZF2 table / grid (and much much more) generator with huge possibilities of decorating and conditioning. Integrated with DataTables, Doctrine 2, Bootstrap 2.0 and 3.0.
http://dudapiotrek.laohost.net/
MIT License
75 stars 59 forks source link

Datatables 1.10 - How to Use: #26

Closed drchav closed 9 years ago

drchav commented 9 years ago

Here some update n datables 1.10

<?php /**

namespace Core\DataTables;

use ZfTable\Params\AbstractAdapter; use ZfTable\Params\AdapterInterface; use ZfTable\Table\Exception;

class ParamAdapterDataTable extends AbstractAdapter implements AdapterInterface, \Zend\Stdlib\InitializableInterface {

/**
 *
 * @var \ArrayObject | \Zend\Stdlib\ArrayObject
 */
protected $object;

/**
 *
 * @var int
 */
protected $page;

/**
 *
 * @var string
 */
protected $order;

/**
 *
 * @var string
 */
protected $column;

/**
 *
 * @var int
 */
protected $itemCountPerPage;

/**
 * Quick search
 * @var string
 */
protected $quickSearch;

const DEFAULT_PAGE = 1;
const DEFAULT_ORDER = 'asc';
const DEFAULT_ITEM_COUNT_PER_PAGE = 2;

public function __construct($object)
{
    if ($object instanceof \ArrayObject) {
        $this->object = $object;
    } else if ($object instanceof \Zend\Stdlib\ArrayObject) {
        $this->object = $object;
    } else {
        throw new Exception\InvalidArgumentException('parameter must be instance of ArrayObject');
    }
}

/**
 * Init method
 */
public function init()
{
    $array = $this->object->toArray();

    $this->page = (isset($array['start'])) ? ($array['start'] / $array['length'] + 1) : self::DEFAULT_PAGE;

    if(isset($array['order'][0]['column'])){
        $headers = $this->getTable()->getHeaders();
        $slice = array_slice($headers, $array['order'][0]['column'], 1);
        $this->column = key($slice);
    }
    $this->order = (isset($array['order'][0]['dir'])) ? $array['order'][0]['dir'] : self::DEFAULT_ORDER;
    $this->itemCountPerPage = (isset($array['start'])) ? $array['length'] : 20;
    $this->quickSearch = (isset($array['search']['value'])) ? $array['search']['value'] : '';
}

/**
 * Get page
 * @return int
 */
public function getPage()
{
    return $this->page;
}

/**
 * Set page
 * @param string $page
 */
public function setPage($page)
{
    $this->page = $page;
    return $this;
}

/**
 * Get order
 * @return string
 */
public function getOrder()
{
    return $this->order;
}

/**
 * Set asc or desc ordering
 * @param order $order
 */
public function setOrder($order)
{
    $this->order = $order;
}

/**
 * Get column
 * @return string
 */
public function getColumn()
{
    return ($this->column == '') ? null : $this->column;
}

/**
 *
 * @param string $column
 * @return \ZfTable\Params\AdapterArrayObject
 */
public function setColumn($column)
{
    $this->column = $column;
    return $this;
}

/**
 * Get item count per page
 * @return int
 */
public function getItemCountPerPage()
{
    return $this->itemCountPerPage;
}

/**
 *
 * @param type $itemCountPerPage
 */
public function setItemCountPerPage($itemCountPerPage)
{
    $this->itemCountPerPage = $itemCountPerPage;
}

/**
 * Return offset
 * @return int
 */
public function getOffset()
{
    return ($this->getPage() * $this->getItemCountPerPage()) - $this->getItemCountPerPage();
}

/**
 * Get quickserach string
 * @return string
 */
public function getQuickSearch()
{
    return $this->quickSearch;
}

public function getPureValueOfFilter($key)
{
    return $this->object[$key];
}

}

drchav commented 9 years ago

and in the controlle i made this:

$request = $this->getRequest(); $dataPost = $request->getPost(); $userData = $this->getUserData();

    $adapter = $this->getService('DbAdapter');
    $sql = new Sql($adapter);
    $source = $sql->select()
        ->from(array('a'=>'tblmerchant_invoices'))
        ->columns(
            array(
                'id',
                'userFrom',
                'procStatus',
                'transactionId',
                'invoiceNumber',
                'defaultCurrency',
                'invoiceDate',
                'invoiceDueDate',
                'totalItens',
                'itensDiscount',
                'delivery',
                'totalInvoice'
            )
        )
        ->join(array('b' => 'tblsystem_users'), 'a.userFrom = b.id', array('fullName','userEmail'))
        ->join(array('c' => 'tblsystem_status'), 'a.procStatus = c.id', array('statusName','statusColor'))
        ->where(array("a.userTo" => $userData->getId()));

    $table = new \Core\DataTables\Invoices();
    $table->setAdapter($adapter)
        ->setSource($source)
        ->setParamAdapter(new \Core\DataTables\ParamAdapterDataTable($dataPost));

    $dataSource = $sql->prepareStatementForSqlObject($source);
    $exec = $dataSource->execute();

    $dadosCount = $exec->count();

    $dados = json_decode($table->render('dataTableJson'));
    $retorno = array(
        'draw' => $dados->sEcho,
        'recordsTotal' => $dadosCount,
        'recordsFiltered' => $dados->iTotalDisplayRecords,
        'data' => $dados->aaData
    );
    return $this->jsonResponse($retorno);
dudapiotr commented 9 years ago

Thanks, In the next version I will add You changes. I do not close ticket, because it can help other people.

dudapiotr commented 9 years ago

Thanks a lot for You pull request. I've just merged.