Hbuilder MUI 选择器(Picker)设置默认选项

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hbuilder MUI 选择器(Picker)设置默认选项相关的知识,希望对你有一定的参考价值。

mui 联动选择初始化很多人都遇到问题,就是setselectedvalue 不好用,实际上这个地方应该是有bug的

所以只能用setselectedindex 来实现

下面我是我的实现方式供大家参考:

技术分享
<script>
            (function($, doc) {
                $.init();
                $.ready(function() {
                    //-----------------------------------------
                    //                    //级联示例
                    var cityPicker3 = new $.PopPicker({
                        layer: 3
                    });
                    cityPicker3.setData(cityData3);
                    cityPicker3.pickers[0].setSelectedValue(130000);
                    var shi=cityPicker3.getSelectedItems()[0].children;
                    var val_shi=130200;
                    var val_xian=130204;
                    for(var i=0;i<shi.length;i++)
                    {                        
                        if(shi[i].value==val_shi)
                        {
                            cityPicker3.pickers[1].setSelectedIndex(i);
                            var xian=shi[i].children;
                            for(var j=0;j<xian.length;j++)
                            {
                                if(xian[j].value==val_xian)
                                {
                                    cityPicker3.pickers[2].setSelectedIndex(j);
                                }
                            }
                        }                        
                    }
                    //cityPicker3.pickers[1].setSelectedIndex(2);
                    //cityPicker3.pickers[2].setSelectedIndex(3);
                    //cityPicker3.pickers[1].setSelectedValue(130205);
                    var showCityPickerButton = doc.getElementById(‘showCityPicker3‘);
                    var cityResult3 = doc.getElementById(‘cityResult3‘);
                    showCityPickerButton.addEventListener(‘tap‘, function(event) {
                        cityPicker3.show(function(items) {
                            cityResult3.innerText = "你选择的城市是:" + (items[0] || {}).text + " " + (items[1] || {}).text + " " + (items[2] || {}).text;
                            //返回 false 可以阻止选择框的关闭
                            //return false;
                        });
                    }, false);
                });
            })(mui, document);
        </script>
View Code

 

以上是关于Hbuilder MUI 选择器(Picker)设置默认选项的主要内容,如果未能解决你的问题,请参考以下文章

MUI组件四:选择器滚动条单选框区域滚动和轮播组件

HBuilder+移动APP开发实例选择模版 这里选择mui项目,会自动引入mui的js和css,如下:

mui-picker 增加过滤

Hybrid App系列之工具篇

HBuilder完成webApp入门

HBuilder开发App教程06-首页