原版本是这样的
http://www.jq22.com/jquery-info169
效果图,这个用来做编辑查看这样的没问题
我想要这样的,就对上面的改造了下,以下是改良版本的js和css
针对上面的使用,加了个自定显示隐藏列功能
代码如下
这个是和jqgrid一起用的
使用
$(function () {
//右键菜单
rightmenuclick("gridTable","TICCTEQ-MINEX");
});
//右键菜单
//jqGrid=gridTable
//stroage=TICCTEQ-MINEX
function rightmenuclick(jqGrid,stroage) {
//获取grid列
var columnArray = $("#" + jqGrid).jqGrid("getGridParam", "colModel");
//需要去掉第一个,那是系统自带的
var cfg = window.localStorage[stroage];
var items = [];
debugger;
if (cfg == undefined) {
//如果是空,则默认显示全部
for (var i = 1; i < columnArray.length; i++) {
var it = columnArray[i];
items.push({
label: it.label, value: it.name, checked: !it.hidden, action: function (source, data, val) {
if (val == true) {
$("#" + jqGrid).setGridParam().showCol(data);
} else {
$("#" + jqGrid).setGridParam().hideCol(data);
}
window.localStorage[stroage] = JSON.stringify(source);
}
});
}
} else {
//配置的
var arrys = JSON.parse(cfg);
for (var i = 0; i < arrys.length; i++) {
var it = arrys[i];
items.push({
label: it.label, value: it.value, checked: it.checked, action: function (source, data, val) {
if (val == true) {
$("#" + jqGrid).setGridParam().showCol(data);
} else {
$("#" + jqGrid).setGridParam().hideCol(data);
}
window.localStorage[stroage] = JSON.stringify(source);
}
});
}
for (var i = 0; i < items.length; i++) {
if (items[i].checked == true) {
$("#" + jqGrid).setGridParam().showCol(items[i].value);
} else {
$("#" + jqGrid).setGridParam().hideCol(items[i].value);
}
}
}
//右键菜单
$('.ui-jqgrid-htable').contextPopup({
title: '',
items: items
});
}
使用
item新格式
{ label: 'Some Item', value: '1', checked: true, action: function (data, val) { alert(data + val) } },
$(function () {
$('.ui-jqgrid-htable').contextPopup({
title: '',
items: [
{ label: 'Some Item', value: '1', checked: true, action: function (data, val) { alert(data + val) } },
{ label: 'Another Thing', value: '2', action: function (data, val) { alert(data + val) } },
{ label: 'Blah Blah', value: '3', action: function (data, val) { alert(data + val) } },
{ label: 'Sheep', value: '4', action: function (data) { alert(data) } },
{ label: 'Cheese', value: 'icons/bin-metal.png', action: function () { alert('clicked 5') } },
{ label: 'Bacon', value: 'icons/magnifier-zoom-actual-equal.png', action: function () { alert('clicked 6') } },
{ label: 'Onwards', value: 'icons/application-table.png', action: function () { alert('clicked 7') } },
{ label: 'Flutters', value: 'icons/cassette.png', action: function () { alert('clicked 8') } },
{ label: 'Sheep', value: '4', action: function (data) { alert(data) } },
{ label: 'Cheese', value: 'icons/bin-metal.png', action: function () { alert('clicked 5') } },
{ label: 'Bacon', value: 'icons/magnifier-zoom-actual-equal.png', action: function () { alert('clicked 6') } },
{ label: 'Sheep', value: '4', action: function (data) { alert(data) } },
{ label: 'Cheese', value: 'icons/bin-metal.png', action: function () { alert('clicked 5') } },
{ label: 'Bacon', value: 'icons/magnifier-zoom-actual-equal.png', action: function () { alert('clicked 6') } },
{ label: 'Sheep', value: '4', action: function (data) { alert(data) } },
{ label: 'Cheese', value: 'icons/bin-metal.png', action: function () { alert('clicked 5') } },
]
});
});
css
.contextMenuPlugin {
-webkit-user-select: none;
display: none;
font-family: tahoma, arial, sans-serif;
font-size: 11px;
position: absolute;
left: 100px;
top: 100px;
min-width: 100px;
list-style-type: none;
margin: 0;
padding: 0;
background-color: #f7f3f7;
border: 2px solid #f7f7f7;
outline: 1px solid #949694;
max-height:300px;
overflow-y:auto;
max-width:200px;
min-width:160px;
}
.contextMenuPlugin > li {
margin: 0 0 0 0;
padding: 1px;
background-repeat: no-repeat;
}
.contextMenuPlugin > li > a {
position: relative;
display: block;
padding: 3px 3px 3px 28px;
color: ButtonText;
text-decoration: none;
margin: 1px;
}
.contextMenuPlugin > li > a img,
.contextMenuPlugin > li > a input
{
position: absolute;
left: 3px;
margin-top: -2px;
width: 16px;
height: 16px;
}
.contextMenuPlugin > li > a:hover {
border: 1px solid #fffbff;
outline: 1px solid #b5d3ff;
margin: 0;
background: -moz-linear-gradient(top, rgba(239,239,255,0.5) 0%, rgba(223,223,255,0.5) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(239,239,255,0.5)), color-stop(100%,rgba(223,223,255,0.5))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(239,239,255,0.5) 0%,rgba(223,223,255,0.5) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(239,239,255,0.5) 0%,rgba(223,223,255,0.5) 100%); /* Opera11.10+ */
background: -ms-linear-gradient(top, rgba(239,239,255,0.5) 0%,rgba(223,223,255,0.5) 100%); /* IE10+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#80efefff', endColorstr='#80dfdfff',GradientType=0 ); /* IE6-9 */
background: linear-gradient(top, rgba(239,239,255,0.5) 0%,rgba(223,223,255,0.5) 100%); /* W3C */
cursor: default;
}
.contextMenuPlugin > li.divider {
border-top: 1px solid #e7e3e7;
border-bottom: 1px solid #ffffff;
height: 0;
padding: 0;
margin: 5px 0 5px 27px;
}
.contextMenuPlugin > .header {
background: rgb(90,90,90); /* Old browsers */
background: -moz-linear-gradient(top, rgba(90,90,90,1) 0%, rgba(20,20,20,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(90,90,90,1)), color-stop(100%,rgba(20,20,20,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(90,90,90,1) 0%,rgba(20,20,20,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(90,90,90,1) 0%,rgba(20,20,20,1) 100%); /* Opera11.10+ */
background: -ms-linear-gradient(top, rgba(90,90,90,1) 0%,rgba(20,20,20,1) 100%); /* IE10+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#5a5a5a', endColorstr='#141414',GradientType=0 ); /* IE6-9 */
background: linear-gradient(top, rgba(90,90,90,1) 0%,rgba(20,20,20,1) 100%); /* W3C */
position: relative;
cursor: default;
padding: 3px 3px 3px 3px;
color: #ffffff;
}
.contextMenuPlugin > .gutterLine {
position: absolute;
border-left: 1px solid #e7e3e7;
border-right: 1px solid #ffffff;
width: 0;
top: 0;
bottom: 0;
left: 26px;
z-index: 0;
}
js
/**
* jQuery plugin for Pretty looking right click context menu.
*
* Requires popup.js and popup.css to be included in your page. And jQuery, obviously.
*
* Usage:
*
* $('.something').contextPopup({
* title: 'Some title',
* items: [
* {label:'My Item', icon:'/some/icon1.png', action:function() { alert('hi'); }},
* {label:'Item #2', icon:'/some/icon2.png', action:function() { alert('yo'); }},
* null, // divider
* {label:'Blahhhh', icon:'/some/icon3.png', action:function() { alert('bye'); }},
* ]
* });
*
* Icon needs to be 16x16. I recommend the Fugue icon set from: http://p.yusukekamiyamane.com/
*
* - Joe Walnes, 2011 http://joewalnes.com/
* https://github.com/joewalnes/jquery-simple-context-menu
*
* MIT License: https://github.com/joewalnes/jquery-simple-context-menu/blob/master/LICENSE.txt
*/
jQuery.fn.contextPopup = function(menuData) {
// Define default settings
var settings = {
contextMenuClass: 'contextMenuPlugin',
gutterLineClass: 'gutterLine',
headerClass: 'header',
seperatorClass: 'divider',
title: '',
items: []
};
// merge them
$.extend(settings, menuData);
// Build popup menu HTML
function createMenu(e) {
var menu = $('<ul class="' + settings.contextMenuClass + '"><div class="' + settings.gutterLineClass + '"></div></ul>')
.appendTo(document.body);
if (settings.title) {
$('<li class="' + settings.headerClass + '"></li>').text(settings.title).appendTo(menu);
}
settings.items.forEach(function(item) {
if (item) {
var rowCode = '<li><a href="#"><span></span></a></li>';
// if(item.value)
// rowCode += '<img>';
// rowCode += '<span></span></a></li>';
var row = $(rowCode).appendTo(menu);
//if(item.value){
// var icon = $('<img>');
// icon.attr('src', item.value);
// icon.insertBefore(row.find('span'));
//}
if (item.value) {
var icon = $("<input type='checkbox'/>");
icon.attr('value', item.value);
icon.get(0).checked = item.checked;
icon.insertBefore(row.find('span'));
}
row.find('span').text(item.label);
if (item.action) {
row.find('a').click(function () {
$(this).find("input").get(0).checked = !$(this).find("input").get(0).checked;
item.checked = $(this).find("input").get(0).checked;
item.action(settings.items,$(this).find("input").get(0).value, $(this).find("input").get(0).checked);
});
row.find('input').click(function (event) {
item.checked = $(this).get(0).checked;
item.action(settings.items, $(this).get(0).value, $(this).get(0).checked);
event.stopPropagation();
});
}
} else {
$('<li class="' + settings.seperatorClass + '"></li>').appendTo(menu);
}
});
menu.find('.' + settings.headerClass ).text(settings.title);
return menu;
}
// On contextmenu event (right click)
this.bind('contextmenu', function(e) {
var menu = createMenu(e)
.show();
var left = e.pageX + 5, /* nudge to the right, so the pointer is covering the title */
top = e.pageY;
//if (top + menu.height() >= $(window).height()) {
// top -= menu.height();
//}
//if (left + menu.width() >= $(window).width()) {
// left -= menu.width();
//}
// Create and show menu
menu.css({zIndex:1000001, left:left, top:top})
.bind('contextmenu', function() { return false; });
// Cover rest of page with invisible div that when clicked will cancel the popup.
var bg = $('<div></div>')
.css({left:0, top:0, width:'100%', height:'100%', position:'absolute', zIndex:1000000})
.appendTo(document.body)
.bind('contextmenu click', function() {
// If click or right click anywhere else on page: remove clean up.
bg.remove();
menu.remove();
return false;
});
// When clicking on a link in menu: clean up (in addition to handlers on link already)
menu.find('a').click(function() {
//bg.remove();
//menu.remove();
});
// Cancel event, so real browser popup doesn't appear.
return false;
});
return this;
};