jqgrid 初始排序 多个字段
时间: 2023-08-04 17:32:46 浏览: 66
如果你想在 jqGrid 中进行多列的初始排序,可以将 `sortname` 设置为一个数组,数组中的每个元素表示一个需要排序的列名,按照数组中的顺序依次排序。
例如,如果你想将 jqGrid 的初始排序方式设置为按照 ID 列降序排列,如果 ID 列相同则按照年龄升序排列,可以这样写:
``` javascript
$("#grid").jqGrid({
url: "/data",
datatype: "json",
colModel: [
{ name: "id", index: "id", sorttype: "int" },
{ name: "name", index: "name" },
{ name: "age", index: "age", sorttype: "int" }
],
sortname: ["id", "age"],
sortorder: ["desc", "asc"],
// 其他选项...
});
```
在上面的例子中,`sortname` 设置为 `["id", "age"]`,`sortorder` 设置为 `["desc", "asc"]`,表示初始排序方式为按照 ID 列降序排列,如果 ID 列相同则按照年龄升序排列。需要注意的是,`sortname` 和 `sortorder` 的数组元素个数必须一致,否则会导致排序错误。
相关问题
jqgrid 初始排序
你可以使用 `sortname` 和 `sortorder` 选项来设置 jqGrid 的初始排序方式。其中,`sortname` 表示需要排序的列名,`sortorder` 表示排序的方式,可以是升序(`asc`)或降序(`desc`)。
例如,如果你想将 jqGrid 的初始排序方式设置为按照 ID 列降序排列,可以这样写:
``` javascript
$("#grid").jqGrid({
url: "/data",
datatype: "json",
colModel: [
{ name: "id", index: "id", sorttype: "int" },
{ name: "name", index: "name" },
{ name: "age", index: "age", sorttype: "int" }
],
sortname: "id",
sortorder: "desc",
// 其他选项...
});
```
在上面的例子中,`sortname` 设置为 `"id"`,`sortorder` 设置为 `"desc"`,表示初始排序方式为按照 ID 列降序排列。注意,需要将 ID 列的 `index` 属性设置为 `"id"`,才能使 `sortname` 生效。
jqgrid 渲染表格 其余的字段 丢失
在 jqGrid 渲染表格时,如果您的数据中包含了一些您没有在列模型中定义的字段,那么这些字段的数据会被忽略,不会出现在表格中。
为了解决这个问题,您可以在列模型中添加一个隐藏列来存储这些额外的字段。您可以在这个隐藏列中使用 formatter 函数来显示您需要的数据。例如,如果您的数据中包含一个名为 `extra` 的字段,您可以添加一个隐藏列来存储这个字段的值:
```javascript
{
name: 'extra',
hidden: true
}
```
然后,您可以使用 formatter 函数来显示这个隐藏列中的数据。在 formatter 函数中,您可以使用 `cellvalue` 参数来获取隐藏列中的值,并将其显示在需要的位置。例如,如果您想在表格的第二列中显示 `extra` 字段的值,可以使用以下代码:
```javascript
{
name: 'name',
index: 'name',
width: 100,
formatter: function(cellvalue, options, rowObject) {
return rowObject.extra; // 在第二列中显示 extra 字段的值
}
}
```
请注意,这种方法只适用于您需要显示的字段数量比较少的情况。如果您的数据中包含了大量的额外字段,您可能需要重新考虑您的数据结构,或者使用其他的解决方案来处理这个问题。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)