自动填充cakephp

时间:2016-03-21 05:26:54

标签: jquery json ajax autofill cakephp-2.6

我正在尝试在我的应用程序中实现自动填充功能,但它无法正常工作。我尝试了一切。我还包括必要的js和css文件请查看我的代码并提出任何更改建议。 控制器:

    public function getfill($id=NULL)
{
    $this->autoRender = false;
    if($this->request->data['type'] == 'panels_table')
    {
        $name = trim( $this->data['name_startsWith'] );
        $result = $this->Panel->find('all', array('conditions'=>array('Panel.name LIKE'=>'%'.$name.'%')));
        $data = array();
        foreach ($result as $row)
        {
            $name = $row['Panel']['name'].'|'.$row['Panel']['designation'].'|'.$row['Panel']['specialization'].'|'.$row['Panel']['university'].'|'.$row['Panel']['college'].'|'.$row['Panel']['address'].'|'.$row['Panel']['phone'].'|'.'|'.$row['Panel']['email'];
            array_push($data, $name);
        }
        echo json_encode($data);
        exit;
    }

}

查看:

     $('.pchange').each(function()
 {       
    var g=$(this).attr('id');
    //alert(g);
    var lastChar = g.replace ( /[^\d.]/g, '' );
    $('#name'+lastChar).autocomplete({
        source: function( request, response )
        {
            $.ajax({
            url : '<?php echo Router::url(array('controller'=>'Panels','action'=>'getfill'));?>',
            dataType: "html",
            method: 'post',
            data: {
            name_startsWith: request.term,
            type: 'panels_table',
            row_num : 1
            },
            success: function( data ) { 
                response( $.map( data, function( item ) {
                var code = item.split("|");
                return {
                    label: code[0],
                    value: code[0],
                    data : item
                }
            }));
        }
    });
    },
    autoFocus: true,            
    minLength: 1,
    select: function( event, ui ) {
        var namess = ui.item.data.split("|");                       
        $('#desi'+lastChar).val(namess[1]);
        $('#speci'+lastChar).val(namess[2]);
        $('#univr'+lastChar).val(namess[3]);
        $('#col'+lastChar).val(namess[4]);
        $('#addr'+lastChar).val(namess[5]);
        $('#phone'+lastChar).val(namess[6]);
        $('#email'+lastChar).val(namess[7]);
    }               
});});

0 个答案:

没有答案