Я пытаюсь использовать собственный форматировщик для jqgrid в моем приложении Ruby on Rails, но у меня есть ужасная трудность в его ответе.Нестандартный форматировщик JQGrid не работает
Вот функция, я использую:
function YNFormatter(cellvalue, options, rowObject)
{
var color = (cellvalue >= 10) ? "green" : "red";
return '<span style="background-color:' + color + ';">' + cellvalue + '</span>';
}
Однако моя сетка по-прежнему отображается, но без какого-либо форматирования происходящего.
Кроме того, чтобы дать контекст, ниже остальной код jqgrid для моего index.html.erb с вышеуказанной функции включены:
<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>!
Если кто-то знает, что я делаю неправильно, ваша помощь будет очень благодарна! Благодарю.
ПОСТАНОВИЛИ: Эй все, я получил эту работу прямо сейчас! Вышел на этот подход в StackOverflow, который я видел ранее во время поиска на сайте: http://stackoverflow.com/questions/10653194/jqgrid-set-a-color-conditional-custom-formatter-based-on-row- Значения Спасибо всем, кто посмотрел на выше сказанное, но очень ценю, и я надеюсь, что это решение может помочь другим тоже :) – NamingError