raoul2000 / yii2-workflow

A simple workflow engine for Yii2
BSD 3-Clause "New" or "Revised" License
171 stars 48 forks source link

Gridview filter dropdown #65

Closed ferllings closed 3 years ago

ferllings commented 3 years ago

Hello,

On my index page, I have a GridView widget, using an ActiveDataProvider object (from ->search() ) So I don't have any model object. So, I did a bit of 'hack' to get the full list of statues:

$model = MyModel::find()->one();
    $allStatuses = WorkflowHelper::getAllStatusListData(
        $model->getWorkflow()->getId(),
        $model->getWorkflowSource()
    );

GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns'=>[
        [
            'attribute' => 'status',
            'filter' =>$allStatuses,
        ]
]
]);

Is there a better way to generate the dropdown filter menu that this?

Thanks

raoul2000 commented 3 years ago

Hi, given a workflow Id, one option could be to directly get the list of all statuses from the workflowSource component (which wouldn't force to read a model from the DB). However this would be possible only if you're sure that MyModel is only associated with only one workflow, and that you know the ID of this workflow. By default the workflowSource component has name "workflowSource".

Once you have a reference to the workflowSource just call getAllStatuses (see workflowHelper for example)

Hope it helps

ferllings commented 3 years ago

Thanks that works well:

    $workflowSource = Yii::$app->get('workflowSource');

    $allStatuses = WorkflowHelper::getAllStatusListData(
        'MyWorkflow',
        $workflowSource
    );