资讯专栏INFORMATION COLUMN

新浪微博,微信@功能jquery插件,@xxsome,ajax请求数据,前端渲染列表项选择!

AJie / 1147人阅读

摘要:插件使用官网官网有使用简介,这里记录下插件使用过程遇到的坑。使用中数据格式是这样的发请求后返回数据结构是这样的所以将数据结构写成符合预期的这个参数可能有几个是会报错,过滤掉。完整栗子如下请使用产品名模糊查询后选择对应产品下载

jquery.mentsInput插件使用:
官网:http://podio.github.io/jquery...
官网有使用简介,这里记录下插件使用过程遇到的坑。
js部分:
if( settings.elastic ) {
elmInputBox.elastic();
}
会报错,这里注释掉 elmInputBox.elastic();可以继续使用。
使用中数据格式是data这样的:

$("textarea.mention").mentionsInput({
onDataRequest:function (mode, query, callback) {
var data = [

  { id:1, name:"Kenneth Auchenberg", "avatar":"http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif", "type":"contact" },
  { id:2, name:"Jon Froda", "avatar":"http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif", "type":"contact" },
  { id:3, name:"Anders Pollas", "avatar":"http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif", "type":"contact" },
  { id:4, name:"Kasper Hulthin", "avatar":"http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif", "type":"contact" },
  { id:5, name:"Andreas Haugstrup", "avatar":"http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif", "type":"contact" },
  { id:6, name:"Pete Lacey", "avatar":"http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif", "type":"contact" }
];

data = _.filter(data, function(item) { return item.name.toLowerCase().indexOf(query.toLowerCase()) > -1 });

callback.call(this, data);

}
});
发请求后api返回数据结构是这样的:
[{
"last_update_time":1479719379472,
"asin":"B012AADEMI",
"list_price":null,
"name":"WildBird Care Vertical Pull-out Tray Bird Feeder BCF4A,
Natural color", "publisher":"WildBird Care LLC",
"product_width":9.0,
"product_length":6.0,
"storage_item":[
],
"brand":"WildBird Care",
"purchase_items":[
],
"standard_sku":null,
"product_type_name":"PET_SUPPLIES",
"image_url":"http://ecx.images-amazon.com/...",
"product_height":14.2,
"product_group":"Pet Products",
"id":"bbd45493-baa0-4862-ab24-7083ea203b88",
"product_weight":2.4
},
{
"last_update_time":1479859200000,
"asin":"B012AADEWI",
"list_price":24.15,
"name":"WildBird Care Wooden Upside-down Suet Feeder BCF2A,
Natural Color", "publisher":"WildBird Care LLC",
"product_width":9.1,
"product_length":9.3,
"storage_item":[
],
"brand":"WildBird Care",
"purchase_items":[
],
"standard_sku":null,
"product_type_name":"PET_SUPPLIES",
"image_url":"http://ecx.images-amazon.com/...",
"product_height":4.7,
"product_group":"Pet Products",
"id":"87a04d9b-0b69-4e3d-a19f-19f3d4bdb4f0",
"product_weight":1.95
}]

所以将数据结构写成符合预期的:name这个参数可能有几个是 null会报错,过滤掉。
var info = $.grep(JSON.parse(resData),function (v,i) {

                    if(v.name!=null){
                        return v.name;
                    }
                });
                var arr = [];
                for (var i = 0;i

预期的数据结构这样:
{
"id" : 1,
"name" : "Kenneth Auchenberg",
"avatar": "http://cdn0.4dots.com/i/custo...",
"icon" : "icon-16 icon-person",
"type" : "contact"
}
我把id这个参数换成了要反会给后端的asin(类似id),在列表项显示产品名称,选择对应的产品后,
会替换成对应的产品asin;然后在调用getMentions这个函数获取数据传给后端。
var asin = "";

    $("#asin").mentionsInput("getMentions", function(data) {
        $.each(data,function (index,val) {
            asin += val.id+";"
        })
    });

完整栗子如下:


文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/81193.html

相关文章

  • FEDay 参会小记

    摘要:介绍微信风格的,与客户端体验一致,这个自己去微信上看吧,略。微信调试一件套,网页授权模拟集成代理远程调试。这些在微信开发者中心有介绍,略。年微信开发经验的人,终于又成为了零年开发经验的人,重新走上了踩坑之路。 showImg(https://segmentfault.com/img/bVtEd1);活动地址:http://fequan.com/2016/ 注意:英文不好,小记也带有自己...

    xcc3641 评论0 收藏0
  • 全栈开发自学路线

    摘要:前言这里筑梦师是一名正在努力学习的开发工程师目前致力于全栈方向的学习希望可以和大家一起交流技术共同进步用简书记录下自己的学习历程个人学习方法分享本文目录更新说明目录学习方法学习态度全栈开发学习路线很长知识拓展很长在这里收取很多人的建议以后决 前言 这里筑梦师,是一名正在努力学习的iOS开发工程师,目前致力于全栈方向的学习,希望可以和大家一起交流技术,共同进步,用简书记录下自己的学习历程...

    galaxy_robot 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<