dongtiaobeng7901 2013-10-25 15:43
浏览 7
已采纳

带有自动完成功能的Cakephp表单输入

I am using CakePhp 2.2.1 and I am having some problems to implement what I just asked in the title, I found several tutorials but most of them are for cakephp 1.3 and the others are not what I want to do. I have a "events" table which contains a "player_id" thus a Player has many Events and an Event belongs to a Player.

In my Event add form I proceed as the cookbook says and I get a dropdown list of players to choose from, however what I want is to just write the names of the players and select the one I want from the autocomplete results. Also these players must be from the team that I select before that. Any ideas?

Thanks in advance.

  • 写回答

3条回答 默认 最新

  • doupeng5320 2013-11-13 20:14
    关注

    Special thanks to Andrew for pointing out this api.jqueryui.com/autocomplete. However there is not a real guide to use this one. So i found this post, which explains what Abhishek's second link says but I could understand it better. So here is my solution if anyone is interested:

    1 - Download from the autocomplete page the .js you need. Save it in app/webroot/js

    2 - Either in your app/View/Layouts/default.ctp or in the view you want to use the autocomplete add:

    echo $this->Html->script('jquery-1.9.1.js'); 
    echo $this->Html->script('jquery-ui-1.10.3.custom.js');
    echo $this->fetch('script');
    

    3 - In your view add (mine was add_goal.ctp):

    <script>
    $(document).ready(function(){
    var myselect = document.getElementById("EventTeam"); //I needed to know which team I was looking players from. 
    var team = myselect.options[myselect.selectedIndex].value; //"EventTeam" was a dropdown list so I had to get the selected value this way.
    $("#EventPlayer").autocomplete({
        source: "/events/autoComplete/" + team,
        minLength: 2, //This is the min ammount of chars before autocomplete kicks in
        autoFocus: true
    });
    $("input:submit").button();
    $("#EventPlayerId").autocomplete({
        select: function(event, ui) {
            selected_id = ui.item.id;
            $('#EventAddGoalForm').append('<input id="EventPlayerId" type="hidden" name="data[Event][player_id]" value="' + selected_id + '" />');
        }
    });
    $("#EventPlayerId").autocomplete({
        open: function(event, ui) {
            $('#EventPlayerId').remove();
        }
    });
    });
    </script>
    

    4 - In your Controller (mina was EventController.php):

    public function autoComplete($team = null){
        Configure::write('debug', 0);
        $this->autoRender=false;
        $this->layout = 'ajax';
        $query = $_GET['term'];
        $players = $this->Event->Player->find('all', array(
            'conditions' => array('Player.team_id' => $team, 'Player.name LIKE' => '%' . $query . '%'),
            'fields' => array('name', 'id')));
        $i=0;
        foreach($players as $player){
            $response[$i]['id']=$player['Player']['id'];
            $response[$i]['label']=$player['Player']['name'];
            $response[$i]['value']=$player['Player']['name'];
            $i++;
        }
        echo json_encode($response);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。