VUE-下拉菜单不使用组件,项目上的js实现

pageInit.js:提供js的 function

 

/*
* 重新设置选项框的值
* _that: vue对象
* el:需要重新设置值的dom选择器
* defaultValue: 默认值
*/
export function resetSelectValue(_that, el, defaultValue) {
    $(el).each(function() {
        var select = $(this).find('select');
        var opt = $(this).find('select>option:first');
        if (defaultValue) {
            opt = $(this).find('select>option[value=' + defaultValue + ']');
        }
        if (opt != null && opt.length > 0) {
            var val = opt.val();
            var text = opt.text();
            select.val(val);
            var isbind = $(this).attr('isbind');
            // 如果绑定了vue属性,则设置选项框的同时需要设置vue对象值
            if (isbind == 'true') {
                var name = $(this).attr('bindattr');
                // var name = select.attr('name');
                if (name) {
                    var names = name.split('.');
                    var obj = _that;
                    for (var i = 0; i < names.length - 1; i++) {
                        obj = obj[names[i]];
                    }
                    obj[names[names.length - 1]] = val;
                }
            }
            
            $(this).find('[type=text]').val(text);
        }
    });
}

/*
* 设置表头固定,数据发生变化,加载完列表后调用
*/
export function resetListHeader() {
    // 初始化表头
    $(".right .listWrapper").each(function(){
    /*    var lisW=[];
        var obj = $(".right .listWrapper");
        var lis = obj.find(".listHead").children().children("li");
        for(var i=0;i
*   
*   
* 
* 属性说明: * isbind:是否绑定vue属性,true/false * bindattr:绑定的属性名称,isbind="true"时有效 * callback:切换下拉框后的回调,默认无 * 也可以使用jquery获取select的值获取属性值 */ function initSelect(_that) { $(".selectIptFxd").unbind('click').bind("click", function(event) { if(!$(this).hasClass("disabled")) { $(".selectOpt").slideUp(); $(".selectIptFxd").not($(this)).removeClass("open"); $(".selectOptFxd").hide().remove(); clkElJQ=$(this); clkElJS=$(this)[0]; if($(this).hasClass("open")) { $(this).removeClass("open"); $(".selectOptFxd").hide().remove(); } else { $(this).addClass("open"); initOptions(); setPosition(); initOptionEvent(_that); } event.stopPropagation(); } }) } /* * 初始化新版选项框中值,页面初始化使用 * _that:当前vue对象 * el: jquery选择器,string */ export function initSelectValue(_that, el) { $(el + " .selectIptFxd").each(function() { var obj = $(this); var isbind = obj.attr('isbind'); var value = ''; if (isbind == 'true') { var attr = obj.attr('bindattr'); if (attr) { var attrs = attr.split('.'); var temp = _that; for (var i = 0; i < attrs.length - 1; i++) { temp = temp[attrs[i]]; } value = temp[attrs[attrs.length - 1]]; } } var text = obj.find('option[value=' + value + ']').text(); obj.children("[type=text]").val(text); }); } /* * 初始化选项 * _that:当前vue对象 */ function initOptions(_that) { var opts = []; var optsLi = ""; for(var i = 0; i < clkElJQ.find("option").length; i++) { //opts[i]=clkElJQ.find("option")[i].text(); var text = clkElJQ.find("option").eq(i).text().replace(//g, '>'); optsLi = optsLi + "
  • " + text + "
  • "; } var optsEl = "
      " + optsLi + "
    "; $("body").append(optsEl); } /* * 设置位置 */ function setPosition() { if (!clkElJS) { return; } var top = clkElJS.getBoundingClientRect().top; var height = clkElJS.getBoundingClientRect().height; var left = clkElJS.getBoundingClientRect().left; var width = clkElJS.getBoundingClientRect().width; if(top - height > $(window).height() / 2) { if(clkElJQ.find("option").length * 30 + 2 > ($(window).height() - top - height)) { $(".selectOptFxd").css({"left": left + "px", "top": "auto", "bottom": $(window).height() - top - 2 + "px", "width": width + "px"}); if(clkElJQ.find("option").length * 30 + 2 > top) { console.log("topOverflow"); $(".selectOptFxd").css({"left": left + "px", "top": "0px", "bottom": $(window).height() - top - 2 + "px", "width": width + "px"}); } else { $(".selectOptFxd").css({"left": left + "px","top": "auto", "bottom": $(window).height() - top - 2 + "px", "width": width + "px"}); } } else { $(".selectOptFxd").css({"left": left + "px", "top": top + height - 2 + "px", "bottom": "auto", "width": width + "px"}) } } else { if(clkElJQ.find("option").length * 30 + 2 > ($(window).height() - top - height)) { $(".selectOptFxd").css({"left": left + "px", "top": top + height - 2 + "px", "bottom": "0px", "width": width + "px"}) } else { $(".selectOptFxd").css({"left": left + "px", "top": top + height - 2 + "px", "bottom": "auto", "width": width + "px"}) } } } /* * 初始化li上事件 */ function initOptionEvent(_that) { $('.selectOptFxd li').unbind('click').bind('click', function(event) { var title = $(this).attr("title"); var value = $(this).attr("val"); clkElJQ.children("select").val(value); clkElJQ.children("[type=text]").val(title); clkElJQ.removeClass("open"); clkElJQ.children("select").find('option').attr('selected', false); clkElJQ.children("select").find('option[value="' + value + '"]').attr('selected', true); console.log(clkElJQ.children("select").parent().html()) $(".selectOptFxd").hide().remove(); var isbind = clkElJQ.attr('isbind'); if (isbind == 'true') { var attr = clkElJQ.attr('bindattr'); if (attr) { var attrs = attr.split('.'); var obj = _that; for (var i = 0; i < attrs.length - 1; i++) { obj = obj[attrs[i]]; } obj[attrs[attrs.length - 1]] = value; } } var callback = clkElJQ.attr('callback'); if (callback) { eval('_that.' + callback + '(event)'); } //setTimeout(function(){$(window).trigger("resize")},300) event.stopPropagation(); }) }

    使用技巧:

     

    1、格式必须相同

    2、

    • bindattr="recordInfo.storeType",下拉框选择完之后的值赋给变量recordInfo.storType
    • isbind:是否绑定vue属性,true/false

    3、

    • value="$t('message.cloudset.main.display.alwayssave')",此处为初始显示的值

    4、

    • value="-1" 选择这个option,则会将recordInfo.storType=-1
    • {{$t('message.cloudset.main.display.alwayssave')}} 这个option显示的值

    再举一个例子。使用v-for的:

     

    你可能感兴趣的:(VUE)