dongtuo4132 2016-05-12 12:29
浏览 42
已采纳

在yii中更新cgridview后,Jquery UI可排序无法正常工作

I am working on project which runs on AJAX based requests(No Reload). Jquery sortable is working fine until we update cgridview, after that it is not working.

Also if i am sorting multiple times, then it stops working. This page have lots of image list.

I have a code like below :|

index.php

//Get image details
foreach($arrContent as $key => $value) {
    ob_start();
        $this->widget('ImageWidget',array('imageModel'=>new Images(),'entityType'=>$key,'entityTypeId'=>$specModel->spec_id,'type'=>'View'));
        $arrContent[$key] = ob_get_contents();
    ob_end_clean();
    ob_start();
        $this->widget('ImageWidget',array('imageModel'=>new Images(),'entityType'=>$key,'entityTypeId'=>$specModel->spec_id,'type'=>'Form'));
        $arrContent[$key] .= ob_get_contents();
    ob_end_clean();
}

ob_start();
    $this->widget('BrochureWidget',array('brochureModel'=>new Brochure(),'entityType'=>"SPEC", 'entityTypeId'=>$specModel->spec_id,'type'=>'Form'));
    $brochureContent .= ob_get_contents();
ob_end_clean();

$this->widget('zii.widgets.jui.CJuiTabs',array(
    'tabs'=>array(
        "Brochure({$countBrochure})"=>array(
            'content'=>$brochureContent,
        ),
        "Elevation Images({$countElevationImages})"=>array(
            'content'=>$arrContent['SPEC_ELEVATION']
        ),
    ),
)
.............................
............................

ImageViewWidget

<?php   
..........................  
........................   

             Yii::app()->clientScript-> registerScript("draggable-$this->entityType-$value", false);

$draggable = "sortGrid('image-grid-$this->entityType-$value', '{$sortUrl}')";

Yii::app()->clientScript->registerScript("draggable-$this->entityType-$value", $draggable);

$this->widget('zii.widgets.grid.CGridView', array(
            'id'=>"image-grid-$this->entityType-$value",
            'ajaxUrl'=>Yii::app()->createAbsoluteUrl('bdxFeed/images',array('entityType'=>$this->entityType,'entityTypeId'=>$this->entityTypeId)),
            //'rowCssClassExpression'=>'"items[]_{$data->image_id }"',
            'rowHtmlOptionsExpression'=>'array("rel"=>"items[]_{$data->image_id}")',
            'dataProvider'=>$dataProvider,
            'afterAjaxUpdate' => 'sortGrid',
            'htmlOptions'=> array(
                'style'=>'padding:0px',
            ),
            'columns'=>array(

            ........................
            ........................

ImageFormwidget

<?php
........................
........................
$submitForm = <<< EOJ
$('#image-form-{$this->entityType}').on('submit',function() {
    var URL = '{$url}';
        $.ajax({
            type:'post',
            url : URL,
            data:new FormData(this),
            dataType:'json',
            contentType:false,
            processData:false,
            cache:false,
            success:function(response){
                if(response.status == 1) {
                    if(imageType != "" && response.new)
                        $('#images #gridViews').html(response.html);

                    $('#imageFail').hide();
                    $('#imageSucc').html(response.message);
                    $('#imageSucc').show();
                    updateCount();
                    $('#communityLoader').hide();

                    gridId = "image-grid-$this->entityType";

                    if(imageType != '') {
                        gridId += "-" + imageType;
                    }
                    //Update gridviwe for newly inserted record
                    if(!response.new || imageType == '')
                    {
                        $.fn.yiiGridView.update(gridId);
                    }
                    document.getElementById('image-form-{$this->entityType}').reset();

                    //add sorting functionality to updated grid view
                    if(imageType != "" ) {
                        $('input[name="Images[image_type]"]').each(function() {
                            gridId = "image-grid-$this->entityType-" + $(this).val();
                            if($('#' + gridId).length) {
                                setTimeout(function() { sortGrid(gridId, '{$sortUrl}'); }, 300);
                            }
                        });
                   }
                   else {
                         setTimeout(function() { sortGrid(gridId, '{$sortUrl}'); }, 300);
                   }

                }
                else {
                    $('#imageSucc').hide();
                    $('#imageFail').html(response.message);
                    $('#imageFail').show();
                    $('#communityLoader').hide();
                }
            },
            error:function() {
                $('#communityLoader').hide();
            }

        })
    });

JS

function sortGrid(id, URL) {    
    $('#' + id + ' table.items tbody').sortable({
        forcePlaceholderSize: true,
        forceHelperSize: true,
        items: 'tr',
        update : function() {
                $('#communityLoader').css({
                    height:$('#communityLoader').parent().height(),
                    width:$('#communityLoader').parent().width()
                });
                $('#communityLoader').show();
                serial = $('#' + id + ' table.items tbody').sortable('serialize', {key: 'items[]', attribute: 'rel'});

                $.ajax({
                    type : 'POST',
                    url : URL,
                    data : serial,
                    success : function(data) {
                       $('#communityLoader').hide();
                       $.fn.yiiGridView.update(id);
                       setTimeout(function() {sortGrid(id, URL)}, 2000);
                    },
                    error : function(request, status, error) {
                        $('#communityLoader').hide();
                    }
                });
            },
        helper: fixHelper
    }).disableSelection();
}
  • 写回答

1条回答 默认 最新

  • dso89762 2016-05-17 08:32
    关注

    Above issue is resolved by doing below changes:

    ImageViewWidget:
    Note : i have removed the afterAjaxUpdate.

     <?php   
     ..........................  
    ........................   
    $this->widget('zii.widgets.grid.CGridView', array(
            'id'=>"image-grid-$this->entityType-$value",
            'ajaxUrl'=>Yii::app()->createAbsoluteUrl('bdxFeed/images',array('entityType'=>$this->entityType,'entityTypeId'=>$this->entityTypeId)),
            //'rowCssClassExpression'=>'"items[]_{$data->image_id }"',
            'rowHtmlOptionsExpression'=>'array("rel"=>"items[]_{$data->image_id}")',
            'dataProvider'=>$dataProvider,
             //'afterAjaxUpdate' => 'sortGrid',
            'htmlOptions'=> array(
                'style'=>'padding:0px',
            ),
            'columns'=>array(
    
            ........................
            ........................
    

    and in js:

    I have changed below :

     $.fn.yiiGridView.update(id);
     setTimeout(function() {sortGrid(id, URL)}, 2000);
    

    with

    $.fn.yiiGridView.update(gridId,{
                                complete: function(jqXHR, status) {
                                    if (status=='success'){
                                        sortGrid(gridId, '{$sortUrl}');
                                    }
                                }
                            });
    

    That's it, it is resolved bu doing above changes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败