我理解這個問題非常好。我同意predefined formatter哪一個目前可以使用('showlink'和'link'格式化程序)不夠靈活。
我可以建議你另一個格式化程序,你可以下載here。格式化器的使用是非常簡單:
{label: "AddToCart", name: "Addtocrt_addtocrt", formatter: "dynamicLink",
formatoptions: {
url: function (cellValue, rowId, rowData) {
return '/Store/AddToCart' + rowId + '?' +
$.param({
quantity: rowData.Stocks_valkogus
});
}
}
}
定義爲函數的url
將在<a>
用作href
屬性的值。
此外到url
formatoptions
的「dynamicLink」格式化器支持target
選項(使用的相同的含義由「showlink」),cellValue
其也可以起到和onClick
回調與rowId
,iRow
,iCol
,cellValue
,e
作爲參數。如果onClick
回調被定義,那麼url
的值將被忽略。所以可以跳過格式化程序選項url
的定義。
The demo證明 'dynamicLink' 格式化的用法:
![enter image description here](https://i.stack.imgur.com/q43Je.png)
的formatter: 'dynamicLink'
的當前代碼,你可以在下面找到:
/*global jQuery */
(function ($) {
'use strict';
/*jslint unparam: true */
$.extend($.fn.fmatter, {
dynamicLink: function (cellValue, options, rowData) {
// href, target, rel, title, onclick
// other attributes like media, hreflang, type are not supported currently
var op = {url: '#'};
if (typeof options.colModel.formatoptions !== 'undefined') {
op = $.extend({}, op, options.colModel.formatoptions);
}
if ($.isFunction(op.target)) {
op.target = op.target.call(this, cellValue, options.rowId, rowData, options);
}
if ($.isFunction(op.url)) {
op.url = op.url.call(this, cellValue, options.rowId, rowData, options);
}
if ($.isFunction(op.cellValue)) {
cellValue = op.cellValue.call(this, cellValue, options.rowId, rowData, options);
}
if ($.fmatter.isString(cellValue) || $.fmatter.isNumber(cellValue)) {
return '<a' +
(op.target ? ' target=' + op.target : '') +
(op.onClick ? ' onclick="return $.fn.fmatter.dynamicLink.onClick.call(this, arguments[0]);"' : '') +
' href="' + op.url + '">' +
(cellValue || ' ') + '</a>';
} else {
return ' ';
}
}
});
$.extend($.fn.fmatter.dynamicLink, {
unformat: function (cellValue, options, elem) {
var text = $(elem).text();
return text === ' ' ? '' : text;
},
onClick: function (e) {
var $cell = $(this).closest('td'),
$row = $cell.closest('tr.jqgrow'),
$grid = $row.closest('table.ui-jqgrid-btable'),
p,
colModel,
iCol;
if ($grid.length === 1) {
p = $grid[0].p;
if (p) {
iCol = $.jgrid.getCellIndex($cell[0]);
colModel = p.colModel;
colModel[iCol].formatoptions.onClick.call($grid[0],
$row.attr('id'), $row[0].rowIndex, iCol, $cell.text(), e);
}
}
return false;
}
});
}(jQuery));
我打算放置的代碼格式化程序和一些其他插件到github上的jqGrid。
更新:Free jqGrid延伸formatter: "showlink"
選項(請參見the wiki article和the answer)。所以在使用免費jqGrid的情況下,不需要使用formatter: "dynamicLink"
。
謝謝。我嘗試過,但編輯開始之前的數量被返回。我更新了問題。 – Andrus 2012-01-29 11:10:52
@Andrus:如果您使用具有'repeatitems:true'的'jsonReader',則應該'rowData [iCol]'而不是'rowData.Stocks_valkogus'。格式化程序只是將'rowData'參數轉發給'url'函數,並使用與數據格式相對應的符號。你另外寫了關於*編輯*數據的問題,但我不明白你有什麼問題。你能更準確地描述現存的問題嗎? – Oleg 2012-01-29 12:59:40
用戶點擊數量欄。這開始內聯編輯。用戶輸入所需數量,然後點擊添加到使用dynamicformatter創建的購物車鏈接。在這種情況下,dynamicformatter應該將輸入的數量傳遞給url。也許dynamicformatter創建代碼應該檢索包含用戶輸入數量的input元素的內容。還是保存編輯過的行並在保存後檢索輸入的數量更好?保存不應該將數據發佈到服務器。 – Andrus 2012-01-29 13:13:46