0

如何使用这个插件(CakeDC 的一个分支)在日期之间进行搜索?

https://github.com/dereuromark/search

我读过文档,但我做不到。即使我只搜索了一个日期,我也没有成功。

public $filterArgs = array(
    'my_date' => array('type' => 'like')
);

在视图中:

echo $this->Form->input('my_date', array('div' => false, 'label' => 'Date', 'dateFormat'     => 'DMY', 'minYear' => date('Y') - 80));
4

1 回答 1

3

有两种方法可以使用 CakeDC 进行日期搜索,

第一:只寻找特定的年份

在模型中

'creationdate'  => array('type' => 'like'),

在控制器动作中:

    $this->Prg->commonProcess();

if(array_key_exists('creationdate',$this->passedArgs))
$this->passedArgs['creationdate'] = $this->passedArgs['creationdate']['year'];

并认为:

    echo $this->Form->input('creationdate', array(
    'label'             => __('Creation date'),
    'dateFormat'    => 'Y',
    'minYear'           => date('Y') - 100,
    'maxYear'           => date('Y'),
    'empty'             => true
    )
);

第二种方式:查找日期范围

在模型中:

        'creationDateBetween'       => array(
            'type'      => 'expression',
            'method'    => 'CreationDateRangeCondition',
            'field'     => 'MODEL.creationdate BETWEEN ? AND ?',
        ),

public function CreationDateRangeCondition($data = array()){
    if(strpos($data['creationDateBetween'], ' - ') !== false){
        $tmp = explode(' - ', $data['creationDateBetween']);
        $tmp[0] = $tmp[0]."-01-01";
        $tmp[1] = $tmp[1]."-12-31";
        return $tmp;
    }else{
        return array($data['creationDateBetween']."-01-01", $data['creationDateBetween']."-12-31");
    }
}

在视图中:请注意,我使用的是年份范围的滑块

    echo $this->Form->input('creationDateBetween', 
    array(
        'label'     => __('Creation date between X and Y'),
        'div'       => false,
        'style' => 'border: 0; color: #49AFCD; font-weight: bold;'
    )
);

?><div id="creationDateBetweenSlider" style="padding:0;"></div><?php





<script>
$(function() {


    var     creationDateBetweenSlider = $( "#creationDateBetweenSlider" ),
        institutionCreationDateBetween  = $( "#MODELCreationDateBetween" ), 

lock = 0;

creationDateBetweenSlider.slider({
                range: true,
                min: 1900,
                max: 2050,
                values: [ 2000, 2013 ],
                slide: function( event, ui ) {
                    MODELCreationDateBetween.val(  ui.values[ 0 ] + " - " + ui.values[ 1 ] );
                }
            });
        if(lock != 0) MODELCreationDateBetween.val( creationDateBetweenSlider.slider( "values", 0 ) + " - " + creationDateBetweenSlider.slider( "values", 1 ) );

lock = 1;

    });
</script>

希望能帮助到你 !

于 2013-02-26T21:31:28.813 回答