我在 CGridView 中遇到过滤器输入框的问题。因此,在过滤器输入框中输入搜索字符串并按 Enter 键后,过滤器会显示正确的结果,但在显示结果后输入框会被清除。这使得用户查看他们正在搜索的内容非常不方便,因为过滤器输入框是空的,而网格显示了正确的搜索结果。

这是代码。

条目视图名称::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>

这是名为 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']

    ));
}

这是视图 _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',
        )
    )
));

?>

现在就像我说的那样,过滤器正在显示结果,但是输入框在显示结果后被清除。我尝试使用 jQuery 的 Change() 处理程序在输入框中重新插入 val,但它不起作用。

请提供有关如何在过滤器框中保留搜索字符串值的任何建议。哦,顺便说一句,网站上的其他网格运行完美,所以这不是丢失文件的问题。

预先感谢Maxx

有帮助吗?

解决方案

好吧,我在 renderpartial code() 中犯了一个错误,导致了这个错误。简单地改变'dataProvider' => new News()'dataProvider' => $dataProvider 解决了这个问题。希望它可以帮助面临同样问题的人。

工作控制器代码

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']

    ));
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top