V
V
Vitaly2017-03-31 09:39:03
Yii
Vitaly, 2017-03-31 09:39:03

Displaying a table as a tree?

I have a table that looks like this

<?php Pjax::begin(); ?>
    <?= GridView::widget([
        'filterUrl' => Url::toRoute('index'),
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            'id',
            [
                'attribute'=>'company_types__alias',
                'label'=> $Package->t('', 'Тип'),
                'format'=>'text',
                'content'=>function($data){
                    if ($data->type != null)
                    return $data->type->title;
                },
                'filter' => CompanyTypes::getList()
            ],
            'alias',
            'name',
            'email',
            'phone',
            [
                'class' => 'yii\grid\ActionColumn',
                'template' => '{view} {update} '
            ],
        ],
    ]); ?>
    <?php Pjax::end(); ?>

as a result, at the output I get an ordinary table, and I need to display it in the form of a tree. I figured out how to make an ordinary table tree-like, I found a plugin here, everything is ok. But there is a question to a markup of an output. Initially, yii outputs the table table tbody tr td, and in addition to all this, I need to have such a structure
<ul id="tree">

    <li>

        <table>
            <thead>
            <th>Column</th>
            <th>Column1</th>
            <th>Column2</th>
            <th>Column3</th>
            </thead>
            <tbody>
                <tr>
                    <td>cell 1</td>
                    <td>cell 2</td>
                    <td>cell 3</td>
                    <td>cell 4</td>
                </tr>

            </tbody>
        </table>

        <ul>
            <li>
                <table>
                    <tbody>
                        <tr>
                            <td>cell 1</td>
                            <td>cell 2</td>
                            <td>cell 3</td>
                            <td>cell 4</td>
                        </tr>
                    </tbody>
                </table>

                <ul>
                    <li>
                        <table>
                            <tbody>
                                <tr>
                                    <td>cell 1</td>
                                    <td>cell 2</td>
                                    <td>cell 3</td>
                                    <td>cell 4</td>
                                </tr>
                            </tbody>
                        </table>
                        <ul>
                            <li>
                                <table>
                                    <tbody>
                                    <tr>
                                        <td>cell 1</td>
                                        <td>cell 2</td>
                                        <td>cell 3</td>
                                        <td>cell 4</td>
                                    </tr>
                                    </tbody>
                                </table>
                                <ul>
                                    <li>
                                        <table>
                                            <tbody>
                                            <tr>
                                                <td>cell 1</td>
                                                <td>cell 2</td>
                                                <td>cell 3</td>
                                                <td>cell 4</td>
                                            </tr>
                                            </tbody>
                                        </table>
                                    </li>
                                </ul>
                            </li>
                        </ul>
                    </li>
                </ul>
            </li>
</ul>

That is, the table itself should be wrapped in ul li and each child in turn (each row of the standard table that yii forms) should be wrapped in ul li table tbody tr (the line generated by YII, the descendant itself)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ilya Karavaev, 2017-03-31
@Quieteroks

And how do you think GRID should learn about descendants?
In fact, everything is simple. You just need to inherit from grid and rewrite the display functions as you need. With a preliminary breakdown into parents and descendants, followed by a recursive output.

M
Maxim Timofeev, 2017-03-31
@webinar

Write your dataProvider and your widget for output. Perhaps it's worth not to force the gridview, but the listView?

I
Ivan, 2017-03-31
@LiguidCool

I'm not familiar with yii2, but I think it's not a fact that he can do that. In general, recursion is often needed to display a tree - do the output manually.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question