3

サイトから取得したコードを使用してcodeignterでオートコンプリートを実行しようとしましたが、機能していません。誰かが問題を見つけることができますか

私が使用した表示機能は以下のとおりです。

<html>

    <head>
        <title>Autocomplete</title>

        <script src="<?php echo base_url().'js/jquery.autocomplete.js'?>"></script>
        <script src="<?php echo base_url().'js/jquery-1.6.4.js'?>"></script>

        <script type="text/javascript">

          $(function() {
          $( "#username" ).autocomplete({ //the recipient text field with id #username
          source: function( request, response ) {
            $.ajax({
                url: "http://localhost/autocomplete/index.php/autocontroller/search_username",
                dataType: "json",
                data: request,
                success: function(data){
                    if(data.response == 'true') {
                       response(data.message);
                    }
                }
            });
        }
    });
});

        </script>
    </head>

    <body>

        <h1>Autocomplete</h1>

        <input type="text" id="username" value="" />
    </body>

</html>

そして、私が使用したコントローラーを以下に示します

<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of autocontroller
 *
 * @author Sivam
 */
class autocontroller extends CI_Controller {

    public function  __construct() {
        parent::__construct();
         $this->load->helper(array('form', 'url'));
         $this->load->database();
    }

    function index()
    {
        $this->load->view('autoview');
    }

    function search_username()
{
        $username = trim($this->input->get('term', TRUE)); //get term parameter sent via text field. Not sure how secure get() is

        $this->db->select('fname');
        $this->db->from('users');
        $this->db->like('fname', $username);
        $this->db->limit('5');
        $query = $this->db->get();

        if ($query->num_rows() > 0)
        {
            $data['response'] = 'true'; //If username exists set true
            $data['message'] = array();

            foreach ($query->result() as $row)
            {
                $data['message'][] = array(
                    'label' => $row->fname,
                    'value' => $row->fname
                );
            }
        }
        else
        {
            $data['response'] = 'false'; //Set false if user not valid
        }

        echo json_encode($data);
}

}
?>
4

2 に答える 2

3

なぜ使うのかわからない

$data['message'][] = array(
                    'label' => $row->fname,
                    'value' => $row->fname
                );

一次元配列で試すことができます。

$data['message'] = array(
                        'label' => $row->fname,
                        'value' => $row->fname
                    );

これをフォローしてオートコンプリートも使用しました。http://www.jamipietila.fi/codeigniter-and-autocomplete-with-jquery/これを試してみてください...それは私のために働きます..うまくいけば、それはあなたのためにも働くでしょう。

于 2012-05-30T07:48:38.877 に答える
0

これは、codeigniterでオートコンプリートを使用する簡単な方法です。それは私のために働いた。

ビューで:

<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>

<script type="text/javascript">
function lookup(inputString) {
if(inputString.length == 0) {
    $('#suggestions').hide();
} else {
    $.post("<?php echo base_url() ?>your_controller/your_action/"+inputString, function(data){
        if(data.length > 0) {
            $('#suggestions').show();
            $('#autoSuggestionsList').html(data);
        }
    });
}

}

function fill(thisValue) {
$('#id_input').val(thisValue);
  setTimeout("$('#suggestions').hide();", 200);
}
</script>


<?php echo form_input('company_name', '', "id='id_input' autocomplete='off' onKeyUp='lookup(this.value)'") ?>

<div id="suggestions">
  <div class="autoSuggestionsList_l" id="autoSuggestionsList"></div>
</div>

ここで最初の行は、もちろん、jqueryライブラリを呼び出すことです。

次に、2つの関数があります。lookup()はユーザー入力を受け入れてから、必要なデータベース呼び出しを行う関連URLを呼び出します。

次の関数fill()は、結果の出力を含むdivをアンロードします。

最後に、ビューにテキスト入力ボックスを追加し、ユーザーがボックスに入力するとルックアップ関数をフックしました。入力ボックスの下にdivがあり、結果がある場合はそれが表示されます。このdivは、ユーザーの入力によって結果が得られた場合にのみ表示されます。

コントローラには次のものがあります。

function autocomplete($a)
{
  $i = 0;
  $this->load->model('company');
  $companyList = $this->company->get_companies($a);

  if(count($companyList) > 0):
    echo "<ul>";
    foreach($companyList as $comp):
      echo "<li id='".$companyList[$i]['id']."'><a href='#'>".$companyList[$i]['name']."</a></li>";
      $i++;
    endforeach;
    echo "</ul>";
  endif;
} 

上記の関数では、会社名(またはその一部)を受け入れ、文字列を使用して関数get_companies()を呼び出しています。データベースを呼び出し、結果を配列として返すこの関数。

モデルの場合:

public function get_companies($name)
{
    //$this->db->_compile_select(); 
    $this->db->like('company_name', $name, 'after'); 

    //$this->db->where('id', $name);

    $query = $this->db->get('companies');

    //echo $this->db->last_query(); 
    //echo $query->num_rows();
    $companies = array();
    $i = 0;
    if($query->num_rows() > 0)
    {
        foreach ($query->result() as $row)
        {
            $companies[$i]['id'] = $row->id;
            $companies[$i]['name'] = $row->company_name;
            $i++;
        }


    }

    //print_r($companies);
    return $companies;


}

上記の関数は、データベース呼び出しを行い、2D配列を返します。1つの要素はIDで、もう1つの要素は名前です。

于 2013-10-10T18:04:15.087 に答える