Mohammad
Mohammad

Reputation: 3547

how to run yii widget when button clicked

I have this widget:

<?php

    class Search extends CWidget
    {
        public $dataProvider = null;
        public function init()
        {
            $criteria = new CDbCriteria();

                if ( isset($_GET['file']) ) {
                    $criteria->compare('fileName', $_GET['file'], true, 'OR');
                    $criteria->compare('tags', $_GET['file'], true, 'OR');
                }

                $this->dataProvider = new CActiveDataProvider("Files", array(
                    'criteria' => $criteria,
                    //'countCriteria' => $criteria,
                    'pagination'=>array(
                        // results per page
                        'pageSize'=>1,
                    ),
                ));
        }

        public function run(){
        $this->render('site/result', array(
                    'dataProvider' => $this->dataProvider,
                    'pages' => $this->dataProvider->pagination,
                ));
        }
    }
    ?>

how I can run this widget only when the search button is clicked, and how to view its result in the view reuslt that is located in the folder site

Upvotes: 0

Views: 654

Answers (1)

Telvin Nguyen
Telvin Nguyen

Reputation: 3559

Maybe I misunderstood you, but I think you have a little confusion about widget. Avoid making a
lengthy answer, I made a model in below

enter image description here

Question 1: How I can run this widget only when the search button is clicked?

The idea is that you put the widget content into hidden div, and then add a jQuery script to show it after the search button is clicked

<div id='search-result' style="display:none"><?php $this->widget(...) ?></div>
<script>
$('#search-button-id').click({
// do something
$('#search-result').show();
})</script>

Question 2: how to view its result in the view reuslt that is located in the folder site?

Imao, I recommend you don't do this. The widget is independant part, you can use it for many specific purposes & many locations of your project, so just put a simple view for widget to render in its own, instead of trying to render a view outside widget's scope. It should look like below

/your-app/protected/extensions/widgets/search/Search.php
/your-app/protected/extensions/widgets/search/views/result.php // view for rendering

Upvotes: 2

Related Questions