3 Star 0 Fork 0

Gitee 极速下载/Populate-jq

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/e-sites/populate
克隆/下载
jquery.populate.js 2.32 KB
一键复制 编辑 原始数据 按行查看 历史
Boye Oomens 提交于 2013-02-01 10:40 . Fixed jshint warnings
/**
* Small plugin that populates a <select> element with option nodes
* This can be either an object literal, a HTML fragment or a serialized string
*
* @author Boye Oomens <boye@e-sites.nl>
* @version 0.1.0
* @param {object|string} nodes actual options nodes that need to be injected
* @param {object} options custom config options
* @return {object} jQuery
*/
;(function ($) {
'use strict';
$.fn.extend({
populate: function (nodes, options) {
var defaults = {
select: null,
exclude: null,
opPopulate: null
},
optionNode = '<option value="{key}">{value}</option>',
htmlExpr = /^(?:(<[\w\W]+>)[^>]*|#([\w-]*))$/,
opts = [],
n;
/**
* Small helper function to process <option> nodes
*
* @param {object} node object that maps to
* @return {string} option node
* @private
*/
function template(node) {
return optionNode.replace('{key}', node.key).replace('{value}', node.value);
}
// Handle both objects as well strings
if ( nodes instanceof Object ) {
for (n in nodes) {
if ( nodes.hasOwnProperty(n) ) {
opts.push(
template({
'key': n,
'value': nodes[n]
})
);
}
}
} else if ( nodes.constructor === String && nodes !== '' ) {
// Are we dealing with a HTML fragment?
if ( htmlExpr.test( nodes ) ) {
opts = nodes;
} else {
// Presume it's a serialized string
$.map( nodes.split('&'), function (val) {
opts.push(
template({
'key': val.split('=')[0],
'value': val.split('=')[1].replace('+', ' ')
})
);
});
}
}
return this.each(function () {
var o = $.extend(defaults, options),
self = this,
$self = $(self);
// <select> elements only
if ( self.nodeName === 'SELECT' ) {
// Inject nodes
$self
.find((o.exclude ? 'option:not(' + o.exclude + ')' : 'option'))
.remove()
.end()
.append(opts)
.removeAttr('disabled');
// Apply filter
if ( o.select !== null ) {
$(self.options)
.removeAttr('selected')
.filter(o.select)
.attr('selected', 'selected');
}
// Invoke callback
if ( o.onPopulate && $.isFunction(o.onPopulate) ) {
o.onPopulate.apply(self, [opts]);
}
}
});
}
});
}(jQuery));
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/mirrors/Populate-jq.git
git@gitee.com:mirrors/Populate-jq.git
mirrors
Populate-jq
Populate-jq
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385