-
I have been struggling with how to introduce a js library for several days and I am very frustrated. Following is a example. I use this javascript library for select province/city in China. Basically, only three file is needed.
the first file copied into
<script>
tag.addEventListener('click', function () {
var iosSelect = new IosSelect([iosProvinces, iosCitys]);
});
</script> css file can be import successful use But, The really issue is, i don't know how to import variable The only working solution is, copy those files into I have little knowledge about use import in ES6, anyone can help me, thanks a lot! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You should have a /* eslint no-console:0 */
// Rails Unobtrusive JavaScript (UJS) is *required* for links in Lucky that use DELETE, POST and PUT.
// Though it says "Rails" it actually works with any framework.
import Rails from "@rails/ujs";
Rails.start();
// Add this line
import IosSelect from "iosselect";
// now use the library like normal
var data=[{'id': '10001', 'value': '演示数据1'},{'id': '10002', 'value': '演示数据2'}];
var showDom = document.querySelector('#showDom');// 绑定一个触发元素
var valDom = document.querySelector('#valDom'); // 绑定一个存储结果的元素
showDom.addEventListener('click', function () { // 添加监听事件
var val = showDom.dataset['id']; // 获取元素的data-id属性值
var title = showDom.dataset['value']; // 获取元素的data-value属性值
// 实例化组件
var example = new IosSelect(1, // 第一个参数为级联层级,演示为1
[data], // 演示数据
{
container: '.container', // 容器class
title: '演示标题', // 标题
itemHeight: 50, // 每个元素的高度
itemShowCount: 3, // 每一列显示元素个数,超出将隐藏
oneLevelId: val, // 第一级默认值
callback: function (selectOneObj) { // 用户确认选择后的回调函数
valDom.value = selectOneObj.id;
showDom.innerHTML = selectOneObj.value;
showDom.dataset['id'] = selectOneObj.id;
showDom.dataset['value'] = selectOneObj.value;
}
});
}); |
Beta Was this translation helpful? Give feedback.
You should have a
src/js/app.js
file. In this file you should see