问题描述
我正在尝试在Ruby on Rails应用程序中为jqgrid使用自定义格式器,但是在使其响应方面遇到了很大的困难.
I am trying to use a custom formatter for jqgrid in my Ruby on Rails application, but I am having terrible difficulty in getting it to respond.
这是我正在使用的功能:
Here is the function I am using:
function YNFormatter(cellvalue, options, rowObject)
{
var color = (cellvalue >= 10) ? "green" : "red";
return '<span style="background-color:' + color + ';">' + cellvalue + '</span>';
}
但是,我的网格仍然显示,但是没有任何格式发生.
However, my grid still displays, but without any formatting occurring.
另外,为了提供上下文,以下是我的index.html.erb的jqgrid代码的其余部分,其中包括上述功能:
Also, to give context, below is the rest of the jqgrid code for my index.html.erb with the above function included:
<div id="ItmDisplay"></div>
<table id="grid" class="jqInquiryGrid tblGen tblInlineEdit"></table>
<div id="gridPager"></div>
<script>
$("#grid").jqGrid({
url:'http://localhost:3000/ItmList',
datatype: "json",
altRows:true,
altclass:'oddRow',
jsonReader : {
root:"itmdata",
page: "currpage",
total: "totalpages",
records: "totalrecords",
cell: "itmrow"
},
rowNum:10,
rowList:[10,20,30],
mtype: "GET",
width:796,
hidegrid:false,
loadonce: true,
pager: 'gridPager',
sortname: 'id',
viewrecords: true,
sortorder: "asc",
search: true,
height: 250,
width: 600,
colNames: ['Itm No', 'Title', 'Quantity', 'Category'],
colModel: [{
name: 'id',
index: 'id',
width: 30,
sorttype: "int"},
{
name: 'title',
index: 'title',
width: 90,
sorttype: "String"},
{
name: 'quantity',
index: 'quantity',
width: 90,
sorttype: "float"},
{
name: 'category',
index: 'category',
width: 80,
sorttype: "String"}
],
caption: "Items List ",
// ondblClickRow: function(rowid,iRow,iCol,e){alert('double clicked');}
});
function YNFormatter(cellvalue, options, rowObject)
{
var color = (cellvalue >= 10) ? "green" : "red";
return '<span style="background-color:' + color + ';">' + cellvalue + '</span>';
}
$("#grid").jqGrid('setGridParam', {ondblClickRow: function(rowid,iRow,iCol,e){loadForm(rowid);}});
<script>
// Global variable to hold the record id currently being dealt with
item_id = 0;
// Function to load the edit form for a given id via AJAX
function loadForm(id) {
path = "/items/" + id + "/edit"
item_id = id;
$("#ItmDisplay").load(path);
}
// function to return the current record id
function get_item_id() {
return item_id;
}
$(document).delegate('form', 'submit', function(e) {
e.preventDefault();
var valuesToSubmit = $(this).serialize();
// Rails path to pass the update request to
updatePath = '/items/' + get_item_id();
$.ajax({
url: updatePath, //submits it to the given url of the form
type: "post",
data: valuesToSubmit,
dataType: "JSON"
}).success(function(json){ // the json variable holds the return from the server (JSON Object)
//act on result.
$("#ItmDisplay").text("Record Successfully Saved!!").fadeOut(3500);
var $myGrid = jQuery("#grid");
data = $myGrid.jqGrid('getGridParam', 'data');
index = $myGrid.jqGrid('getGridParam', '_index');
var rowId = json.id, itemIndex = index[rowId], rowItem = data[itemIndex];
console.log(rowItem);
rowItem.title = json.title;
rowItem.quantity = json.quantity;
rowItem.category = json.category;
console.log(rowItem);
$myGrid.jqGrid('setRowData',json.id,rowItem);
});
});
</script>!
如果有人知道我在做什么错,非常感谢您的帮助!谢谢.
If anyone knows what I am doing wrong, your help would be very gratefully appreciated! Thanks.
推荐答案
我看不到您在代码中使用YNFormatter
的位置.您应该为colModel
中的某些列指定formatter: YNFormatter
.
I don't see where you use YNFormatter
in your code. You should specify formatter: YNFormatter
for some column in colModel
.
以下是另一个问题:您使用背景颜色 CSS样式,但是还有其他CSS样式背景可以从父元素应用.要查看背景色,必须删除background-image
.因此,可以结合使用background-image: none
和background-color
来解决此问题.这是您描述的问题的主要原因.
Another problem is the following: you use background-color CSS style, but there are other CSS styles background which applied from the parent elements. To see the background color one have to remove background-image
. So one can fix the problem by usage background-image: none
in combination with background-color
. It's the main reason of the problem which you described.
如果不使用旧版本的jqGrid,则使用格式化程序设置背景颜色不是最佳选择.使用cellattr
更好(请参见我的原始建议将功能纳入jqGrid和答案).主要优点-您可以设置背景颜色,但仍使用formatter: "date"
或formatter: "float"
之类的预定义格式器.
The usage of formatter for setting of background color is not the best choice if you use no so old version of jqGrid. It's much better to use cellattr
(see my original suggestion to include the feature in jqGrid and the answer for example). The main advantage - you can set background color, but still use predefined formatter like formatter: "date"
or formatter: "float"
.
对您发布的代码的一些常见说明:
Some common remarks to the code which you posted:
- 请勿在URL中使用
http://localhost:3000
前缀.代替url:'http://localhost:3000/ItmList'
最好使用url:'/ItmList'
.它不仅更短,而且由于相同来源的限制而减少了出错的可能性Ajax. - 您应该始终在网格中添加 gridview:true 选项,以提高性能.
- 我建议始终以选择器形式
pager: '#gridPager'
使用pager
选项.如果使用pager: 'gridPager'
或pager: $('#gridPager')
形式,则jqGrid将必须对其进行规范化",并将选项更改为pager: '#gridPager'
. - 如果从服务器返回的数据包含纯数据而不是放置在网格单元中的HTML片段,则我建议使用
autoencode: true
选项.该选项可确保从服务器返回的所有文本都将正确显示,即使这些文本包含用于HTML标记的符号. - 属性
sorttype: "String"
是未知的(请参见文档).sorttype
属性的默认值为"text"
.如果需要使用基于文本的排序,最好不要指定任何sorttype
属性. - 您应从
colModel
项目中删除index
属性,这些项目的值应与name
属性的值相同.顺便说一句,如果您使用loadonce: true
,则index
的值必须等于colModel
中某些列的name
属性的值.那么,为什么要通过包含不需要的index
属性来增加代码呢?在我看来,colModel
的较短版本更适合阅读:
- don't use
http://localhost:3000
prefix in the URL. Instead ofurl:'http://localhost:3000/ItmList'
it's better to useurl:'/ItmList'
. It's not only shorter, but it's reduce possibility to make errors because of the same origin restriction of Ajax. - you should always add gridview:true option to your grids to improve performance.
- I recommend to use
pager
option always in the selector formpager: '#gridPager'
. If you use the formpager: 'gridPager'
orpager: $('#gridPager')
jqGrid will have to "normalize" it and to change the option topager: '#gridPager'
. - I recommend to use
autoencode: true
option if the data returned from the server contains pure data instead of HTML fragments placed in the grid's cells. The option make you sure that all texts returned from the sever will be correctly displayed even the texts contain symbols used for HTML markup. - the property
sorttype: "String"
is unknown (see the documentation). The default value ofsorttype
property is"text"
. It's better don't specify anysorttype
property if you need the usage of text based sorting. - you should remove
index
properties fromcolModel
items which value are the same as the value ofname
property. By the way if you useloadonce: true
the values ofindex
have to be equal to the value ofname
property of some columns fromcolModel
. So why to increase the code by including unneededindex
properties? The shorter version ofcolModel
seems to me better for reading:
colModel: [
{ name: 'id', width: 30, sorttype: "int"},
{ name: 'title', width: 90 },
{ name: 'quantity', width: 90, sorttype: "float" },
{ name: 'category', width: 80 }
]
这篇关于JQGrid自定义格式化程序不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!