我正在使用辉煌的selectize.js库生成一个有选择组的有吸引力的选择框.这一切都正常,但我不能使用示例页面中的自定义渲染器(电子邮件联系人)http://brianreavis.github.io/selectize.js/,因为"item"不知道"电子邮件"属性.我知道如何在javascript中执行此操作,但如何在静态html中定义这两个属性?
在js中,这个woulde是
$('#id').selectize({ ... options: [ { name: "Martin", email: "martin@asdf.at" } ], .... }
我尝试了以下方法:
但这不起作用......最后从示例中获取渲染函数:
render: { item: function(item, escape) { return '' + (item.name ? '' + escape(item.name) + '' : '') + (item.email ? '' + escape(item.email) + '' : '') + ''; }, option: function(item, escape) { var label = item.name || item.email; var caption = item.name ? item.email : null; return '' + '' + escape(label) + '' + (caption ? ' ' : '') + ''; } }
我会感谢任何提示!
此致,马丁
使用此示例:
var clearhack = $('.selectized').selectize({ valueField: 'id', labelField: 'name', searchField: ['name'], sortField: 'score',//this is set to 'name' on my version, but seems sortField is only used together with load-function and score-function sortDirection: 'desc', maxItems: 1, //only using options-value in jsfiddle - real world it's using the load-function options: [ {"id":861,"name":"Jennifer","score":6}, {"id":111,"name":"Jenny","score":6}, {"id":394,"name":"Jorge","score":6}, {"id":1065,"name":"Jorge Carlson","score":6}, {"id":389,"name":"Ann Jennifer","score":3}, {"id":859,"name":"Bobby Jenkins","score":3}, {"id":264,"name":"Peter Jenkins","score":3}, {"id":990,"name":"Fred","score":1}, {"id":349,"name":"Kal","score":1}, {"id":409,"name":"Louis","score":1} ], create: false, render: { option: function(item, escape) { return '' + 'ID:'+item.id+' ' + 'Name:'+item.name+' ' + 'DEBUG:'+item.score+'' + ''; } }, score: function(search) { return function(item) { return parseInt(item.score); }; } });