2013-08-07 3 views
0

jQuery 플러그인 Tokeninput (master branch) (https://github.com/loopj/jquery-tokeninput)에는 태그를 추가하는 새로운 기능이 있습니다. 불행히도 지금까지이 기능은 Twitter에서 가장 잘 설명되어 있습니다 : https://twitter.com/loopj/status/332249287062851585.jQuery Tokeninput 및 onFreeTaggingAdd 콜백

onFreeTaggingAdd를 사용하는 방법을 알아 내려고했지만 불행히도 저는 jQuery와 javascript 초보자입니다.

요약하면 내 API에서 출력을 가져 와서 tokenbox에서 사용하는 콜백을 원합니다. 이렇게하면 태그 (소문자 등)를 수정하고 ID를 추가 할 수 있습니다. api에서 제안한 정책이라면 다른 ID/태그로 대체 할 수도 있습니다.

아래 코드는 지금까지 참조하십시오. item = data를 설정하고 그 값을 반환하는 몇 가지 옵션을 시도했지만 성공하지 못했습니다. 어떤 도움을 주셔서 감사합니다!

onFreeTaggingAdd: function (item) { 

$.post("../php/add_tagg_02.php", {tag: item, userid: "userid-dummy"}) 
.done(function(data, status, xhr) { 
alert ("Your suggested new tag " + data.name + " is entered in the database and will be considered for future use."); 
console.log(data.name); //returns the "new" name from the api 
console.log(data.id); //returns the id provided by the api 
}) 
    return item; //returns the "old" name from the user input 
}, 

답변

0

내가 믿는 기본으로 그 name 것을 변경하는 유일한 방법과 동일한에 freetag의 ID 라이브러리 내의 add_freetagging_tokens() 메소드를 편집하는 것입니다. 가 호출되는 시점에서

는, 토큰이 그렇게 이름을 변경, 문자열에 지나지 않는다, 당신은 단순히이 작업을 수행 할 수 있습니다 :

onFreeTaggingAdd: function (item) { 

$.post("../php/add_tagg_02.php", {tag: item, userid: "userid-dummy"}) 
.done(function(data, status, xhr) { 
alert ("Your suggested new tag " + data.name + " is entered in the database and will be considered for future use."); 
console.log(data.name); //returns the "new" name from the api 
console.log(data.id); //returns the id provided by the api 
}) 
    return data.name; //Sets the tokens Name/ID to be the new name from the api. 
}, 

당신은 또한 ID를 사용자 정의하려면 한 경우 문자열 대신 객체를 반환하는 메서드를 변경하는 것이 상대적으로 간단해야합니다.

또한 데이터 소스를 업데이트하지 않으면이 토큰을 검색 할 수 없습니다.

1

추가하고 예에서와 같이, 토큰 프로그래밍 방식으로 제거 할 수 있습니다 : 라이브러리가 설정

$(document).ready(function() { 
    $("#demo-input-plugin-methods").tokenInput("http://shell.loopj.com/tokeninput/tvshows.php"); 
       // Add a token programatically 
       $("#plugin-methods-add").click(function() { 
        $("#demo-input-plugin-methods").tokenInput("add", {id: 999, name: "James was here"}); 
        return false; 
       }); 
// Remove a token programatically 
      $("#plugin-methods-remove").click(function() { 
       $("#demo-input-plugin-methods").tokenInput("remove", {name: "James was here"}); 
       return false; 
      }); 
    }); 
+0

이것은 실제로 작동 중입니다! 코드는 아주 깨끗합니다. 그러나 다음과 같이 제거하고 추가 할 필요가 없어야합니다. '\t onFreeTaggingAdd : function (item) { \t \t $ .post ("../ php/add_tagg_02.php", {tag : item, userid :. "사용자 ID-더미"}) \t \t되는 .done (기능 (데이터, 상태, XHR) { \t \t 경고 ("메시지"); \t \t $ ("# 태그") tokenInput는 ("제거" {이름 : "쓰레기"}); \t \t $ ("# 태그") tokenInput는 ("추가", {ID : data.id, 이름 : data.name}); \t \t}) \t \t. 반환 "쓰레기"; \t} ' – moltubakk

+0

이 작업을 처리 할 수있는 경우 스크립트'add_tagg_02.php '를 사용하여 태그를 직접 변경할 수 있기 때문에 옳다고 생각합니다. – kelsar

관련 문제