[solved] CJuiTabs

Hi guys,

i have a question regarding how to fill CJuiTabs with data.

I have two models Itemtype and Item with one-to-many relation.

Now I want to split a selected list of items into tabs labeled as their item type name like ‘tools’, ‘clothes’ etc.

I tried to work it out by reading named scopes, data provider, list view in the api, but my code looks clumsy to me, more or less hardcoding the expected item types into a bunch of data providers and the CJuiTabs widget… ;(

Is there any Yii-magic-way to do this?

TIA

Marco

Maybe i should add another info…

The Items are related to another Model: Shop

So in my controller i select a Shop, Shop->items gives me the list of the items i referred to in my original post.

If I understand you correctly, the simple way is to do something like this:




$tabs = array();

foreach($shop->items as $item)

{

	if(!isset($tabs[$item->type]))

	{

		$tabs[$item->type] = '';

	}

	$tabs[$item->type] .= $item->name . '<br />';

}

$this->widget('zii.widgets.jui.CJuiTabs', array(

	'tabs' => $tabs,

));



@derelict,

sorry, but this is a bit too simple. What i try to achieve is like this, but only not hardcoded* ;(




//controller

public function actionIndex()

 {

  $types = array('type1', 'type2', 'type3');

  foreach ($types as $type)

   {

	$criteria=new CDbCriteria;

	$criteria->with=array(

			'item.type',

	);

	$criteria->addSearchCondition('type.class', $class, true);


	$shop[$type] = Shop::model()->with('item.type')->findAll($criteria);


	$this->render('index', array('shop'=>$shop));

   }


// view

<?php


$this->widget('zii.widgets.jui.CJuiTabs', array(

		'tabs'=>array(

		 'type1'=>$this->renderPartial('_items',array('data' => $shop['type1']),true),

		 'type2'=>$this->renderPartial('_items',array('data' => $shop['type2']),true),

    ),

    // additional javascript options for the tabs plugin

    'options'=>array(

        'collapsible'=>true,

    ),

));

?>




EDIT:

* and without querying the db another three times, as i have already all Items in my Shop Model…

You may select all records and then put them into array as I show before. In this case you even need not to know type’s names that are using.

I put the values into an array and push the array into the view:




//controller

public function actionIndex()

{

	$mytabs = array();

	$content = array();


	foreach ($this->shop->inventories as $inventory)

	{

	 $class = $inventory->item->type->class;

	 $mytabs[$class][] = $inventory;

	}

  	 $this->render('index', array('model'=>$this->user, 'mytabs'=>$mytabs));

}

print_r($mytabs) :  Array ( [class1] => Array ( [0] => obj_item1 [1] => obj_item2 [2] => obj_item3 ) [class2] => Array ( [0] => obj_item4 [1] => obj_item5 ) [class3] => Array ( [0] => obj_item6 ) )



So, how can i get this array in my view into the CJuiTabs widget? I simply don’t understand your point.

I tried to get it into CJuiTabs this way:




//view

foreach ($mytabs as $label=>$value)

{

 $tab_array=array($label=>$this->renderPartial('_items',array('datas'=>$value),true));

}


$this->widget('zii.widgets.jui.CJuiTabs', array(

		'tabs'=>array(

		 $tab_array,

              ),

    // additional javascript options for the tabs plugin

    'options'=>array(

        'collapsible'=>true,

    ),

));

// _items

	<?php foreach($datas as $data): ?>

	 <?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>

	 <br />

	 <?php echo CHtml::encode($data->item->name); ?>

	 <br />

	 <?php echo CHtml::encode($data->quantity); ?>

	 <br />

	<?php endforeach; ?>



No error message, but the CJuiTabs is empty. No tabs, no content

HTML output:




<div id="yw0" class="ui-tabs ui-widget ui-widget-content ui-corner-all ui-tabs-collapsible">

<ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">

</ul>

</div>



@derelict,

i found my error.




foreach ($mytabs as $key=>$value)

{

  $tab_array[$key]=$this->renderPartial('_items',array('datas'=>$value),true);

}

$this->widget('zii.widgets.jui.CJuiTabs', array(

                'tabs'=>$tab_array,

    // additional javascript options for the tabs plugin

    'options'=>array(

        'collapsible'=>true,

    ),

));



Now it works! Thank you for giving me this tip.

Found your post about CJuiTab and it really save me.

For those who working with CJuiTabs, it is possible to setup so that the Tab label has ‘space’ in it.




$tab_array['type 1'] = $this->renderPartial('_items',array('data' => $shop['type1']),true)

$tab_array['type 2'] = $this->renderPartial('_items',array('data' => $shop['type2']),true)


$this->widget('zii.widgets.jui.CJuiTabs', array(

                'tabs'=>$tab_array,

));