0

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

Community
  • 1
  • 1
Magician
  • 1,944
  • 6
  • 24
  • 38
  • 1
    Finding it a bit difficult to follow as to what you are after? Can you sktech up the final look you are after and post it? secondly the filteration for user... does this mean that you want to show records for a certain user only i.e. after the used has loggedin??? – Orlymee Oct 05 '12 at 01:58
  • You should be able to display these items in the default CGridView using Gii, you can extend the columns to include items from other tables, like [in this example](http://stackoverflow.com/questions/9117099/showing-attributes-of-another-model-in-cgridview-in-yii#answer-9138563), then it's just a matter of filtering by that column? – Stu Oct 05 '12 at 07:23
  • Did you try do it yourself? And what's errors you got? – Sergey Oct 05 '12 at 07:59
  • edited a bit to make it more clear. Those 3 views are what I was after. I don't even know where to start. I found some filter at CGridView, then FindAll('condition'), but none works. I got error can't find getData() when I send $agenda = Agenda::model()->findAll('task_type_id = 1 AND due_at BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 1 WEEK)') to CGridView, and when I use filter property on CGridView it shows input boxes which I tried to avoid. I don't want user to specify their own filter on this page or input anything. Just plain view of what is coming. – Magician Oct 05 '12 at 18:42

1 Answers1

1

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.

bool.dev
  • 17,508
  • 5
  • 69
  • 93