handsontableを使用しようとしましたが、コンテキストメニューにカスタムアイテムを追加したいと思います。
カスタムメニューを実装するためのチュートリアルはたくさんありますが、デフォルトの項目は無視されます。
そこで、すべてのアイテムのキーを追加しましたが、一部は機能しません。
私の設定は次のようになります。
var basicSettings = {
minRows: 1,
minCols: 1,
rowHeaders: false,
colHeaders: false,
hiddenColumns: true,
contextMenu: {
items: {
row_above: {},
row_below: {},
"hsep1": "---------",
col_left: {},
col_right: {},
"hsep2": "---------",
remove_row: {},
remove_col: {},
"hsep3": "---------",
undo: {},
redo: {},
"hsep4": "---------",
make_read_only: {},
"hsep5": "---------",
alignment: {},
"hsep6": "---------",
copy: {},
cut: {},
"paste": {
name: 'Paste',
callback: function (key, option) {
this.copyPaste.triggerPaste();
}
},
"hsep7": "---------",
mergeCells: {
name: "Merge"
},
"hsep8": "---------",
// Custom menu item to set color of cells
set_color: {
key: "color",
name: "Color",
"submenu": {
"items": [
{
key: "color:1",
"name": "Black",
callback: function(key, options) {
for (var i = options[0].start.row; i <= options[0].end.row; i ++){
for (var j = options[0].start.col; j <= options[0].end.col; j ++){
this.getCell(i, j).className = "color-black";
}
}
}
}, {
key: "color:2",
"name": "White",
callback: function(key, options) {
for (var i = options[0].start.row; i <= options[0].end.row; i ++){
for (var j = options[0].start.col; j <= options[0].end.col; j ++){
$(this.getCell(i, j)).removeClass("color-*").addClass("color-white");
}
}
this.render();
}
}, {
key: "color:3",
"name": "Red",
callback: function(key, options) {
for (var i = options[0].start.row; i <= options[0].end.row; i ++){
for (var j = options[0].start.col; j <= options[0].end.col; j ++){
this.getCell(i, j).style.backgroundColor = "red";
}
}
this.render();
}
}
]
}
}
}
},
manualRowResize: true,
manualColumnResize: true,
contextMenuCopyPaste: {
swfPath: '/bower_components/zeroclipboard/dist/ZeroClipboard.swf'
},
copyPaste: true,
mergeCells: true,
search: true,
stretchH: 'all',
autoColumnSize: {useHeaders: true},
autoRowSize: {syncLimit: 300},
width: 1000,
height: window.innerHeight - 100,
licenseKey: "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx"
};
メニューはこんな感じ。
すべてのデフォルトメニュー項目でカスタム項目を追加する方法はありますか?もしそうなら、私は質問3と質問4への回答は必要ありません。
この質問をさせてくれる最も重要な部分は、カスタムメニュー、つまり「set_color」です。「黒」または「赤」をクリックするとその色になりますが、セルの値を変更すると元に戻ります。セルが背景色を元に戻すのを防ぐにはどうすればよいですか?
すべての機能を有効にする以外に、追加のカスタムアイテムが必要です。しかし、「マージ」アイテムの適切なキーが見つかりません。現在の「マージ」機能はとても奇妙に機能します。関数を正しく機能させる方法は?
https://github.com/handsontable/handsontable/issues/2853を使用して貼り付け機能を実装しようとしましたが、このエラーが表示されます。
Uncaught TypeError: Cannot read property 'triggerPaste' of undefined
それらの実用的な使用法で私を助けてください。前もって感謝します。
すべてのデフォルトメニュー項目でカスタム項目を追加する方法はありますか?もしそうなら、私は質問3と質問4への回答は必要ありません。
Handsontable
とcontextMenu
のセットtrue
。例: let
example3 = document.getElementById('example3'),
settings3,
hot3;
settings3 = {
data: [...],
rowHeaders: true,
colHeaders: true,
contextMenu: true // set to `true`..
};
hot3 = new Handsontable(example3, settings3);
contextMenu
ように設定を更新します。 let cm = hot3.getPlugin('ContextMenu');
hot3.updateSettings({
contextMenu: {
// Clone the pre-defined items and add your custom items.
items: Object.assign({}, cm.itemsFactory.predefinedItems, {
'hsep1': '---------',
'set_color': {
key: 'color',
name: 'Color',
submenu: {
items: [{
key: 'color:red',
name: 'Red',
callback: setCellColor
}, {
key: 'color:blue',
name: 'Blue',
callback: setCellColor
}]
}
}
})
}
});
この質問をさせてくれる最も重要な部分は、カスタムメニュー、つまり「set_color」です。「黒」または「赤」をクリックするとその色になりますが、セルの値を変更すると元に戻ります。セルが背景色を元に戻すのを防ぐにはどうすればよいですか?
それを防ぐ方法がわかりません。ただし、適切なセルの色(またはその他のカスタム/メタデータ..)を復元する1つの方法があります。
// This is my callback for the 'set_color' context menu items.
// Sample `key`: 'color:red'
function setCellColor(key, opt) {
let color = key.substring(6);
for (let i = opt[0].start.row; i <= opt[0].end.row; i++) {
for (let j = opt[0].start.col; j <= opt[0].end.col; j++) {
this.getCell(i, j).style.color = color;
this.setCellMeta(i, j, 'color', color); // Save the color
}
}
}
// Listen to the `beforeRenderer` event, and restore the cell's color
// before the "renderer" starting rendering the cell
Handsontable.hooks.add('beforeRenderer', function(td, r, c, p, pv, cp){
if (cp.color) {
td.style.color = cp.color;
}
}, hot3);
ここで完全な例を試してください:http://jsfiddle.net/y9j3m29c/1/、これはhttps://docs.handsontable.com/3.0.0/demo-context-menu.html#page-customに基づいています
SheetClip()
: <script src="https://unpkg.com/sheetclip"></script>
let clipboardCache = '';
const sheetclip = new SheetClip();
settings3 = {
...
afterCopy: function(changes){
clipboardCache = sheetclip.stringify(changes);
},
afterCut: function(changes){
clipboardCache = sheetclip.stringify(changes);
},
afterPaste: function(changes){
clipboardCache = sheetclip.stringify(changes);
}
};
let cm = hot3.getPlugin('ContextMenu');
hot3.updateSettings({
contextMenu: {
// Clone the pre-defined items and add your custom items.
items: Object.assign({}, cm.itemsFactory.predefinedItems, {
...
'paste': {
name: 'Paste',
disabled: function(){
return clipboardCache.length === 0;
},
callback: function(){
var plugin = this.getPlugin('copyPaste');
this.listen();
plugin.paste(clipboardCache);
}
}
})
}
});
https://docs.handsontable.com/3.0.0/demo-copy-paste.html#paste-in-context-menuの詳細とhttp://jsfiddle.net/y9j3m29c/2/のデモ—またはhttp ://jsfiddle.net/y9j3m29c/4/「クリップボードのクリア」機能が付属しています。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加