2012-04-03 83 views
3

在rails中,jQuery令牌輸入不允許自定義條目。在rails中,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會到達客戶端並設置爲添加令牌的值。

獲取plugin允許從github免費標記設施。

<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;  
} 
?> 
相關問題