stand
stand

Reputation: 139

Button click yii2

I have this button in view:
<button name="render_button" class='btn btn-block btn-primary' style="width:100px;height:30px;" > Render </button><br>
How to set up call actionIndex with certain function, for example, if button_1 is clicked then call actionIndex with getSome();, like this:

public function actionIndex()
{
    if(isset($_POST['render_button'])) {
        $data = order::getSome();
    }

    $dataProvider = new ArrayDataProvider([
        'allModels' => $data,
        'sort' => [
        ],
        'pagination' => [
            'pageSize' => 50,
        ],
    ]);

    return $this->render('index',['dataProvider' =>$dataProvider]);
}

Upvotes: 1

Views: 590

Answers (1)

Christos Lytras
Christos Lytras

Reputation: 37308

You should set an initial value to the $data variable, a value like null and then pass it to the $dataProvider array like this:

public function actionIndex()
{
    $data = null;
    if(isset($_POST['render_button'])) {
        $data = order::getSome();
    }

    $dataProvider = new ArrayDataProvider([
        'allModels' => $data,
        'sort' => [
        ],
        'pagination' => [
            'pageSize' => 50,
        ],
        'data' => $data
    ]);

    return $this->render('index',['dataProvider' =>$dataProvider]);
}

Now, inside your view, you just check whenever the $data variable is null or else you get the trigger and have order::getSome() data strored in the $data variable.

EDIT

Didn't see you are passing the $data variable to the allModels item, but it's the same. Just check the allModels inside your view whenever it's set to null or it has some data.

UPDATE

Update your view index.php to have a post form like this:

<form method="post">
    <button type="submit" name="render_button" class='btn btn-block btn-primary' style="width:100px;height:30px;" > Render </button><br>
</form>

Upvotes: 1

Related Questions