I have a CGridView with a list of clients/events. For each row (EventClient) I wanted a quick edit dialog.
I used Zaccarias excellent article as the base http://www.yiiframework.com/wiki/145/cjuidialog-for-create-new-model/
First follow the wiki above to create all the required code. Then make the following modifications in your CGridView page:
For each column, set the _updateComment_url property in the Javascript function to the required url
array( 'name'=>'comment', 'header'=>'Comments', 'type'=>'raw', 'value'=>'CHtml::link( ($data["comment"]?$data["comment"]:"(comment)"), "", array( \'style\'=>\'cursor: pointer; text-decoration: underline;\', \'onclick\'=>\'{ updateComment._updateComment_url="\'. Yii::app()->createUrl( "eventClient/updateComment", array("id"=>$data["id"]) ) .\'"; updateComment(); $("#dialogComment").dialog("open");}\' ) );', ),
In the same page include the updateComment() function that calls the action
<script type="text/javascript"> function updateComment() { // public property var _updateComment_url; <?php echo CHtml::ajax(array( 'url'=>'js:updateComment._updateComment_url', 'data'=> "js:$(this).serialize()", 'type'=>'post', 'dataType'=>'json', 'success'=>"function(data) { if (data.status == 'failure') { $('#dialogComment div.divComment').html(data.div); // Here is the trick: on submit-> once again this function! $('#dialogComment div.divComment form').submit(updateComment); } else { $('#dialogComment div.divComment').html(data.div); setTimeout(\"$('#dialogComment').dialog('close') \",2000); // Refresh the grid with the update $.fn.yiiGridView.update('event-client-grid'); } } ", )); return false; } </script>
Total 7 comments
I use a slightly different approach. I simply add a special class to all a elements, e.g. "ajax-modal". Then I attach an on click event to all a.ajax-modal, and use the href to load the dialog content.
We can try to improve the style a bit more with the link, by changing like that:
This allows you to use a standard link with attached the function. If the updateComment function is thought for work in both ajax/submit way, users without javascript enabled will be able to update comment using post (like CGridView, wich works with ajax but has also a fallback)
I just made a reply on your post:
http://www.yiiframework.com/forum/index.php?/topic/20273-passing-parameters-to-an-ajax-link/page__gopid__99408#entry99408
Dont delete it, it still solves the problem, but you can inprove it, its what I said :)
Should I delete this wiki instead?
Oh I totally agree with you, but I've been trying to solve this for days. This was the only solution I could find that would work :)
If you know a better way I would be very grateful :) http://www.yiiframework.com/forum/index.php?/topic/20273-passing-parameters-to-an-ajax-link/page__pid__99380#entry99380
Thanks, Russ
I know its about style, but...
You shouldnt have much inline js nor you should have functions and least of all global variables. perhaps you could make use of jQuery events, selectors and external files, talk about registerScript or registerSCript file.
I know readers can take your code and make whatever they want with it, but I beleave that when writing a wiki you should advice readers to best practices, thats because most readers will copy paste your code and modify to their needs leading to... global variables inline js, etc
Just my opinion.
Leave a comment
Please login to leave your comment.