单击两次时,Javascript Jtable关闭子表

Javascript Jtable Close Child Table when clicked twice

本文关键字:Jtable Javascript 两次 单击      更新时间:2023-09-26

我有一个带有一些子表的Javascript Jtable。我想做的是在第二次单击行标题时关闭子表。

所以我设置了一些全局变量:

var NotesOpen = false;
var HistoryOpen = false;
var ElementsOpen = false;

因此,在f.ex Notes子表i上,单击函数如下所示:

$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
        }
    }
}