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 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算