我有一个带有一些子表的 Javascript Jtable。我想做的是当第二次单击行标题时关闭子表。
所以我设置了一些全局变量:
var NotesOpen = false;
var HistoryOpen = false;
var ElementsOpen = false;
因此,在 f.ex 上,Notes 子表中的单击函数如下所示:
$img.click(function () {
if (NotesOpen == true) {
console.log($(this).closest('tr').next('tr').find(':button').html());
$(this).closest('tr').next('tr').find(':button').click();
$(this).closest('tr').next('tr').find(':button').trigger('click');
$(this).closest('tr').next('tr').find('.jtable-close-button').click();
}
NotesOpen = true;
我可以看到调试线。它找到了正确的行,因为它说的是 <span>Close</span>
在控制台窗口中。如果我只是检查 $(this).closest('tr').next('tr').html()
我可以看到它位于正确的行。
但是,当我尝试触发点击事件时,我只收到错误:Uncaught Error: cannot call methods on jtable prior to initialization; attempted to call method 'destroy' jquery-2.1.1.js:250
最佳答案
此代码有助于通过单击图像图标来打开或关闭子表。
//CHILD TABLE DEFINITION FOR "PHONE NUMBERS"
Phones: {
title: '',
width: '5%',
sorting: false,
edit: false,
create: false,
display: function (studentData) {
var $img = $('<img src="/Content/images/Misc/phone.png" title="Edit phone numbers" />'), //Create an image that will be used to open child table
parentTable = $("#StudentTableContainer");
//Open | Close child table when user clicks the image
$img.click(function(){
var tr = $(this).parents("tr"),
isChildRowOpen = parentTable.jtable("isChildRowOpen", tr );
if( isChildRowOpen ){
$( parentTable.jtable("getChildRow", tr ) ).slideUp();
return;
}
// some another code
}
}
}
关于Javascript Jtable 单击两次时关闭子表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25587121/