Yii Framework Forum: CJuiSortable - trouble with 'update' in options array - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CJuiSortable - trouble with 'update' in options array Rate Topic: -----

#1 User is offline   locomo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 03-April 11

Posted 03 April 2011 - 12:00 PM

I'm trying to update my category's 'position' in my database tables when a new sort takes place via ajax.

This post got me most of the way I think but I'm still having some trouble.

I have a partial that gets rendered when viewing a 'Store':
_categories.php
<?php 
$items = array(); 
foreach($categories as $category) {
	$items['id_'.$category->id] = $category->title;
}
$this->widget('zii.widgets.jui.CJuiSortable', array(
    'id'=>'items',
    'items'=>$items,
    // additional javascript options for the accordion plugin
    'options'=>array(
                'delay'=>100,
		'update'=>"js:function(){
			$.ajax({
				type: 'POST',
               			url: '{$this->createUrl('store/sortCategories')}',
                		data: $(this).sortable('serialize'),
			});
		}",
	),				
));
?>


then I have this action in my controller for 'Store':
StoreController.php
	public function actionSortCategories()
	{
	    echo "<script>alert('yo');</script>";
    	    Store::model()->updateCategoryOrder($_POST['id']);
	}


finally I have this method in my model for 'Store':
Store.php
	public function updateCategoryOrder()
	{
		foreach ($_GET['id'] as $position => $item)
		{
			$category = Category::model()->findByPk($item);
			$category->position = $position;
			return $category->save();
		}
	}


I think everything looks right, but I'm very new to Yii - I can sort my items but the 'update' ajax function never seems to get invoked and so the 'sortCategories' action never gets invoked in my controller (my 'yo' alert is never triggered).

Any ideas?
0

#2 User is offline   locomo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 03-April 11

Posted 07 April 2011 - 06:02 PM

help?
0

#3 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,545
  • Joined: 12-October 09
  • Location:Croatia

Posted 08 April 2011 - 03:16 AM

In my opinion... even if the ajax is made... the alert() would not be fired as you are just echoing it...

To be sure if the ajax works... you would need to check with firefox / firebug (or something similar - chromebug,etc)...
Find more about me.... btw. Do you know your WAN IP?
0

#4 User is offline   Igor Ivanovic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 17-October 10
  • Location:Zagreb

Posted 08 April 2011 - 05:06 AM

Ajax data has to be returned thats why your ( 'yo' alert is never triggered);
And ajax only recognize data, your js functions on other page will never be triggered by ajax;
0

#5 User is offline   locomo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 03-April 11

Posted 08 April 2011 - 10:23 AM

great - thanks .. all set now
0

#6 User is offline   hydr0gen 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 26-October 11

Posted 25 November 2011 - 06:38 AM

Hmm... Strange problem... It should work, anyway I'm using this code and it's fine:


            $.ajax({
                type: 'POST',
                url: '{$this->createUrl('category/saveorder')}',
                data: 'th=' + $(this).sortable('toArray').toString()

            });

0

#7 User is offline   wepa 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 05-April 12

Posted 10 April 2012 - 03:34 PM

View Postlocomo, on 08 April 2011 - 10:23 AM, said:

great - thanks .. all set now


hi, how do you solved the problem? I'm getting this error 400 CHttpException
0

#8 User is offline   natb19 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 10-January 12

Posted 20 July 2012 - 05:51 AM

View Postwepa, on 10 April 2012 - 03:34 PM, said:

hi, how do you solved the problem? I'm getting this error 400 CHttpException


The way I sorted it out using console.log in firebug (recommended!) to figure what was going on:

	'update'=>"js:function(){
		$.ajax({
			type: 'POST',
			url: '{$this->createUrl('sortitems')}',
			data: {'items[]':$(this).sortable('toArray')},
		});
	}",


and then in the controller:

	public function actionSortItems()
	{
	    if (isset($_POST['items']) && is_array($_POST['items'])) {
		foreach($_POST['items'] as $index=>$id){
		    $item = Item::model()->findByPk($id);
		    $item->order = $index;
		    $item->save();
		    echo "<p>$index : $id</p>";//this isn't needed, but useful for checking that the data was received ok
		}
	    }
	    exit;//exit here so the response is kept short, you could always return a confirm message
	}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users