2017-02-05 24 views
0

我有一個JQuery的jtable有一個子表。據我所知,它是根據jtable演示中的示例設置的。主表=(聯繫人)和子表(類別)顯示沒有任何問題。我的問題是類別子表上的刪除操作沒有發佈行鍵值(categoryID),因爲我期望它,我不明白爲什麼不。主表上的類似動作發佈就好了。請注意下面代碼中的兩個console.log行輸出postData變量,第一行報告聯繫人錶行(ID)的ID,但第二行打印空數組而不是CategoryID。任何幫助讚賞。jtable子表不是不能發佈密鑰值

感謝

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'); 

回答

0

OK,我解決了這個問題,對不起,打擾任何人誰可能花費的時間在看這個。問題是我的子表變量名稱錯了,它們應該是category_ID和Contact_ID

相關問題