1

次のような JSON 配列を取得します。

[{"type":"user","id":"12","name":"Name of User"},{"type":"group","id":"9","name":"Name of UserGroup"}]

バックエンドは、名前、ID、およびタイプ (ユーザーまたはグループ) を含む JSON 配列を返します。

入力フィールドに入力する前にIDを操作する方法はありますか?

 type+"_"+id

?

実際には、これを使用して tokenInput を初期化しています。

$("input#id").tokenInput(apiURL+"search.php", {
                defaultValue:"User or User Group",
                hintText:"Fill in User or User Group name.",
                noResultsText:"nothing found.",
                searchingText:"Searching...",
                queryParam:"search",
                crossDomain:false,
                tokenLimit:5,
                preventDuplicates:true,
                minChars:3,
                resultsFormatter:function(item) {
                    return "<li><img src="+((item.type=="user") ? "contact" : "contact_group")+ ".png\"/>"+item.name+"</li>";
                }
            });

Rory McCrossan の助けを借りて、私はそれを機能させました。id を resultsFormatter メソッドに変更すると、うまくいきます。

resultsFormatter:function(item) {
     if (item.id.indexOf("_") == -1)
     {
           item.id = item.type+"_"+item.id;
     }

     return "<li><img src="+((item.type=="user") ? "contact" : "contact_group")+ ".png\"/>"+item.name+"</li>";

}

"if (item.id.indexOf (...))" は必要ありません。tokenInput がエントリをキャッシュするためです。同じ文字列を検索すると、ID には既に type+"_" プレフィックスがあり、それを取得します。 2回目/3回目/...

4

1 に答える 1

1

これを試して:

resultsFormatter: function(item) {
    var itemId = item.type + "_" + item.id;
    return "<li id='" + itemId + "'><img src="+((item.type=="user") ? "contact" : "contact_group")+ ".png\"/>"+item.name+"</li>";
}
于 2012-05-29T12:32:27.633 に答える