Cgridview selected row column value in Yii - yii

Using below code im getting all "Name" Column values
$.fn.yiiGridView.getColumn("CGridViewUser",1).text();
if i want to get selected row "Name" column value (like shown in image), how to achevie this?

First add an ID to your GridView.
<?php
$this->widget('zii.widgets.grid.CGridView', array
(
'dataProvider'=>$dataProvider,
'htmlOptions'=>array('id'=>'MyID'), //MyID is an ID to grid wrapper
........
........
Now, jQuery
$("#MyID table tbody tr").click(function()
{
$this=$(this);
var firstColVal= $this.find('td:first-child').text();
var secondColVal= $this.find('td:nth-child(2)').text();
var lastColVal= $this.find('td:last-child').text();
alert(firstColVal);
});
It will work.

Related

Get 'data-sort' orthogonal value from DataTables within search.push function

I am looping the rows within $.fn.dataTable.ext.search.push function to select some rows based on many criteria. I am setting some values on the TD of the tables known as orthogonal data. I am trying to get the value of the 'data-sort' but I don't know how. I am able to get the cell's inner data via data[2] (for column 2), but not the 'data-sort' or 'data-filter'. Any ideas?
$.fn.dataTable.ext.search.push(
function (settings, data, dataIndex) {
var iRating = parseFloat(data[2]) || 0; // this works
var datasort = //somehow get the data-sort from the TD
);
HTML
<td data-sort="57000" class=" seqNum">.....</td>
Looks like this way I can get the value. If there are any other better ways please advice:
$(settings.aoData[dataIndex].anCells[2]).data('sort')
Yes there is an easier way.
The data parameter is the "search data" for the row. i.e. the values for "filter" data for each col - not your "sort" data / anything else.
But you also have access to the full data object for the row i.e. the 4th parameter - rowData.
So you need to use rowData at the index of the column you want (you say 'for column 2', zero based so - 2 would be for the 3rd column).
Then you should find a property called 'sort' :
function(settings, searchData, index, rowData, counter){
var dataSort = rowData[1]['sort'];
console.log(`This should be the value you want : ${dataSort}`);
}
As per the documentation here

Calling a function from the view in yii

I have this function in my Courses model which grabs the department_id from the url and displays courses based on the department_id
public function loadcourses()
{
$data=Courses::model()->findAll('department_id=:department_id',
array(':department_id'=>(int) $_GET['department_id']));
$data=CHtml::listData($data,'id','course_name');
//echo "<option>Select Courses</option>";
foreach($data as $value=>$course_name){
echo CHtml::tag('input type= "checkbox"' , array('value'=>$value,'name'=>'course_id[]'), CHtml::encode($course_name),true);
}}
I need it to display in the view as a checkbox so the value can be submitted
i tried this
<?php
Courses::model()->loadcourses()
echo $form->checkboxList($model,'course_id',array()
);?>
the check boxes display but don't carry any value and it Posts an empty value, I need the values to save in the course_id field

get value of CHtml::activeDropDownList in yii

I have this code
<?php echo CHtml::activeDropDownList(
$semaineModel,
'libelleSemaine',
CHtml::listData(Semaine::findBySql('SELECT * FROM Semaine')->all(), 'idSemine', 'libelleSemaine')
); ?>
but why that displays just the last element of the table, and me I have 6 items in this table 'Semaine 1' to 'Semaine 6'
and that code display just 'Semaine 6'.
an idea please ?
Then you don't nedd CHtml but active dropDownList
Assiming your Semain model is named Semain
use app\models\Semaine;
use yii\helpers\ArrayHelper;
$semaines=Semaine::find()->all();
$listSemaines = $listData=ArrayHelper::map($semaines,'idSemine', 'libelleSemaine');
echo $form->field($model, 'idSemaine')->dropDownList( $listSemaines,
['prompt'=>'Select Semaine...']);

How to put contrasting information into a CGridView column based on a condition?

I'm looking into showing/hiding specific column data on a CGridView widget for the Wii Framework.
I have a CButtonColumn which contains 3 buttons. However, on certain conditions, I want to display something different for a particular row.
I have 3 different conditions which determin what gets displayed for particular row.
The following illustrates what I want to do:
| 1 | Title A | [hide][view][update] <-- if (condition == 'a')
| 2 | Title B | [hide][view][update] <-- if (condition == 'a')
| 3 | Title C | display text or link or button <-- if (condition == 'b')
| 4 | Title D | display alternative buttons <-- if (condition == 'c')
What is my best approach to take here?
I can't use 'visible'=> $model->processingStatus != "processed" on the column because this will remove the whole column. I need to target each row insatead.
Should I use the 'visible' parameter on each individual button? I have tried this using the commented out code below but it breaks the page.
FYI: I have successfully tried the 'visible' parameter on the CButtonColumn itself, but its not what I need. Plus not sure which row's status it is reading.
Or should I add a function to the controller? Have it do the if/else statements and return back what is to be displayed. How would this work?
Here is my code:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'my-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name'=>'myid',
'header'=>'ID',
),
'Title',
array(
'class'=>'CButtonColumn',
'visible'=> $model->status != "done",
'template'=>'{hide}{view}{update}',
'buttons'=>array(
'hide'=>array(
'label'=>'Hide', //Text label of the button.
'imageUrl'=>Yii::app()->request->baseUrl . '/img/icons/bulb-off.png' //Image URL of the button.
//'click'=>'function(){alert("Toggle Hide!");}', //A JS function to be invoked when the button is clicked.
//'options'=>array(), //HTML options for the button tag.
//'url'=>'javascript:void(0)', //A PHP expression for generating the URL of the button.
//'visible'=> $model->status == "done", //A PHP expression for determining whether the button is visible.
),
'view'=>array(
//Text label of the button.
'label'=>'View',
//Image URL of the button.
'imageUrl'=>Yii::app()->request->baseUrl . '/img/icons/view-record.png'
),
'update'=>array(
'label'=>'Update/Edit',
'imageUrl'=>Yii::app()->request->baseUrl . '/img/icons/edit-pencil.png',
'url'=>'Yii::app()->createUrl("metadataandchapters/create?bookid=" . $data->bookid)',
)
)
)
)
)); ?>
Hope I am making good enough sense here!
You should use visible button option, but it should be a PHP expression string, e.g. :
'visible'=> '$data->status == "done"',
http://www.yiiframework.com/doc/api/1.1/CButtonColumn#buttons-detail
Extend CButtonColumn with your own class, then you should be able to change this function to whatever you need to render or hide buttons or do any changes you want.
/**
* Renders a link button.
* #param string $id the ID of the button
* #param array $button the button configuration which may contain 'label', 'url', 'data-icon', 'imageUrl' and 'options' elements.
* #param integer $row the row number (zero-based)
* #param mixed $data the data object associated with the row
*/
protected function renderButton($id, $button, $row, $data)
More details about the function http://www.yiiframework.com/doc/api/1.1/CButtonColumn#renderButton-detail

using listdata only and get from another model in Yii

can i just have listdata by itself? The list doesn't list anything, all I keep getting is "Array" as the return.
I have:
$awards= $model->findAll('uid='.$uid);
return CHtml::listData($awards, 'award_id', '$data->award->name');
try this code:
$awards= $model->findAll(array('condition'=>'uid ='.$uid)); // get records from table
return CHtml::listData($awards, 'award_id', 'award_name'); // third parameter should the array value. Most of the case we use to display in combo box option text.
If you are getting from another table, then declare a variable in your award model.
public $awrd_name;
Then you have to get records using relation in criteria.
Output will be :
array("1" => "award1", "2" => "award2", "3" => "award3");
refer this link : http://www.yiiframework.com/forum/index.php/topic/29837-show-two-table-fields-in-dropdownlist/