Bug with ajax and jquery [SOLVED]

Hi everyone,

When i want to add a jquery library in order to trunc a long text, the text is trunced very well. But as soon as i sort a column in the CGridView, the text is not trunced anymore (the less and the more link diseappear). Its seems like the javascript file is not recognized when i sort a column.

Here is is my code:


// i register my javascript file as follow:


Yii::app()->clientScript->registerScript(Yii::app()->baseUrl."/scripts/trunc.js");


Yii::app()->clientScript->registerScript('sample1', "

        $('.sample1').truncated();

");




$this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider'=>$dataProvider,

    'columns'=>array(

        array(         

            'name'=>'description',

            'value'=>'$data->truncedDescription',

        ),

        array(            // display a column with "view", "update" and "delete" buttons

            'class'=>'CButtonColumn',

        ),

    ),

));



Do you have an idea how can i fix this bug ?

Thanks for your help

it is not a bug, the problem arises when you do an ajax call the DOM object that you truncate is renewed in the DOC and your $(’.sample1’).truncated(); is not working anymore.

In order to maintain my events within the page I always use .live() -since jquery 1.4.1 you can do custome events.

$(’.sample1’).live(‘myevent’,function(e, nam, val){$(this).truncated();});

It will truncate your text… even through AJAX calls

Thanks antonio, i will try this right now

Use livequery plugin for selectors not related to native events as custom events require to be fired!

Send me a message If you have problems…

Hi antonio,

I tried this code:

Javascript:


Yii::app()->clientScript->registerScript('sample1', "

$('.sample1').live('click', function(){

		$(this).truncate({max_length:20});

		return false;

		});

");

Html code:


<div class="sample1">this is the div one for trancated text</div>

<div class="sample1">this is the div two for trancated text</div>

But the text is not trancated, (i dont see the more or less link). When i dont use the live fonction the text is trancated.

Do you have an idea why ?

Thanks

Yes, because it needs to ‘clicked’ to be truncated… check the code.

Please, download livequery plugin and once you previously include it after jquery library do:


Yii::app()->clientScript->registerScript('sample1', "

$('.sample1').livequery(function(){

    $(this).truncate({max_length:20});

});

", CClientScript::POS_READY);

I am sure it will work… I am doing this all the time in my CMSs.

You’re right antonio, its works now. Thanks

Congrats man!


Please update the title of the topic as [SOLVED] so it can help other fellow programmers -go to your first post -> edit -> full editor… thanks!