【问题标题】:jQuery TextExt: Tags with custom data objectsjQuery TextExt:带有自定义数据对象的标签
【发布时间】:2015-04-28 00:19:41
【问题描述】:

我目前正在努力为我的网站实现一个带有自动完成功能的标签(带有自定义数据对象)的 jQuery 插件。可以在这里找到 jQuery TextExt (http://textextjs.com/)。我目前正在努力为每个标签使用自定义数据对象,只能从自动完成的内容中选择。基于此示例 (http://textextjs.com/manual/examples/tags-with-custom-data-objects.html),我试图弄清楚在选择标签时如何返回“name”和“id”。有谁知道如何实现这一目标或为我指明正确的方向?

也许答案就在这个例子中的某个地方 (http://textextjs.com/manual/examples/filter-with-suggestions.html)?

这是我写的,它不起作用(它只返回名称,我尝试将“item.id”添加到函数中,但这对我也不起作用):

<script type="text/javascript">
jQuery(document).ready(function( $ ){
    jQuery('#textarea').textext({
        plugins: 'tags',
        items: [
            { name: 'PHP', id: '1' },
            { name: 'Closure', id: '2' },
            { name: 'Java', id: '3' }
        ],

        ext: {
            itemManager: {
                stringToItem: function(str)
                {
                    return { name: str };
                },

                itemToString: function(item)
                {
                    return item.name ;
                },

                compareItems: function(item1, item2)
                {
                    return item1.name == item2.name;
                }
            }
        }
    });
})
</script>

【问题讨论】:

    标签: javascript jquery json jquery-textext


    【解决方案1】:

    您的 itemManager 代码应该如下所示,您需要将建议存储在自定义数组中,以便在 stringToItem 方法中查找它们的相关 ID

    itemManager: {
        items: [],  // A custom array that will be used to lookup id    
        stringToItem: function(str)
        {
            //Lookup id for the given str from our custom array
            for (var i=0;i<this.items.length;i++)
                if (this.items[i].name == str) {
                    id = this.items[i].id;
                    break;
                }    
           return { name: str, id: id };
        },
    
        itemToString: function(item)
        {
            //Push items to our custom object
            this.items.push(item);
            return item.name ;
    
        },
        compareItems: function(item1, item2)
        {
            return item1.name == item2.name;
        }    
    }
    

    【讨论】:

    • 我用您发送的内容替换了我的 itemManager 代码,但没有任何效果.. :/ 我还将我的插件调整为以下内容并包含 JS 文件plugins: 'autocomplete suggestions tags filter',
    猜你喜欢
    • 2018-09-21
    • 1970-01-01
    • 2018-03-08
    • 2023-03-27
    • 2012-09-20
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多