代码之家  ›  专栏  ›  技术社区  ›  pia-sophie

如何轻松排序视图

  •  0
  • pia-sophie  · 技术社区  · 8 年前

    我想对Zend3中基于表格的视图进行排序。

    以下是我的视图脚本的一部分:

    <table class="table">
    <tr>
        <th>id</th>
        <th>cccid</th>
        <th>gesch id</th>
        <th><a href="<?= $this->url('mandant',array('query' => array('sort' => 'desc'))); ?>">Name</a> </th>
        <th>langname</th>
        <th>&nbsp;</th>
    </tr>
    
    
    <?php 
    
    foreach ($mandants as $mandant) : 
        //  $unit=$units->fetchAllP($project->ProjectID);
    //var_dump(get_object_vars($project));?>
         <tr>
             <td><?= $this->escapeHtml($mandant->id)?></td>
             <td><?= $this->escapeHtml($mandant->cccid) ?></td>
             <td><?= $this->escapeHtml($mandant->geschid) ?></td>
             <td><?= $this->escapeHtml($mandant->name) ?></td>
             <td><?= $this->escapeHtml($mandant->langname) ?></td>
            <td>
                <a href="<?= $this->url('mandant', ['action' => 'show', 'id' => $mandant->id]) ?>">Show</a> 
                <a href="<?= $this->url('mandant', ['action' => 'edit', 'id' => $mandant->id]) ?>">Edit</a> 
    
            </td>
        </tr>
    <?php endforeach; ?>
    </table>
    

    我要单击列名称来排序孔表。就像我点击名称和视图列表按名称排序。

    正如你所看到的,还有其他的尝试。视图中是否有可能,还是需要控制器操作?我真的很想在Zend中做,而不是用Ajax或其他。

    如何正确操作?

    ****编辑1

    我进一步尝试,创建了一个action sortaction,它返回一个新的viewModel:

            return new ViewModel([
            'mandants' => $this->mandantTable->sortMandant(0,0),
        ]);
    

    当然,这是可行的,但a还需要一个新的viewscript,并为被调用的modelfunction指定参数,选择按排序的字段。第二个参数给出了asc或desc。

    我真的很乐意学习一些关于如何正确地完成它的知识,比如最佳实践。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Marcel    8 年前

    YourController extends AbstractActionController
    {
        protected $tableGateway;
        ...
        public function sortAction()
        {
             // contains the db field, which should the result sorted by or an empty string
             $sort = $this->params()->fromQuery('sort', '');
    
             // contains the sort direction the db query should ordered by (asc or desc)
             $direction = $this->params()->fromQuery('order', '');
    
             // executes sql query with the given parameters
             $result = $this->tableGateway->getSortedResult($sort, $direction);
    
             // return result to the view
             return new ViewModel([
                 'result' => $result,
             ]);
        }
        ...
    }
    

    getSortedResult $sort $direction

    class YourTableGateway extends TableGateway
    {
        ...
        public function getSortedResult(string $sort, string $direction) : ResultSet
        {
            $select = $this->getSql()->select();
            $select->columns([
                'column_a',
                'column_b',
                'column_c',
            ]);
            $select->order($sort . ' ' . $direction);
    
            $resultset = $this->selectWith($select);
    
            return $restulset;
        }
        ...
    }
    

    <thead>
        <tr>
            <th><a href="<?= $this->url('name', [ 'action' => 'show', 'id' => 1 ], [ 'query' => [ 'sort' => 'name', 'order' => 'ASC' ]]); ?>">Table Headline 1</a></th>
            <th><a href="<?= $this->url('name', [ 'action' => 'show', 'id' => 1 ], [ 'query' => [ 'sort' => 'gender', 'order' => 'ASC' ]]); ?>">Table Headline 2</a></th>
        </tr>
    </thead>
    

    sort order query

    推荐文章