在cakephp中创建动态表

时间:2016-03-26 08:32:50

标签: jquery json ajax cakephp-2.6

在我的模块中,我需要一个类似于自动完成的功能。 我需要的是每当我输入一个名字时创建一个表。比如说,如果我键入John,那么表格应该显示数据库中的所有'John'以及其他属性,如电子邮件ID,专业化,地址等。然后,当我选择特定的'john'时(通过单击名称/检查)框/任何其他方式),他的信息应自动填写我的表格。 我能够使用json(我已经在警告框中打印)提取所有'John'的详细信息,但我面临的两个问题是: 1.)我无法创建对应于'John'的表 2.)当用户点击所需的行时,我不知道如何将数据传递给我的表单。

当我提取数据时,只显示表格中最后一个条目的数据(即最后一个''john'');即只有最后''约翰'才能得救。虽然for循环显示所有''john'。

我正在粘贴我的控制器并查看代码供您参考。

请提出建议。

//Controller:
public function getmasterdata($id=NULL)
{
    $this->layout = 'ajax';
    $this->autoRender = false;

    $q=$this->request->data['nns'];
    $details=array();
    $this->loadModel('Approved_panel');
    $conditions = 'Approved_panel.name LIKE "%'.$q.'%"'; 

    $countt=$this->Approved_panel->find('count',array('conditions'=>$conditions));
    $masterdata=$this->Approved_panel->find('all',array('conditions'=>$conditions));
    //print_r ($masterdata);

    foreach($masterdata as $m)
    {
        $details=$m['Approved_panel'];
        print_r($details);

    }
    //print_r($details);
    $data='';
    foreach($details as $key=>$value)
    {
        $data.=$key.'//'.$value.'&&&';
    }
    print_r($data);
    //echo json_encode($details);
    exit;
}

`

//View:
 $("#name"+lastChar).on('keyup',function ()
 {
        $('#master_details').show();
        var namess=$('#name'+lastChar).val();
        //alert(namess);
        $.ajax({  
        cache: false,
        dataType: "html",
        type: "POST",  
        evalScripts: true,
        url: '<?php echo         Router::url(array('controller'=>'Panels','action'=>'getmasterdata'));?>',
        data: ({nns:namess}),  
        success: function(result){ 
                alert(result);
                $('#detailss').val(result);
           }
        }); 
    });

`

//This is the div in which data should be displayed. These are the headers. I don’t know how to get data into this.

 <div id="master_details">
 <h2 align="center">Previous Panel Member's Details</h2>
 <table>
 <thead>
<tr>
    <th> Name</th>
    <th> Designation</th>
    <th> Specialization</th>
    <th> University</th>
    <th> College</th>
    <th> Address</th>
    <th> Phone</th>
    <th> Email</th>
    <th> Papercode</th>
    <th> Action</th>
   </tr>
 </thead>
 </table>
</div>

2 个答案:

答案 0 :(得分:0)

请检查$ q是否返回任何内容 echo $ q;

或做pr($ this-&gt; request-&gt; data)//获取数组数据的打印

答案 1 :(得分:0)

也许是你的条件陈述的写法。 echo $ sql说的是什么。您还可以在phpmyadmin中运行该语句。试试这个:

$masterdata=$this->Approved_panel->find('all', ['conditions'=>[Approved_panel.name LIKE' =>  "%'.$q.'%"]];
相关问题