代码之家  ›  专栏  ›  技术社区  ›  The50

扩展后端列表对象

  •  1
  • The50  · 技术社区  · 7 年前

    如何扩展后端中显示的列表项?

    我正在尝试使用此功能:

    listExtendRecords($records)
    

    问题是,我需要重新创建相同的object$记录,但我想向其中添加自定义数据。例如,我的记录是来自JK商店插件的订单。我需要的是从这些订单中获取所有产品,并将每个产品作为不同的列表项。

    我可以在这个函数中进行这些更改,只返回$记录,但是我如何在这里创建一个新的items对象呢?我尝试使用:

    $new = new Production();
    return $new;
    

    但我得到:

    调用未定义的方法October\Rain\Database\QueryBuilder::currentPage()

    如何创建可以返回到后端列表的新工作对象?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Hardik Satasiya    7 年前

    嗯,从你想展示的问题来看 related items ( 订单产品 ) under the 订单记录'it self,

    似乎使用扩展是不可能的,或者如果我们使用扩展,它会变得更难/更复杂

    相反,我们可以使用 small trick

    我们将 override partial 对于 list 对于特定列表,我们可以使用 config_list.yaml

    有了这个 order 列表配置添加此附加选项

    ....
    toolbar:
    buttons: list_toolbar
    search:
        prompt: 'backend::lang.list.search_prompt'
    recordUrl: 'hardiksatasiya/demotest/demo/update/:id'
    // add customViewPath
    customViewPath: $/hardiksatasiya/demotest/controllers/demo/list_override
    

    现在我们重写2个部分(我从 modules\backend\widgets\lists\partials )

    _list_body_rows.htm
    _list_body_row.htm
    

    我修改了这些内容

    _列出\u body\u行。htm公司

    <?php foreach ($records as $record): ?>
        <?= $this->makePartial('list_body_row', [
            'record' => $record, 
            'treeLevel' => $treeLevel, 
            'custom' => isset($custom) ? true : false]) 
    ?>
    <?php endforeach ?>
    

    _list\u body\u行。htm公司

    <?php
        $expanded = $showTree ? $this->isTreeNodeExpanded($record) : null;
        $childRecords = $showTree ? $record->getChildren() : null;
        $treeLevelClass = $showTree ? 'list-tree-level-'.$treeLevel : '';
    ?>
    <tr class="<?= $treeLevelClass ?> <?= $this->getRowClass($record) ?>">
        <!-- we are using that custom variable here we dont want to show check box for our products-->
        <?php if ($showCheckboxes && $custom == false): ?>
            <?= $this->makePartial('list_body_checkbox', ['record' => $record]) ?>
        <?php endif ?>
    
        <?php if ($showTree): ?>
            <?= $this->makePartial('list_body_tree', [
                'record' => $record,
                'expanded' => $expanded,
                'childCount' => $record->getChildCount()
            ]) ?>
        <?php endif ?>
    
        <!-- we are using that custom variable here 
            and make our row seperatly as we need
            for all item/product record this partial executed so
            we code it for single row it will be repeated through all product items automatically-->
        <?php if($custom): ?>
            <td> <!-- checkbox column we make it blank--> </td>
            <!-- 
                colspan based on requirement 
                you can fully customize your td tags from here
            -->
            <td colspan="<?= count($columns) ?>"> <a href="/backend/products/edit/<?= $record->id ?>"> <?= $record->name ?> </a></td>
    
        <?php else: ?>
    
            <?php $index = $url = 0; foreach ($columns as $key => $column): ?>
                <?php $index++; ?>
                <td class="list-cell-index-<?= $index ?> list-cell-name-<?= $column->getName() ?> list-cell-type-<?= $column->type ?> <?= $column->clickable ? '' : 'nolink' ?> <?= $column->cssClass ?>">
                    <?php if ($column->clickable && !$url && ($url = $this->getRecordUrl($record))): ?>
                        <a <?= $this->getRecordOnClick($record) ?> href="<?= $url ?>">
                            <?= $this->getColumnValue($record, $column) ?>
                        </a>
                    <?php else: ?>
                        <?= $this->getColumnValue($record, $column) ?>
                    <?php endif ?>
                </td>
            <?php endforeach ?>
    
        <?php endif; ?>
    
        <?php if ($showSetup): ?>
            <td class="list-setup">&nbsp;</td>
        <?php endif ?>
    </tr>
    
    <?php if ($showTree && $expanded): ?>
        <?= $this->makePartial('list_body_rows', ['records' => $childRecords, 'treeLevel' => $treeLevel+1]) ?>
    <?php endif ?>
    
    <!-- you can customise this condition basde on your order have items or not
    i used simple relation condition here -->
    <?php if ($record->relation): ?>
    
        <?php $childRecords = is_array($record->relation) ? $record->relation : [$record->relation]; ?>
        <?php
            // currently as $childRecords i used relation but you can create your own model array
            // and pass here it will be received in next iteration
            // notice we are passing $custom variable and we also override
            // _list_body_rows.htm and it will loop through all records and pass
            // $custom  variable and it will be true based on its existance
            // if we pass custom variable it will pass it also with true other wise
            // it will pass it false
        ?>
    
        <?= $this->makePartial('list_body_rows', ['records' => $childRecords, 'treeLevel' => $treeLevel+1, 'custom' => true]) ?>
    <?php endif ?>
    

    它会给你这样的输出

    enter image description here

    我也有 added comments 在里面 new paritals 如果您需要mroe信息,请评论如何使用它们。