select2官方文档描述的 templateResult
option。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| //自定义select2 option 展示模板 function columnSelectFormat(selectDom){ selectDom.select2({templateResult:function(state){ var dom = $(state.element); if ( !dom.val() ) return '(value为空)'; var $state = $('<div title="'+dom.text()+'"><span>'+dom.text()+'</span><span>' + dom.val() + '</span></div>'); return $state; }, templateSelection: function(state){ var dom = $(state.element); if ( !dom.val() ) return '(value为空)'; var $state = $('<div title="'+dom.text()+'"><span>'+dom.text()+'</span><span>' + dom.val() + '</span></div>'); return $state; }}); }
// $.prototype $.fn.extend({ columnSelectFormat: function(){ columnSelectFormat(this); } });
|