//mounted中的部分代码
select2vue = {};
$("select").each(function (index, item) {
var s2 = $(item).select2({
language: "zh-CN", //设置 提示语言
width: "100%", //设置下拉框的宽度
theme: "classic",
placeholder: "请选择"
}).on("change", function (e) {
console.log(e);
var v = $(e.target).val();
var p = $(e.target).attr("data-vuep");
eval("vue_cust_busi." + p + "='" + v + "';");
//$(e.target).find("option").attr("selected",false);
//$(e.target).find("option[value='"+v+"']").attr("selected",true);
});
var p = $(item).attr("data-vuep");
select2vue[p] = s2;
dom2vue[p] = item;
});
setTimeout(function(){
vue_cust_busi.editor.ID_CUST="3";
vue_cust_busi.editor.NAME_CUST="*有限责任公司";
console.log("修改");
},10,null);
//watch中的部分代码
"temp.P1": function (val) {
fire(arguments.callee.name.toString(), val);
},
//通用函数
function fire(p, val) {
$(dom2vue[p]).val(val);
select2vue[p].trigger("change");
}
//html
<select data-vuep="editor.P1" class="form-control ">
<option value="" ></option>
<option v-for="yearOpt in yearOpts" v-bind:value="yearOpt">{{yearOpt}}</option>
</select>
<!DOCTYPE html>
<html>
<head>
<title>vue select2 封装</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="external nofollow" rel="stylesheet" />
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<style type="text/css">
.content{
text-align: center;
padding:50px;
}
.content *{
text-align: left;
}
.select{
width: 350px;
}
</style>
</head>
<body>
<div class="content" id="vue-example">
<select class="select" v-select2='options' v-model="selectValue"></select>
<br/>
<span>结果:{{ selectValue }}</span>
</div>
</body>
<script type="text/javascript">
Vue.directive('select2', {
inserted: function (el, binding, vnode) {
let options = binding.value || {};
$(el).select2(options).on("select2:select", (e) => {
// v-model looks for
// - an event named "change"
// - a value with property path "$event.target.value"
el.dispatchEvent(new Event('change', { target: e.target })); //说好的双向绑定,竟然不安套路
});
},
update: function(el, binding, vnode) {
$(el).trigger("change");
}
});
var vueApp = new Vue({
el: "#vue-example",
data: {
selectValue: '你还没有选值',
options: {
data: [
{ id: 0, text: 'enhancement' },
{ id: 1, text: 'bug' },
{ id: 2, text: 'duplicate' },
{ id: 3, text: 'invalid' },
{ id: 4, text: 'wontfix' }
]
}
}
});
</script>
</html>
Vue.directive('select2', {
inserted: function (el, binding, vnode) {
let options = binding.value || {};
$(el).select2(options).on("select2:select", (e) => {
// v-model looks for
// - an event named "change"
// - a value with property path "$event.target.value"
el.dispatchEvent(new Event('change', { target: e.target })); //说好的双向绑定,竟然不安套路
console.log("fire change in insert");
});
},
update: function (el, binding, vnode) {
for (var i = 0; i < vnode.data.directives.length; i++) {
if (vnode.data.directives[i].name == "model") {
$(el).val(vnode.data.directives[i].value);
console.log("new value in update:"+vnode.data.directives[i].value);
}
}
$(el).trigger("change");
console.log("fire change in update");
}
});
//html代码
<select v-select2="" v-model="editor.P1" required="required" class="form-control ">
<option value=""></option>
<option v-for="item in codes" v-bind:value="item.NAME">{{item.NAME}}</option>
</select>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有