On rails jQuery input current does not allow customization of record

On rails, the jQuery input token does not allow customization of the record.

I downloaded 1.6.0 version of token input, but I cannot enter custom record. As soon as I enter text in the textbox and drag out the cursor, the text disappears. Only I need to select from the auto complete list.

For example: - If I use below kind of script function what would be the solution?

<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>

      

Please give me a solution for this, the input token should allow a custom write.

+3


source to share


2 answers


This will most likely be fixed in the Tokeninput master branch, but at the same time merging that branch into your own fork should fix it for you.



0


source


It works like a charm for me.

I need a custom record that will store the database and the auto-increment id will be the value of this token.

I installed this as the following. There are tricks, when adding a token, the request goes to the server, adds a new token to the database, and its new insert identifier comes to the client side and is set as the value of the added token.

Get the plugin using the free tagging tool from 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;    
}
?>

      

+1


source







All Articles