Как удалить строки с локальными данными в jqgrid
jqGrid параметр loadonce: true используется
Выбор строк и нажатие кнопки удаления
URL не указан
Как удалить строки только в локальных данных и подавить это сообщение об ошибке? Можно ли установить фиктивный URL или любую другую идею, чтобы разрешить удаление строки? Было бы хорошо, если бы формы добавления и редактирования могли использоваться также с локальными данными.
url: 'GetData',
datatype: "json",
multiselect: true,
multiboxonly: true,
scrollingRows : true,
autoencode: true,
loadonce: true,
prmNames: {id:"_rowid", oper: "_oper" },
rowTotal: 999999999,
rownumbers: true,
rowNum: 999999999,
Обновление 1
Из ответа Олега я понял следующее решение:
- Отключить стандартную кнопку удаления jqGrid
- Добавить новую кнопку удаления на панель инструментов.
С помощью этой кнопки нажмите событие вызова при условии
grid.jqGrid('delGridRow', rowid, myDelOptions);
метод. Можно выбрать несколько строк. Как удалить все выбранные строки, этот образец удаляет только одну?
Не лучше ли изменить jqGrid, чтобы кнопки удаления, редактирования, добавления работали без URL? В настоящее время требуется передать фиктивный URL, который всегда возвращает успех для локального редактирования данных.
1 ответ
Вы можете использовать метод delRowData, чтобы удалить любую локальную строку.
Вы можете использовать delGridRow из редактирования формы, если вам это нужно. Я описал путь здесь и использовал для форматера: "действия" (см. Здесь, здесь и первоначально здесь).
var grid = $("#list"),
myDelOptions = {
// because I use "local" data I don't want to send the changes
// to the server so I use "processing:true" setting and delete
// the row manually in onclickSubmit
onclickSubmit: function(options, rowid) {
var grid_id = $.jgrid.jqID(grid[0].id),
grid_p = grid[0].p,
newPage = grid_p.page;
// reset the value of processing option which could be modified
options.processing = true;
// delete the row
grid.delRowData(rowid);
$.jgrid.hideModal("#delmod"+grid_id,
{gb:"#gbox_"+grid_id,
jqm:options.jqModal,onClose:options.onClose});
if (grid_p.lastpage > 1) {// on the multipage grid reload the grid
if (grid_p.reccount === 0 && newPage === grid_p.lastpage) {
// if after deliting there are no rows on the current page
// which is the last page of the grid
newPage--; // go to the previous page
}
// reload grid to make the row from the next page visable.
grid.trigger("reloadGrid", [{page:newPage}]);
}
return true;
},
processing:true
};
а затем использовать
grid.jqGrid('delGridRow', rowid, myDelOptions);
ОБНОВЛЕНО: в случае multiselect: true
myDelOptions
может быть изменено на следующее:
var grid = $("#list"),
myDelOptions = {
// because I use "local" data I don't want to send the changes
// to the server so I use "processing:true" setting and delete
// the row manually in onclickSubmit
onclickSubmit: function(options) {
var grid_id = $.jgrid.jqID(grid[0].id),
grid_p = grid[0].p,
newPage = grid_p.page,
rowids = grid_p.multiselect? grid_p.selarrrow: [grid_p.selrow];
// reset the value of processing option which could be modified
options.processing = true;
// delete the row
$.each(rowids,function(){
grid.delRowData(this);
});
$.jgrid.hideModal("#delmod"+grid_id,
{gb:"#gbox_"+grid_id,
jqm:options.jqModal,onClose:options.onClose});
if (grid_p.lastpage > 1) {// on the multipage grid reload the grid
if (grid_p.reccount === 0 && newPage === grid_p.lastpage) {
// if after deliting there are no rows on the current page
// which is the last page of the grid
newPage--; // go to the previous page
}
// reload grid to make the row from the next page visable.
grid.trigger("reloadGrid", [{page:newPage}]);
}
return true;
},
processing:true
};
ОБНОВЛЕНО 2: Чтобы иметь поддержку клавиатуры для операции удаления и установить кнопку "Удалить" по умолчанию, вы можете добавить в delSettings
дополнительная опция
afterShowForm: function($form) {
var form = $form.parent()[0];
// Delete button: "#dData", Cancel button: "#eData"
$("#dData",form).attr("tabindex","1000");
$("#eData",form).attr("tabindex","1001");
setTimeout(function() {
$("#dData",form).focus(); // set the focus on "Delete" button
},50);
}