duancaishun4812 2013-11-16 02:59
浏览 125
已采纳

Yii Cgridview过滤器输入框搜索后不显示搜索字符串

i'm facing an issue with the filter input box in CGridView. So the filter is showing the correct result after entering the search string in the filter input box and hitting enter, but the input box is getting cleared after the result is being shown. This makes it quite inconvenient for the user to see what they was searching for because the filter input box is empty while the grid shows the correct search results.

Here are the code.

entry view name ::newsReleases.php

    <?php
$this->widget('bootstrap.widgets.TbGridView', array(
    'id' => 'product-news-grid-' . $id,
    'itemsCssClass' => 'table table-striped',
    'htmlOptions' => array(
        'class' => 'news-datagrid',
    ),
    'dataProvider' => $dataProvider->searchProductNews($symbol), 
    'filter' => $dataProvider,
    'enableHistory' => false,
    'ajaxUpdate' => 'product-news-grid-' . $id,
    'ajaxUrl' => Yii::app()->createUrl('/realTime/AjaxUpdateProductNews'),
    'pager' => array(
        'header' => '',
        'cssFile' => false,
        'maxButtonCount' => 5,
        'selectedPageCssClass' => 'active',
        'hiddenPageCssClass' => 'disabled',
        'firstPageCssClass' => 'previous',
        'lastPageCssClass' => 'next',
        'firstPageLabel' => '<<',
        'lastPageLabel' => '>>',
        'prevPageLabel' => '<',
        'nextPageLabel' => '>',
    ),
    'summaryCssClass' => 'label label-warning',
    'columns' => array(
        array(
            'name' => 'headlines',
            'header' => 'Headlines',
            'value' => function($data) {
                return '<div class="product-news"> <a target="_blank" href="' . $data->link . '" > ' . $data->headlines . '</a></div>';
            },
            'type' => 'raw',
        ),
        array(
            'name' => 'publish_date',
            'header' => 'Date',
            'value' => function($data) {
                return '<span class="news-pub-date">' . $data->publish_date . '</span>';
            },
            'type' => 'raw',
        )
    )
));
?>


<!-- Now this script had to be included again in order to make the ajax sorting and pagination work, or else none of the ajax functionality is working. Remember when getting stuck with ajax update in grid views always use this script -->




<script type="text/javascript" src="/ProductAnalysis/assets/dd5f9a70/gridview/jquery.yiigridview.js"></script>
<script type="text/javascript">

    /*<![CDATA[*/
    jQuery(function($) {

        jQuery('[data-toggle=popover]').popover();

        jQuery('body').tooltip({
            "selector": "[data-toggle=tooltip]"
        });



        jQuery('#product-news-grid-' + $('#symbol-id').text()).yiiGridView({
            'ajaxUpdate': ['product-news-grid-' + $('#symbol-id').text()],
            'ajaxVar': 'ajax',
            'pagerClass': 'pagination',
            'loadingClass': 'grid-view-loading',
            'filterClass': 'filters',
            'tableClass': 'table table-striped',
            'selectableRows': 1,
            'enableHistory': false,
            'updateSelector': '{page}, {sort}',
            'filterSelector': '{filter}',
            'url': '/ProductAnalysis/index.php/realTime/AjaxUpdateProductNews',
            'pageVar': 'News_page',
            'afterAjaxUpdate': function() {
                jQuery('.popover').remove();
                jQuery('[data-toggle=popover]').popover();
                jQuery('.tooltip').remove();
                jQuery('[data-toggle=tooltip]').tooltip();
                $('#News_headlines').change(function() {
                    var inputVal = $(this).val();

                    $('#News_headlines').val(inputVal);

                });
            }
        });
    });
    /*]]>*/

</script>

Here is the controller action named AjaxUpdateProductNews

public function actionAjaxUpdateProductNews() {



    $dataProvider = new News();
    $dataProvider->unsetAttributes();

    if (isset($_GET['News'])) {

        $dataProvider->attributes = $_GET['News'];
    }





    $id = explode("-", $_GET["ajax"]);

    $realTime = RealTime::model()->findByPk($id[count($id) - 1]);

    $this->renderPartial('_newsView', array(

        'dataProvider' => new News(),
        'symbol' => $realTime->symbol,
        'id' => $realTime->id,
        'headlines' => $_GET['News']['headlines'],
        'publish_date' => $_GET['News']['publish_date']

    ));
}

and here is the view _newsView

  <?php




$this->widget('bootstrap.widgets.TbGridView', array(
    'id' => 'product-news-grid-'. $id,
    'itemsCssClass' => 'table table-striped',
    'htmlOptions' => array(
        'class' => 'news-datagrid',
    ),
    'dataProvider' => $dataProvider->searchProductNewsSymbol($symbol, $headlines, $publish_date), 
    'filter' => $dataProvider,
    'enableHistory' => false,
    'ajaxUpdate' => 'product-news-grid-'. $id,
    'ajaxUrl' => Yii::app()->createUrl('/realTime/AjaxUpdateProductNews'),
    'pager' => array(
        'header' => '',
        'cssFile' => false,
        'maxButtonCount' => 5,
        'selectedPageCssClass' => 'active',
        'hiddenPageCssClass' => 'disabled',
        'firstPageCssClass' => 'previous',
        'lastPageCssClass' => 'next',
        'firstPageLabel' => '<<',
        'lastPageLabel' => '>>',
        'prevPageLabel' => '<',
        'nextPageLabel' => '>',
    ),
    'summaryCssClass' => 'label label-warning',
    'columns' => array(
        array(
            'name' => 'headlines',
            'header' => 'Headlines',
            'value' => function($data) {
                return '<div class="product-news"> <a target="_blank" href="'. $data->link .'" > '. $data->headlines .'</a></div>';
            },
            'type' => 'raw',
        ),
        array(
            'name' => 'publish_date',
            'header' => 'Date',
            'value' => function($data) {
                return '<span class="news-pub-date">'. $data->publish_date .'</span>';
            },
            'type' => 'raw',
        )
    )
));

?>

Now like i said the filter is showing the result, but the input box is getting cleared after it shows the result. I tried to reinsert the val in the input box using jQuery's change() handler, but it doesn't works.

Please provide any sort of advice on how to retain the search string value in the filter box. Oh, btw other grids on the sites are working flawlessly, so its not a problem with missing files.

Thanks in advance, Maxx

  • 写回答

1条回答 默认 最新

  • douliang7068 2013-11-16 13:28
    关注

    Ok i've made a mistake in the renderpartial code() which caused this error. Simply changing 'dataProvider' => new News() to 'dataProvider' => $dataProvider fixed the issue. Hope it helps someone who is facing the same issues.

    The working controller code

    public function actionAjaxUpdateProductNews() {
    
    
    
        $dataProvider = new News();
        $dataProvider->unsetAttributes();
    
        if (isset($_GET['News'])) {
    
            $dataProvider->attributes = $_GET['News'];
        }
    
    
    
    
    
        $id = explode("-", $_GET["ajax"]);
    
        $realTime = RealTime::model()->findByPk($id[count($id) - 1]);
    
        $this->renderPartial('_newsView', array(
    
            'dataProvider' => $dataProvider,
            'symbol' => $realTime->symbol,
            'id' => $realTime->id,
            'headlines' => $_GET['News']['headlines'],
            'publish_date' => $_GET['News']['publish_date']
    
        ));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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