创建DataObject以在SilverStripe中构建表行和列

时间:2017-05-15 13:05:21

标签: silverstripe data-objects

我正在尝试为SilverStripe站点做一些非常不同的事情:在几个子页面上是数据表,这些表每个都有自己的列标题集,有些表的列比其他表多。我想避免在富文本编辑器中构建表格,因为这会导致很多错误,并且随着时间的推移维持这是一件麻烦事。

我想要做的是创建一个DataObject,它允许第n个列和第n个相应的行。这样我可以在模板中调用一个循环(或可能是两个),我已经有了HTML表结构。内容管理员可以完全控制任何给定子页面的表格中的哪些列,并且他们不必担心维护HTML表格设置。

我有一些想法没有产生我想要的结果而没有a)使内容管理器的UI体验过于复杂,以及b)无法正确地将列与行链接。

我曾想过为Table Headers创建一个DataObject,为Table Rows创建一个DataObject,但后来我很难理解如何以一种有意义的方式组合它们,特别是因为可能有任意数量的列。 / p>

有人会有任何建议来接近这个吗?

更新:好的,我有一些可以使用的TableRowItem数据对象,并且接近工作。但是,现在的问题是:当我基本上即时创建它们时,如何将字段值保存到数据库?就像现在一样,保存到数据库的唯一字段是PDF文件上传字段,其他所有内容在点击“创建”时都会被删除。

<?php

class TruckBodyPdfTableRowItem extends DataObject {

    private static $db = array(
    );

    // One-to-one relationship with gallery page
    private static $has_one = array(
        'TablePage'=> 'Page',
        'TableColumnSet' => 'TableColumnSet',
        'PDF' => 'File',
    );


    // tidy up the CMS by not showing these fields
    public function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->removeFieldFromTab("Root.Main","TablePageID");
        $fields->removeFieldFromTab("Root.Main","TableColumnSetID");
        $fields->removeFieldFromTab("Root.Main","SortOrder");
        $fields->addFieldsToTab("Root.Main", $this->getMyColumnOptions());

        return $fields;
    }
    public function getMyColumnOptions()
    {
        $columnArray = [];
        $Columns = DataObject::get('TableColumnSet');

        foreach($Columns as $Column){
          $columnArray[] = TextField::create($Column->TableColumnHeader);
        }

        return $columnArray;
    }

    // Tell the datagrid what fields to show in the table
    private static $summary_fields = array(
    );

    public function canEdit() {
        return true;
    }

    public function canDelete() {
        return true;
    }

    public function canCreate(){
        return true;
    }

    public function canPublish(){
        return true;
    }

    public function canView(){
        return true;
    }
}

但这些是棘手的部分:弄清楚如何将值从一个DataObject映射到另一个DataObject的标签,然后根据已创建的列数自动生成第n行。

<?php

class TablePage extends Page
{
    private static $db = array(
        'H1' => 'varchar(250)',
    );

    private static $has_many = array(
        'TableRowItems' => 'TableRowItem',
        'TableColumnSets' => 'TableColumnSet'
    );

    private static $has_one = array(

    );

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $fields->addFieldToTab("Root.Main", new TextField("H1"), "Content");

        $gridFieldConfig = GridFieldConfig_RecordEditor::create();

        $gridFieldConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
            // field from drawer class => label in UI
            'TableColumnHeader' => 'Table Column Header'
        ));

        $gridfield = new GridField(
            "TableColumnSets",
            "Table Column Sets",
            $this->TableColumnSets(),
            $gridFieldConfig
        );

        $fields->addFieldToTab('Root.Specs Table', $gridfield);




        $gridFieldConfig2 = GridFieldConfig_RecordEditor::create();

        $gridFieldConfig2->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
            // field from drawer class => label in UI
            'TableRowValue' => 'Table Row Value'
        ));

        $gridfield2 = new GridField(
            "TableRowItems",
            "Table Row Items",
            $this->TableRowItems(),
            $gridFieldConfig2
        );
        $fields->addFieldToTab('Root.Specs Table', $gridfield2);



        return $fields;
    }
}

