简体   繁体   中英

Filtering CGridView with custom textfield

I need to filter CGridView with custom text-field. I have custom dataprovider(CSqlDataProvider) which I return and fill the table with. Table is fine, but what seems pain to me is filtering it. I spent few hours already trying to figure this out and the support is poor. I saw that there is some jQuery method used to update CGridView, and that it takes 'data' parameter, whose value I don't know how to format. Here is what I made in my /create file where I need this thing.

PHP:

<?php
Yii::app()->clientScript->registerScript('search', "
$('.search-form form').submit(function(){
    $.fn.yiiGridView.update('studentiSaSmjeraData', {
        data: $(this).serialize(),  -----------> this should be changed to get data from my text input using GET I guess, but I dont know the format
    });
    return false;
    });
");
?>

HTML and PHP considering search form:

<div class="search-form">
        <p>
            Pretraga po broju indeksa: 
        </p>
        <?php $form=$this->beginWidget('CActiveForm', array(
            'action'=>Yii::app()->createUrl($this->route),
            'method'=>'get',
        )); ?>
        <div class="row">
            Broj indeksa: <input type="text" class="br_ind" name="br_ind" id="br_ind" value=""></input>
        </div>
        <div class="row buttons">
            <?php echo CHtml::submitButton('Search'); ?>
        </div>
        <?php $this->endWidget(); ?>
</div>

The column in dataProvider I want to compare this text with is called 'br_ind'.

EDIT: Seems like the input field name and id have to do something with this too(they should be in some specific format). My problem is - the data is not from any specific model. It is obtained by a table product and some additional constraints on two tables, so I don't have particular model to use filters or anything which has(afaik) support in Yii. But what I want seems simple, yet turned into nightmare for me. The documentation about parametres of function is so poor. Hope someone can help. Anyone bumped into this and couldn't solve it?

$.fn.yiiGridView.update() is using jQuery's ajax function behind the scenes (if ajax updating is enabled for the grid). You can check out the jQuery docs for details on the data parameter and other options that can be passed to .ajax() .

Using $(this).serialize() as you are currently doing should make jQuery add br_ind=whatever_the_value_is as a querystring parameter to the GET request to the server (again, assuming you are using GET, don't see a reason to use POST or another verb but not enough info provided in question). Use your browser's developer tools to verify the URL being requested and the response content.

Then it is a matter of whether the server is processing the request properly. You haven't provided any details so I can't help you much there.

this is a sample

    $.fn.yiiGridView.update('activity-grid', {
        data: 'Activity[Type]='+tp+'&Activity[idAgent]='+agent+'&Activity[StartTime]='+start+'&Activity[EndTime]='+end
    });

in this example, in server side, you just have to get $_GET[Activity]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM