docs/table/detail/options.ajax.md
发送异步请求的 URL。默认会自动传递两个参数:?page=1&limit=30(该参数可通过 request 属性自定义)
page 代表当前页码、limit 代表每页数据条数。
请求的方式,默认:get
请求的其他参数。如:where: {token: 'sasasas', id: 123}
请求的数据头参数。如:headers: {token: 'sasasas'}
请求的内容编码类型。若要发送 json 内容,可设置:
contentType: 'application/json'
请求的数据类型,默认 json。
设置当 dataType: 'jsonp' 时的回调函数名。
用于对默认的分页相关的请求参数 page,limit 重新设定名称。如:
request: {
pageName: 'curr', // 页码的参数名称,默认:page
limitName: 'nums' // 每页数据条数的参数名,默认:limit
}
那么请求数据时的参数将会变为 ?curr=1&nums=30
{
"code": 0,
"msg": "",
"count": 1000,
"data": [{}, {}]
}
很多时候,您接口返回的数据格式并不一定都符合 table 默认规定的格式,比如:
{
"status": 0,
"message": "",
"total": 180,
"data": {
"item": [{}, {}]
}
}
此时我们可以借助 parseData 回调函数将数据解析并转换为默认规定的格式:
table.render({
elem: '',
url: '',
parseData: function(res){ // res 即为原始返回的数据
return {
"code": res.status, // 解析接口状态
"msg": res.message, // 解析提示文本
"count": res.total, // 解析数据长度
"data": res.data.item // 解析数据列表
};
},
// … //其他参数
});
该函数非常实用
</td> </tr> <tr> <td>ajax <sup>2.12+</sup>
</td> <td> <div class="ws-anchor" id="options.ajax"> 自定义 ajax 请求,用于发送异步请求。 </div>table.render({
// 自定义 ajax 请求
// origOptions - 包含了原始的请求参数
// type - 执行 ajax 请求的来源,'table' 或 'treeNodes'
ajax: function (origOptions, type) {
$.ajax({
url: origOptions.url,
data: origOptions.data,
dataType: origOptions.dataType,
})
.done(function (data) {
// 调用原始的 success 回调
origOptions.success(data);
})
.fail(function (xhr, status, error) {
// 调用原始的 error 回调
origOptions.error(xhr, status, error);
})
.always(function () {
// 调用原始的 complete 回调
if (typeof origOptions.complete === "function") {
origOptions.complete();
}
});
}
});
在返回的数据中,允许规定某些特定字段,以便 table 组件进行相应的特定解析。
| 特定字段名 | 描述 | 读写状态 |
|---|---|---|
| LAY_CHECKED | 当前行的选中状态 | 可读可写 |
| LAY_DISABLED | 当前行是否禁止选择 | 可读可写 |
| LAY_INDEX | 当前行下标。每页重新从零开始计算 | 只读 |
| LAY_NUM | 当前行序号 | 只读 |
| LAY_COL | 当前列的表头属性选项 | 只读 |
示例一: 在返回的数据中设置特定字段:
{
"code": 0,
"count": 1000,
"data": [{},{
LAY_DISABLED: true
}]
}
示例二: 在模板中读取特定字段示例:
{{!
<script type="text/html" id="TPL-demo-xxx">
当前行下标: {{= d.LAY_INDEX }}
当前列的某个表头属性: {{= d.LAY_COL.field }}
</script>
!}}