내 새로운 demo이 작업을 수행하는 방법을 보여줍니다 데모에서
내가있는 jqGrid의 플러그인 디렉토리에 포함 된
jquery.contextmenu.js
의
small modification 사용합니다. 내 코드는 특히 CSS 클래스를 사용하고
menuStyle
및
itemHoverStyle
의
contextMenu
에 사용되는 색상을 얻는 데있어 완벽합니다. 그럼에도 불구하고 코드는 우리에게 필요한 것입니다.
데모의 주요 부분은 네비게이터 바를 빌드 한 후 호출 할 수있는 createContexMenuFromNavigatorButtons
기능 (navGrid
및 navButtonAdd
이후)에서 구성됩니다. 사용법은 매우 간단합니다 :
는
createContexMenuFromNavigatorButtons($("#list"), '#pager');
createContexMenuFromNavigatorButtons
의 코드는 아래 찾을 :
function createContexMenuFromNavigatorButtons(grid, pager) {
var buttons = $('table.navtable .ui-pg-button', pager),
myBinding = {},
menuId = 'menu_' + grid[0].id,
menuDiv = $('<div>').attr('id', menuId).hide(),
menuUl = $('<ul>');
menuUl.appendTo(menuDiv);
buttons.each(function() {
var $div = $(this).children('div.ui-pg-div:first'), $spanIcon, text, $td, id, $li, gridId = grid[0].id;
if ($div.length === 1) {
text = $div.text();
$td = $div.parent();
if (text === '') {
text = $td.attr('title');
}
if (this.id !== '' && text !== '') {
id = 'menuitem_' + this.id;
if (id.length > gridId.length + 2) {
id = id.substr(0, id.length - gridId.length - 1);
}
} else {
// for custom buttons
id = $.jgrid.randId();
}
$li = $('<li>').attr('id', id);
$spanIcon = $div.children('span.ui-icon');
$li.append($spanIcon.clone().css("float", "left"));
$li.append($('<span>').css({
'font-size': '11px',
'font-family': 'Verdana',
'margin-left': '0.5em'
}).text(text));
menuUl.append($li);
myBinding[id] = (function ($button) {
return function() { $button.click(); };
}($div));
}
});
menuDiv.appendTo('body');
grid.contextMenu(menuId, {
bindings: myBinding,
onContextMenu: function (e) {
var rowId = $(e.target).closest("tr.jqgrow").attr("id"), p = grid[0].p, i, lastSelId;
if (rowId) {
i = $.inArray(rowId, p.selarrrow);
if (p.selrow !== rowId && i < 0) {
// prevent the row from be unselected
// the implementation is for "multiselect:false" which we use,
// but one can easy modify the code for "multiselect:true"
grid.jqGrid('setSelection', rowId);
} else if (p.multiselect) {
// Edit will edit FIRST selected row.
// rowId is allready selected, but can be not the last selected.
// Se we swap rowId with the first element of the array p.selarrrow
lastSelId = p.selarrrow[p.selarrrow.length - 1];
if (i !== p.selarrrow.length - 1) {
p.selarrrow[p.selarrrow.length - 1] = rowId;
p.selarrrow[i] = lastSelId;
p.selrow = rowId;
}
}
return true;
} else {
return false; // no contex menu
}
},
menuStyle: {
backgroundColor: '#fcfdfd',
border: '1px solid #a6c9e2',
maxWidth: '600px',
width: '100%'
},
itemHoverStyle: {
border: '1px solid #79b7e7',
color: '#1d5987',
backgroundColor: '#d0e5f5'
}
});
}
한 매우 흥미로운 질문을. 나는 이것을 어떻게 더 잘 구현할 수 있는지 살펴볼 것이다. – Oleg
대단히 고마워요. 관련 질문은 http://stackoverflow.com/questions/8457495/how-to-add-standard-textbox-command-to-jqgrid-context-menu입니다. 어쩌면 표준 편집 옵션을 메뉴 (jqgrid 상단 도구 모음에?)에 추가하거나 텍스트 상자의 인라인 편집시 브라우저 기본 메뉴를 사용할 수도 있습니다. – Andrus