layui带参查询
时间: 2023-06-29 14:09:46 浏览: 41
在 layui 中,可以通过发送 Ajax 请求带参查询数据。具体步骤如下:
1. 在页面上定义一个表格,例如:
```html
<table id="table"></table>
```
2. 在 JavaScript 中编写 Ajax 请求代码,例如:
```javascript
layui.use('table', function(){
var table = layui.table;
//执行一个 table 实例
table.render({
elem: '#table',
url: '/api/data', //数据接口
where: { //传递参数
name: '张三',
age: 18
},
cols: [[ //表头
{field: 'id', title: 'ID', width:80},
{field: 'username', title: '用户名', width:120},
{field: 'email', title: '邮箱', width:150},
{field: 'sex', title: '性别', width:80, sort: true},
{field: 'city', title: '城市', width:100},
{field: 'sign', title: '签名', width:200},
{field: 'experience', title: '积分', width:80, sort: true},
{field: 'score', title: '评分', width:80, sort: true},
{field: 'classify', title: '职业', width:100},
{field: 'wealth', title: '财富', width:135, sort: true},
{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
]]
});
});
```
其中,`url` 表示数据接口的地址,`where` 表示传递的参数。
3. 在后端编写数据接口的代码,例如 PHP 中可以这样:
```php
<?php
$name = $_GET['name']; //获取 name 参数
$age = $_GET['age']; //获取 age 参数
//根据参数查询数据并返回
//...
?>
```
4. 最后,在浏览器中访问页面,即可看到带参查询的结果。