class TablePage_Controller extends Page_Controller
{
    private static $allowed_actions = array(

    );

    public function init()
    {
        parent::init();
        // You can include any CSS or JS required by your project here.
        // See: http://doc.silverstripe.org/framework/en/reference/requirements
    }
}

以下是TableColumnSet和TableRowValue类。我想,会有一组列标题与第n行相关联,所以我认为这两个类之间会有一个$has_many关系,因为TableColumnSet可能有很多TableRowValues,但只有是所有TableRowValues的一个TableColumnSet。我希望使用带有所有列标题的下拉列表将TableRowValues与TableColumnSet值相关联,但这听起来不错。必须手动将一行中的每个字段与列标题相关联,这似乎是单调且可能很困难的内容管理器。

<?php

class TableColumnSet extends DataObject {

    private static $db = array(
        'SortOrder' => 'Int',
        'TableColumnHeader'=>'varchar(250)'
    );

    // One-to-one relationship with gallery page
    private static $has_one = array(
        'TablePage'=> 'Page'
    );

    private static $has_many = array(
        'TableRowItems' => 'TableRowItem'
    );

    // tidy up the CMS by not showing these fields
    public function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->removeFieldFromTab("Root.Main","TablePageID");

        $fields->removeFieldFromTab("Root.Main","SortOrder");

        return $fields;
    }

    // Tell the datagrid what fields to show in the table
    private static $summary_fields = array(
        'TableColumnHeader' => 'Table Column Header',
    );

    public function canEdit() {
        return true;
    }

    public function canDelete() {
        return true;
    }

    public function canCreate(){
        return true;
    }

    public function canPublish(){
        return true;
    }

    public function canView(){
        return true;
    }
}

我觉得可能会在这里找到一些东西,至少在列标题和行之间的关系方面?不过我不确定。

1 个答案:

答案 0 :(得分:-1)

我可能不在这里,因为我没有使用SilverStripe的经验。但是......我的PHP / HTML表解决方案可能适用于此处:

<?php

// parse your table data into this structure
$tableData = array(
    "rowOne" => array(
        "columnName" => "columnValue1",
        "colName" => "colValue1"
        // .....
    ),
    "rowTwo" => array(
        "columnName" => "columnValue2",
        "colName" => "colValue2"
        // .....
    )
);

// now loop through the array with a printHeader parameter
$tableHTML = array(
    "<table>"
);
$tableHead = array(
    "<thead>"
);
$tableBody = array(
    "<tbody>"
);
$printHeader = true;

foreach ($tableData as $row) {
    foreach ($row as $column => $value) {
        $tableRow = "<tr>";
        if ($printHeader) {
            $tableHead[] = "<th>".$column."</th>";
        }
        $tableRow .= "<td>".$value."</td>";
    }
    $tableBody[] = $tableRow."</tr>";
    // after the first row, set printHeader to false and close the <thead>
    $printHeader = false;
    $tableHead[] = "</thead>";
}

 // implode table header to string with linebreaks
$tableHead = implode(PHP_EOL, $tableHead);

// close table <tbody> & implode to string with linebreaks
$tableBody[] = "</tbody>";
$tableBody = implode(PHP_EOL, $tableBody);

// add all table elements together
$tableHTML[] = $tableHead;
$tableHTML[] = $tableBody;
$tableHTML[] = "</table>";
// implode table array to string
$tableHTML = implode(PHP_EOL, $tableHTML);

// print or write anywhere
echo($tableHTML);

?>

循环中所有步骤的数组结构是使默认服务器内存更清晰,以删除旧数据。如果将所有引用连接($var .= "string";)作为字符串,则所有引用都将保留在内存中,并在显示大表时陷入服务器。 我希望这有一些帮助