How to select and get the row id of Cgridvew of yii

I have been working on yii infrastructure for the last 10 days. I have done a lot in my project, but I am stuck in one problem. in the following table, when I click any row, it should get the data of the selected row in the jquery dialog, and it should open when the edit button on the table toolbar is clicked. enter image description here

as an example, when I click on any line in the listed lines, then it should stand out, and if I click the edit button, it should open a dialog form with the selected row data. here is my code .....

<table class="display" id="dt3"> <ul class="table-toolbar"> <li><a href="#" id="create-user"><img src="<?php echo Yii::app()->request->baseUrl; ?>/images/icons/basic/plus.png" alt="" /> Add</a></li> <li><a href="#"><img src="<?php echo Yii::app()->request->baseUrl; ?>/images/icons/basic/delete.png" alt="" /> Delete</a></li> <li><a href="#" id="create-user2"><img src="<?php echo Yii::app()->request->baseUrl; ?>/images/icons/basic/edit.png" alt="" /> Edit</a></li> </ul> <ul> <table class="display" id="dt4"> <tr> <?php $dataProvider=new CActiveDataProvider('Station'); $this->widget('zii.widgets.grid.CGridView', array ( 'dataProvider'=>$dataProvider, 'itemsCssClass'=>'display', 'summaryText'=>'', 'rowCssClass'=>array('odd gradeX','even gradeC'), 'htmlOptions'=>array('class'=>'display'), 'columns'=>array( array ( 'name'=>'Station Name', 'value'=>'$data->Station_Name', ), array ( 'name'=>'Status ', 'value'=>'$data->Status_value', ), array ( 'name'=>'Description ', 'value'=>'$data->Station_Description', ), array ( 'name'=>'Order ID ', 'value'=>'$data->OrderID', ), array ( 'name'=>'Updated By ', 'value'=>'$data->Updated_by', ), ), ) ); ?> </tr> </table> </ul> </table> <div id="dialog-form" title="Add/Edit Station" class="box-content" style="height: 100px"> <form action="index.php?r=setting/stations" method="POST"> <div class="form-row"> <label class="form-label">Station Name</label> <div class="form-item"> <input type="text" name="station_name" /> </div> </div> <div class="form-row"> <label class="form-label">Description</label> <div class="form-item"> <input type="text" name="station_description"/> </div> </div> <div class="form-row"> <label class="form-label">Order Id</label> <div class="form-item"> <input type="text"name="order_Id" /> </div> </div> <div class="form-row"> <label class="form-label">Updated By</label> <div class="form-item"> <input type="text" name="updated_by" /> </div> </div> <ul style="float:right;"> <li style="float:right;"><a href="#"><input type="submit" class="button small green" value="Add Station"></a></li> </ul> </form> </div> 
+4
source share
2 answers

You must add the id property to the CGridView configuration:

 'id' => 'station-grid', 

then anywhere in your javascript code you can get the currently selected lines with:

 var selected = $('#station-grid').yiiGridView('getSelection'); 

You can, for example, use the above in the click handler on each tr your grid. It is up to you how to integrate it into your javascript.

+1
source

For all verified row identifiers, we use this

  var id = $.fn.yiiGridView.getChecked("your-grid-id", "selectedIds"); // array of seleted id from grid 

If you click on a row to get an identifier like this

 var id = $.fn.yiiGridView.getSelection(grid_id); var id = id[0]; 
+1
source

Source: https://habr.com/ru/post/1497528/


All Articles