在 layui 表格中使用带加减按钮的数字输入框,需要:引入 layui-form 和 layui-number 模块;定义表格列时,设置 edit 属性为 'text' 并指定 numbers: true 选项;渲染表格;使用内置的数字输入框组件,设置 name 和 lay-verify="number" 属性。
如何在 layui 表格中使用带加减按钮的数字输入框?
在 layui 表格中使用带加减按钮的数字输入框,需要引入 layui-form 模块和 layui-number 模块。
引入模块
<script src="layui/layui.js"></script> <script> layui.use(['form', 'number'], function(){ var form = layui.form, number = layui.number; }); </script>
登录后复制
使用步骤
- 定义表格列,设置 edit 属性为 'text' 并指定 number 选项。
<table id="test" lay-filter="test"> <thead> <tr> <th lay-data="{field:'id', width:80, edit:'text'}">id</th> <th lay-data="{field:'username', width:120, edit:'text'}">用户名</th> <th lay-data="{field:'email', width:180, edit:'text'}">邮箱</th> <th lay-data="{field:'age', width:100, edit:'text', numbers: true}">年龄</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>张三</td> <td>zhangsan@qq.com</td> <td><input type="number" name="age" lay-verify="number" value="18" /></td> </tr> <tr> <td>2</td> <td>李四</td> <td>lisi@qq.com</td> <td><input type="number" name="age" lay-verify="number" value="20" /></td> </tr> </tbody> </table>
登录后复制
- 渲染表格。
<script> layui.use(['table'], function(){ var table = layui.table; table.render({ elem: '#test', cols: [[ {field:'id', width:80, edit:'text'}, {field:'username', width:120, edit:'text'}, {field:'email', width:180, edit:'text'}, {field:'age', width:100, edit:'text', numbers: true} ]] }); }); </script>
登录后复制
- 使用内置的数字输入框组件。
layui 内置了数字输入框组件,使用 number 属性可以轻松配置输入框。
<input type="number" name="age" lay-verify="number" value="18" />
登录后复制
以上配置将生成一个带有加减按钮的数字输入框,并且该输入框会受到 layui 表格的验证机制约束。
以上就是如何在layui表格中使用带加减按钮的数字输入框的详细内容,更多请关注代码网其它相关文章!
发表评论