jtable子表没有POST键值

时间:2017-02-05 23:13:02

标签: jquery-jtable

我有一个有子表的Jquery jtable。据我所知,它是根据jtable演示中的示例设置的。主表=(联系人)和子表(类别)显示没有任何问题。我的问题是类别子表上的删除操作没有像我期望的那样发布行键值(categoryID),我不明白为什么不这样做。主表上的类似操作发布就好了。请注意下面代码中输出postData变量的两个console.log行,第一行报告联系表行(ID)的ID,但第二行打印一个空数组而不是CategoryID。任何帮助表示赞赏。

由于

function ReturnAjax(theurl, postdata, errorfn) {
    return $.ajax({
        url: theurl,
        type: 'POST',
        dataType: 'json',
        data: postdata,
        cache: false,
        error: errorfn
    });             
}

    $('#ContactsTableContainer').jtable({
    title: 'Contacts',
    paging: true,
    pageSize: 30,
    sorting: true,
    defaultSorting: 'LastName ASC',
    selecting: true,
    selectOnRowClick: true,
    openChildAsAccordion: true,
    deleteConfirmation: false,
    actions: {
        listAction: function(postData, jtParams) {
            console.log("ContactsTableContainer - Loading list from custom function...");
            return $.Deferred(function($dfd) {
                $.ajax({
                    url: 'ContactsData.php?action=list&jtStartIndex=' + jtParams.jtStartIndex + '&jtPageSize=' + jtParams.jtPageSize + '&jtSorting=' + jtParams.jtSorting,
                    type: 'POST',
                    dataType: 'json',
                    data: postData,
                    success: function(data) {
                        if(data['RowIDs']) { RowIDs = data['RowIDs'].toString().split(','); }
                        $dfd.resolve(data);
                    },
                    error: MyError
                });
            });
        },
        deleteAction: function(postData) {
            console.log('deleting from contacts - custom function..., '+JSON.stringify(postData));
            $.when(
                ReturnAjax(
                    'ContactsData.php?action=list&ContactID='+postData['ID'],
                    postData,
                    MyError
                )
            ).then(
                function(data) {
                    if (data.Result != 'OK') { alert(data.Message); }
                    var msg = '';
                    var len = data.Records.length;
                    if(len>0) {
                        msg = '\t'+data.Records[0].Category;
                        for(var i=1 ; i<len ; i++) { msg += '\n\t'+data.Records[i].Category; }
                        msg = 'Contact is in the following categories\n'+msg;
                    }
                    msg += '\n\nConfirm deletion of this contact';
                    if(confirm(msg)) {
                        $.when(
                            ReturnAjax(
                                'ContactsData.php?action=delete',
                                postData,
                                MyError
                            )
                        ).done(
                            $('#ContactsTableContainer').jtable('reload')
                        );
                    } else {
                        $('#ContactsTableContainer').jtable('reload'); // Had to put this here to ensure that same delete button could be used again
                    }       
                }
            ).fail( function() { console.log('ajax call went wrong'); } );
        }, // end of delete action
    }, // end of actions
    fields: {
        ID: {
            key: true,
            create: false,
            edit: false,
            list: false,
            visibility: 'hidden'
        },
        Categories: {
            title: '',
            width: '5%',
            sorting: false,
            create: false,
            display: function(contact) {
                var $img = $('<img src="Images/layers.png" title="Show contact\'s categories" />');
                //Open child table when user clicks the image
                $img.click(function() {
                    console.log('display function (contact)..., '+JSON.stringify(contact));
                    $('#ContactsTableContainer').jtable(
                        'openChildTable',
                        $img.closest('tr'), //Parent row
                        {
                            title: contact.record.Name + ' - Categories',
                            selecting: true,
                            selectOnRowClick: true,
                            actions: {
                                listAction: 'ContactsData.php?action=list&ContactID=' + contact.record.ID,
                                deleteAction: function(postData) {
                                    console.log('deleting from custom category function..., '+JSON.stringify(postData));
                                    $.when(
                                        ReturnAjax(
                                            'ContactsData.php?action=deleteAssignment&ContactID=' + contact.record.ID,
                                            postData,
                                            MyError
                                        )
                                    ).done(
                                        $('#ContactsTableContainer').jtable('reload')
                                    );
                                }
                            },
                            fields: {
                                CategoryID: { key: true, create: false, edit: false, list: false, visibility: 'hidden' },
                                ContactID: { type: 'hidden', defaultValue: contact.record.ID },
                                Category: { title: 'Category' }
                            }
                        },
                        function(data) { data.childTable.jtable('load'); }
                    );
                });
                //Return image to show on the person row
                return $img;
            }
        },
        FirstName: {
            title: 'Forename',
            width: '25%',
        },
        LastName: {
            title: 'Surname',
            width: '25%',
        },
        HomePhone: {
            title: 'Phone',
            width: '15%',
            sorting: false,
        },
        Mobile: {
            title: 'Mobile',
            width: '15%',
            sorting: false,
        },
        Email: {
            title: 'Email',
            width: '20%',
            sorting: false,
        },
        Name: {
            type: 'hidden'
        },
    }
});

//Load list from server
$('#ContactsTableContainer').jtable('load');

1 个答案:

答案 0 :(得分:0)

好的,我解决了,很抱歉打扰任何可能花时间看这个的人。问题是我的子表变量名称错误,应该是category_ID和Contact_ID