Weird CHTML::link behaivour trying to delete from a table.

For my admin view I use a table instead of the grid view. ( look at the attachment).

My weird problems is that the post of the ajax link that does the delete send me to a page tottaly diffeent of the one that I need to go.

The code of the delete button is the following.




<?php echo CHtml::link('Delete',"#", array("submit"=>array('delete', 'id'=> $usuario['id']), 'confirm' => 'Are you sure?')); ?>



I know that the parameter that is giving instead of the page is the pagination of the table ( That is why it is weird.)

Maybe the solution is as simple as changing an id in the link ? could be great really …

The url that I am getting is the following :

index.php?DataTables_Table_0_length=10

So … that is my trouble. Any help will be really appreciated :D.

There could perhaps be some special handling that I haven’t seen so far, but if not then I would assume that using the ‘submit’ html option should have no effect, since I would imagine that submit is actually destined for a form, whereas you are merely making an anchor.

If you want to have an ajax submit button, then either you will need to make a form, and add a javascript handler to something either to handle the submit or to trigger it (making sure that you cancel the default action and submit by ajax), or you just add a link, and add some javascript to it to send an ajax form on click.

I personally prefer the first option since it degrades gracefully when Javascript is not enabled.

@RedRabbit: The syntax itself is correct. The "submit" key in the htmlOptions can be used to create an ajax link: See http://www.yiiframework.com/doc/api/1.1/CHtml#clientChange-detail

@CTala: Try adding an "id" to your htmlOptions (not the parameter of the url, but an id for the link itself) like:


echo CHtml::link('Delete',"#", array("id"=>'usuario-'.$usuario['id'],"submit"=>array('delete', 'id'=> $usuario['id']), 'confirm' => 'Are you sure?'));

Most of the time the ajax problems are a direct result of non unique ids for elements that use javascript. That can cause weird bugs.

It can indeed generate an ajax form, but I think at the very least you’re going to have to add some kind of ‘params’ array to specify a POST rather than GET submit.

Otherwise, you can just copy the delete method used by CGridView (removing the gridview references of course) from CButtonColumn.

@Haensel. The same behaivour persist. I could not fix it like that :(. I was expecting that was as easy as that though :).

@RedRabbit, An Ajax button works in this situation.

Doing this it works great:




                                            echo CHtml::ajaxSubmitButton(

                                                    'Eliminar', $this->createUrl("/user/delete", array(

                                                        'id' => $usuario['id'],

                                                            //'returnUrl' => '/user/admin',

                                                            )

                                                    ), array(

                                                'type' => 'POST',

                                                'success' => 'js:window.location.reload()'

                                                    ), array(

                                                "onclick" => "confirm('Are you sure?')",

                                                    )

                                            );



The problem is that i am forcing to reload all the page. I will try to delete only the row.

But you are still not using a unique id in your ajax example. If you don’t define your own ids for the html element you create then Yii will create a random one. The problem is that it often duplicates ids when elements get loaded via ajax (like via pagination etc.).




echo CHtml::ajaxButton(

    'Eliminar', 

    $this->createUrl("/user/delete", array(

        'id' => $usuario['id'],

    )),

    array(

        'type' => 'POST',

        'success' => 'js:window.location.reload()'

        ), 

    array(

        "onclick" => "confirm('Are you sure?')",

        'id'=>$usuario['id']

    ));



Tried that?

EDIT2: Why are you reloading the page on success? Couldn’t you just render the new list partially server side and replace it via “update”=>"#yourlistid"