Magician
Magician

Reputation: 2113

display filtered table in yii

This is may be a stupid question, but I am kind of new in yii, and had problem with this simple view.. I have a data like this:

task_type

id  name
1   agenda
2   task
3   notes

agenda:

id  user    due_at  created_at  task_type_id    description

And I would like to display them separately, as agenda and tasks, and notes in different html views like these:

tasks

due_at  description

agenda

due_at  created_at  description

notes

description

I don't want any fancy user filtering as these tables only shows one week max on front page, for current week, and there will be separate page dedicated for this sort of tasks. This is only at front page as quick tasks. I have been trying on CGridView but can't filter it without letting html filter to show up. I need to filter at due_at, and ranged due_at for agenda, and then by user.

Can someone help me to create this?

Thank you in advance

Upvotes: 0

Views: 688

Answers (1)

bool.dev
bool.dev

Reputation: 17478

There are a few things that you need to take care of, if you want to use a CGridView for this, and they are the following:

  1. Use a DataProvider : CGridView requires a data provider as its input. A data provider is:

    Data providers are components that can feed data for widgets such as data grid, data list. Besides providing data, they also support pagination and sorting.

    For your current requirement you can use a CActiveDataProvider. So your gridview code needs this:

    $this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider'=>$dataProvider,
    ));
    

    Pass this data provider from the controller action to the view.

  2. Use criteria: You can pass all those conditions in the criteria property of CActiveDataProvider. This criteria is an object of CDbCriteria. So your $dataProvider can be something like:

    $dataProvider=new CActiveDataProvider('Agenda',array(
        'criteria'=>array(
            'condition'=>'task_type_id=1 AND due_at BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 1 WEEK)',
            'select'=>'due_at, description' // columns you want to select
        )
    ));
    

    The condition property is the:

    query condition. This refers to the WHERE clause in an SQL statement. For example, age>31 AND team=1.

    The select property is:

    the columns being selected. This refers to the SELECT clause in an SQL statement. The property can be either a string (column names separated by commas) or an array of column names. Defaults to '*', meaning all columns.

  3. Don't specify the filter: To not show a filter, just don't pass any filter property value to the grid:

    $this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider'=>$dataProvider,
        //'filter'=>$model // don't pass any filter
    ));
    
  4. Use columns: Use the columns property of CGridView to show only the specific columns you want to show:

    $this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'agenda-grid',
        'dataProvider'=>$dataProvider,
        'columns'=>array(
            'due_at',
            'description'
        )
    ));
    

    Also pass an id which will become the id of this grid's div, incase you don't want the auto-generated id.

That's it, you should get what you want by following the above steps.

Upvotes: 1

Related Questions