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 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