2010-09-23 47 views
0

我有以下問題。這是我的index.php:用JQuery重新載入表格

<div id="tabs"> 
    <ul> 
     <li><a href="#tabs-1">A projects</a></li> 
     <li><a href="#tabs-2">B projects</a></li> 
    </ul> 
    <div id="tabs-1"> 
     <?php echo loadTabs(1);?> 
    </div> 
    <div id="tabs-2"> 
     <?php echo loadTabs(2);?> 
    </div> 

這裏是我的兩個PHP函數建立的表:

function loadTabs($settingId){ 
$query = 'select * from deployments_settings where id="'.$settingId.'"'; 
$result = mysql_query($query); 
$html = '<div id="tab'.$settingId.'_container">'; 
    while ($row = mysql_fetch_array($result)) { 
     $html .= '<div id="tab'.$settingId.'_buttons">'; 
     $html .= '<button id="add_project">'.$row['addbuttoncaption'].'</button>'; 
     $html .= '</div>';// id="tab[1..2]_button_add" 
     $html .= '<div id="tab'.$settingId.'_content">'; 
     $html .= '<br/>'; 
     $html .= loadTables($settingId, $row['deploymentstable']); 
     $html .= '</div>';//id=tab[1..2]_[first..second]content 
    }//while   
$html .= '</div>';// id="tab[1..2]_container" 
return $html;} 

function loadTables($tableId, $tableName) { 
$query = 'select * from '.$tableName.' order by `id` desc'; 
$result = mysql_query($query); 
$html = '<table id="tab'.$tableId.'_table" class="tabTable">'; 
$html .= '<thead><tr>'; 
$html .= '<th>#</th>'; 
$html .= '<th>Project number</th>'; 
$html .= '<th>Deadline</th>'; 
$html .= '<th>Assign to</th>'; 
$html .= '<th>Description</th>'; 
$html .= '<th>Action</th>'; 
$html .= '</tr></thead>'; 
$html .= '<tbody>'; 
    $countRow = 0; 
    while ($row = mysql_fetch_array($result)) { 
     $html .='<tr>'; 
     $html .= '<td class="colID">'.++$countRow.'</td><td class="colPN">'.$row['name'].'</td><td class="colDL">'.$row['deadline'].'</td><td class="colAT">'.$row['assignto'].'</td><td>'.$row['description'].'</td>'; 
     $html .= '<td class="colACT">'; 
     $html .= '<button id="edit_action">Edit</button>'; 
     $html .= '<button title="Remove" contentReload="'.$tableId.'" rrTable="'.$tableName.'" rrID="'.$row['id'].'" id="delete_action">Delete</button>'; 
     $html .= '</td>'; 
     $html .= '</tr>'; 
     }//while 
$html .= '</tbody>'; 
$html .= '</table>'; 
return $html;} 

這裏是我的JQuery的一部分。按鈕刪除。

$("button#delete_action") 
    .button({icons: {primary: "ui-icon-circle-minus"}, text: false}) 
    .click(function() { 
     var contentReload = $(this).attr("contentReload"); 
     //alert(contentReload); 
     if(confirm("Press OK to confirm DELETE operation")) { 
      $.post("coreDB.php", { 
        action: 3, 
        rrTable: $(this).attr("rrTable"), 
        rrID: $(this).attr("rrID"), 
        contentReload: $(this).attr("contentReload") 
       }, 
       function(data, textStatus, XMLHttpRequest) { 
        //alert(data); 
        $("#tab"+ contentReload + "_table").html(data); 
       }); //post 
     } //if then condition 
}); 

的問題是這樣的 - >當「刪除鍵」成功運行,它給回表的輸出,我重新申請表對象$("#tab"+ contentReload + "_table").html(data);。但不幸的是,它不會重新載入表格,JQuery中的所有好東西都不起作用。

是什麼我需要在jQuery端工作,以重新加載表。

預先感謝您!

+0

這是IE中的問題嗎?前段時間有一篇文章談到一些事件沒有在IE的表格元素上觸發。 – 2010-09-23 13:46:21

回答

1

我還沒有真正閱讀過上面的代碼,因爲它很難閱讀,但如果你想從表中刪除行,我會將AJAX調用發送到你的刪除腳本。然後,如果AJAX調用返回true,只需使用JavaScript從表中刪除該行。這意味着您的頁面不必刷新。

+0

謝謝Martin。你能指點一些例子嗎? – Askar 2010-09-23 23:39:44

+0

我給你的邏輯。恐怕你得編碼。 – 2010-09-24 08:25:08

+0

謝謝Martin。 – Askar 2010-09-24 10:30:50

1

這個問題不是很清楚,你是什麼意思,「它不重新加載表」?

重新加載後,按鈕不會起作用:單擊只能將事件附加到頁面中的元素。 由於你刪除,然後創建新的按鈕,沒有點擊他們....

您需要重新應用上面的這些新按鈕上的$(「button#delete_action」)。按鈕...)或你可以使用實況(): http://api.jquery.com/live/

在您的例子:

<script type="text/javascript"> 
function makeButtons() 
{ 
    $("button#delete_action") 
     .button({icons: {primary: "ui-icon-circle-minus"}, text: false}); 
} 
$(document).ready(function() 
{ 
    $("button#delete_action").live('click',function() { 
    var contentReload = $(this).attr("contentReload"); 
    //alert(contentReload); 
    if(confirm("Press OK to confirm DELETE operation")) { 
     $.post("coreDB.php", { 
       action: 3, 
       rrTable: $(this).attr("rrTable"), 
       rrID: $(this).attr("rrID"), 
       contentReload: $(this).attr("contentReload") 
      }, 
      function(data, textStatus, XMLHttpRequest) { 
       //alert(data); 
       $("#tab"+ contentReload + "_table").html(data); 
       makeButtons(); 
      }); //post 
    } //if then condition 

}); }

每次都爲活動但重繪接口分配點擊一次。只需將makeButtons應用於「#tab」+ contentReload +「_table」中的按鈕,即可通過將此選擇器作爲參數傳遞給makeButtons來優化它。

+0

Loic,感謝您指向.live方法,這正是我所需要的。一個問題似乎不起作用,如果我這樣做:$(「button#delete_action」)。button(whatever).live(「click」,function(){..... $(「#tab1_content」) .html(data);})。 .live()方法是否必須在選擇器之後直接調用? – Askar 2010-09-23 22:06:29

+0

是的。將代碼放在代碼中,一次加載一次。在每次重新加載時,將.button()應用於您的按鈕。爲什麼不每次都點擊點擊?你的一些按鈕還沒有被刪除,這會給他們附加第二個點擊事件:一個點擊事件會產生兩個對$ .POST ....的調用,這會很糟糕。 – 2010-09-23 22:38:21

+0

對不起,我是webdev(我的第一個應用程序)的新手。這是我從消息 - > 1中回顧一下「爲所有人加載一次」 - 我的div容器像這樣 - > WRAPS 。所以在$(document)之後。ready(function(){$(「tab [1,2] _container」)。live(「this is should empty?」,function(){});});. 2.「爲什麼不每次點擊點擊?」 - 我沒有明白。當我點擊時,我得到了TAB中整個內容的輸出。我究竟需要做什麼? – Askar 2010-09-23 23:36:22