问题描述
我在这个问题上看到了很多变体,并且我尝试使用所有知识,但是仍然没有运气.
I have seen the many many variations on this issue, and I've tried to use all the knowledge, but still no luck.
我的日期从旧到新排序,我想将它们从新到旧排序.
My dates are sorting old to new, and I want to sort them new to old.
在您看到desc的地方,我已经尝试过asc,但是没有任何变化.
Where you see desc, I've tried asc, but no change.
当我尝试分页时,似乎触发了重新加载,并且排序是正确的,从新到旧.
When I try the paging, it seems to trigger the reload, and the sort is right, from new to old.
将重载设置为1秒并清除间隔的最佳解决方案是吗?还是我有其他问题?
Is the best solution to set a reload for 1 second, and clear interval? Or do I have something else wrong?
我无法对服务器端进行排序,这不是一个选择.
I can't sort server side, it's just not an option.
$("#transactionList").jqGrid({
url: "/cc/transaction/show/"+accountId,
datatype: "local",
autowidth: true,
height: 'auto',
sortname: 'tran_date',
sortorder: 'desc',
sortable:true,
loadonce:true,
viewrecords: true,
gridview: true,
firstsortorder: 'desc',
colNames:['Date','Asset Name','Description','Amount','Actions'],
colModel:[
{name:'tran_date',index:'tran_date',sorttype:'date',sortable:true,formatter:'date',firstsororder: 'desc',datefmt: 'M d,Y',formatoptions: {srcformat:'Y-m-d H:i:s',newformat:'M d,Y'}},
{name:'assname',index:'assname',sortable:true,resizable:false},
{name:'desccription',index:'desccription',sortable:true,resizable:false},
{name:'net_proc', index:'net_proc',align:'right',formatter:'currency',formatoptions{decimalSeparator:".", thousandsSeparator: ",", decimalPlaces: 2, prefix: "$", defaultValue:'0.00'}, sortable:true,resizable:false}, {name:'ID',index:'ID',formatter:actionsFormatter,width:130,align:"center",key:true,resizable:false}
],
caption: "Completed Transactions",
rowTotal: -1,
rowNum: 1000,
rowList: [10,20,30],
pager: '#pager',
onSelectRow: function(row_id) {
},
jsonReader: {
repeatitems: false,
id: "ID",
userdata: 'rows'
},
viewrecords: true,
gridComplete: function() {
//Attach action event handlers
$('span[name="details"]').click(function() {
var row_id = this.id;
var data = $("#transactionList").getGridParam('userData');
var rowData;
$.each(data, function(index,el){
if(el.ID==row_id)
rowData = el;
});
var message = '<div class="sectionItem"><span class="label">Asset Name: </span><span class="value">'+rowData.assname+rowData.assname2+'</span></div>';
message += '<div class="sectionItem"><span class="label">Amount: </span><span class="value">'+rowData.net_proc+'</span></div>';
message += '<div class="sectionItem"><span class="label">Transaction Date: </span><span class="value">'+rowData.tran_date+'</span></div>';
$.popMessage('Transactions Details', message);
}).addToolTip('Details');
}
})
$("#transactionList").setGridParam({datatype: 'json'}).trigger("reloadGrid");
;
推荐答案
从发布的代码中尚不清楚问题的确切来源.您在发布的代码中使用了sortname: 'tran_date', sortorder: 'desc'
,但是colModel
没有名称为'tran_date'
的列.
The exact origin of the problem is not clear from the code which you posted. You use sortname: 'tran_date', sortorder: 'desc'
in the code posted, but the colModel
has no column with the name 'tran_date'
.
通常,重要的是要了解,服务器代码url: "/cc/transaction/show/"+accountId
必须返回排序的数据.选项sortname: 'tran_date', sortorder: 'desc'
将用于构建将发送到服务器的sidx
和sord
参数的值.因此,服务器必须不按sord
顺序返回按sidx
排序的数据.我想您当前的服务器代码不执行此操作.
In general it's important to understand, that the server code url: "/cc/transaction/show/"+accountId
have to return sorted data. The options sortname: 'tran_date', sortorder: 'desc'
will be used to build the values of sidx
and sord
parameters which will be send to the server. So the server have to return the data sorted by sidx
unsing sord
order. I suppose that your current server code don't do this.
更新:服务器应该返回已排序的数据.如果真的不可能,则必须在首次加载后立即求助于数据.为此,您可以在loadComplete
回调内部测试是否是第一次加载数据 .第一次加载时,datatype
的值是原始值"json"
或"xml"
,具体取决于从服务器返回的数据格式.以后datatype
将更改为"local"
.因此,如果datatype
选项的值不是"local"
,则可以触发"reloadGrid"
.相应的代码如下所示
UPDATE: The server should returns sorted data. If it's really impossible you have to resort the data directly after the first loading. To do this you can test inside of loadComplete
callback whether you are loading the data at the first time. On the first loading the value of datatype
is the original value "json"
or "xml"
depend on the format of the data returned from the server. Later datatype
will be changed to "local"
. So if the value of datatype
option is not "local"
you can trigger "reloadGrid"
. The corresponding code could looks like below
$("#transactionList").jqGrid({
url: "/cc/transaction/show/" + accountId,
datatype: "json",
rowNum: 1, // initial small value
loadonce: true,
loadComplete: function () {
var $this = $(this);
if ($this.jqGrid("getGridParam", "datatype") !== "local") {
setTimeout(function () {
$this.jqGrid("setGridParam", { rowNum: 20 }); // the real value
$this.trigger("reloadGrid");
}, 50);
}
},
... // other options which you need
});
这篇关于jqgrid客户端排序desc/asc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!