十月CMS |如何从特定页面上安装的组件中获取属性值?

时间:2017-03-11 11:18:49

标签: php ajax laravel octobercms octobercms-backend

我使用“perPage”选项创建了我的组件,它接受一个数字并设置每页工作显示器的限制...

enter image description here

public function defineProperties()
    {
        return [
            'perPage' => [
                'title' => 'Number os works per page',
                'description' => 'How many works do you want to display per page?',
                'default' => 9,
                'validationPattern' => '^[0-9]+$',
                'validationMessage' => 'Only numbers allowed'
            ],
            'sortOrder' => [
                'title' => 'Order of Works',
                'description' => 'How do you want to order the actors',
                'type' => 'dropdown',
                'default' => 'newest',
            ],
        ];
    }

在GET实现方面做得很好......但是在我的项目中我需要在任何地方实现AJAX,所以我需要通过AJAX加载页面,我需要知道在组件上设置的数字...我怎么样可以得到这个号码吗?

//my layout code
function onOpenWorkList()
{

    $this['works'] = Category::where('slug', input('slug'))->first();

    $this['categories'] = Category::all();

    $this['active_category'] = input('slug');

    $this['perPage'] = "";  // HERE IS THE CODE

    return [
        '.home_categories' => $this->renderPartial('work_list_categories_post'),
        '.container' => $this->renderPartial('work_list')
    ];
}

enter image description here

2 个答案:

答案 0 :(得分:0)

在您的组件文件中:

public $perPage;

/* ... */

public function init()
{
    $this->perPage = $this->property('perPage');
}

然后,在布局中,您可以这样做:

function onOpenWorkList()
{
    $this['works'] = Category::where('slug', input('slug'))->first();

    $this['categories'] = Category::all();

    $this['active_category'] = input('slug');

    $_component = $this->components['builderList']; // Get the component

    $this['perPage'] = $_component->perPage; // Get $perPage from component

    return [
        '.home_categories' => $this->renderPartial('work_list_categories_post'),
        '.container' => $this->renderPartial('work_list')
    ];
}

就个人而言,我只是将onOpenWorkList()函数放入组件文件中(而不是布局代码中),因为它仍然可以通过ajax访问它,就像它在那里一样容易,然后你不必像上面的例子那样获取组件。

答案 1 :(得分:0)

应在onEnd函数中使用PHP区域中的Builder组件获取属性:

    function onEnd()
    {
        $component = $this->page->components['builderDetails']->record
    }