2012-04-03 2 views
3

레일에서 jQuery 토큰 입력이 사용자 정의 항목을 허용하지 않습니다.레일에서 jQuery 토큰 입력이 사용자 정의 항목을 허용하지 않습니다.

토큰 입력의 1.6.0 버전을 다운로드했지만 사용자 지정 항목을 입력 할 수 없습니다. 텍스트 상자에 텍스트를 입력하자마자 & 커서가 나가면 텍스트가 사라집니다. 자동 완성 목록에서 선택해야합니다.

예 : - 아래 종류의 스크립트 기능을 사용하는 경우 해결책은 무엇입니까?

<pre> 
<script type="text/javascript"> 
tokenInput("SOME_ID", "/token_input/name"); 

function tokenInput(text_box_id, url){ 
    jQuery("#" + text_box_id).tokenInput(url, { 
     allowCustomEntry: true, 
     preventDuplicates: true, 
     theme: "facebook", 
    }); 
    } 
</script> 
</pre> 

토큰 입력은 사용자 지정 항목을 허용해야합니다.

답변

0

Tokeninput 마스터 브랜치에서 곧 수정 될 예정이지만 그동안 this branch을 자신의 포크에 병합하면 문제를 해결할 수 있습니다.

1

나는 매력으로 나를 위해 일하고있다.

데이터베이스를 저장할 사용자 지정 항목이 필요했고 자동 증가 ID는이 토큰의 값입니다.

다음과 같이 수정했습니다. 거기에 속임수가 추가 토큰에 대한 요청에 서버에 가서 데이터베이스에 새 토큰을 추가하고 그 새로운 삽입 ID는 클라이언트 측에 와서 추가 된 토큰의 값으로 설정됩니다.

github에서 무료 태그 지정 기능을 허용하여 plugin을 받으십시오.

<input type="text" name="w_i_tk" id="w_i_tk"> 

<script> 
$(document).ready(function() {. 

    $("#w_i_tk").tokenInput("token.php", { 
     theme: "facebook", 
     hintText: "Type tag by which other can search, e.g. PHP, MySql etc.", 
     preventDuplicates: true, 
     tokenLimit: 5, 
     minChars: 2, 
     onAdd: function (item) { 
      if(item.id=="0") { 
       $.ajax({ 
        type:"GET", 
        url:"token.php", 
        data:{action:"newtoken",name:item.name}, 
        success: function(resp) { 
         $("#w_i_tk").tokenInput("remove", {name: item.name}); 
         $("#w_i_tk").tokenInput("add", {id: resp, name: item.name});  
        } 
       }); 

      } 

     }, 
     animateDropdown: false, 
     allowFreeTagging: true 
    }); 
}); 
</script> 

token.php

<?php 
if(isset($_GET["q"])) { 
    $q = trim($_GET["q"]); 
    $isSearchItemExists = false; 
    $sql = sprintf("SELECT token_id, token from tokens WHERE token LIKE '%%%s%%' ORDER BY popularity DESC LIMIT 10", mysql_real_escape_string($q)); 
    $rec = mysql_query($sql); 

    $arr = array(); 
    while($row = mysql_fetch_array($rec)) { 
     $obj = new stdClass(); 
     $obj->id = $row["token_id"]; 
     $obj->name = $row["token"]; 
     if($obj->name==$q) { 
      $isSearchItemExists = true; 
     } 
     $arr[] = $obj; 
    } 
    if(!$isSearchItemExists) $arr = array_merge(getNewToken($q),$arr); 

    $json_response = json_encode($arr); 


    echo $json_response; 

} else if(isset($_GET["action"]) && $_GET["action"]=="newtoken") { 

    $token = strtolower($_REQUEST["name"]); 
    $sql = "SELECT * FROM tokens WHERE token='$token'"; 
    $rec = mysql_query($sql); 
    $numRows = mysql_num_rows($rec); 
    if($numRows>0) { 
     $row = mysql_fetch_array($rec); 
     $id = $row["token_id"]; 
    } else { 
     $sql = "INSERT INTO tokens SET token='$token'"; 
     $rec = mysql_query($sql); 
     $id = mysql_insert_id();  
    } 
    echo $id; 
    exit; 
} 

function getNewToken($q) { 

    $sql = "SELECT max(token_id) as token_id FROM tokens"; 
    $rec = mysql_query($sql); 
    $row = mysql_fetch_array($rec); 
    $maxToken = $row["token_id"]; 
    $newToken = $maxToken + 1; 

    $newItem = array(); 
    $new = new stdClass(); 
    $new->id = "0"; 
    $new->name = $q; 
    $newItem[] = $new; 

    return $newItem;  
} 
?> 
관련 문제