2011-05-21 169 views
0

您好我正在jQuery中動態創建一個表。我在這裏做的還是添加「可拖動的ui-widget」類,但它不影響/工作在我的桌子上。添加類不工作

var tab_title = $tab_title_input.val() || 'Tab '+tab_counter; 
     //alert(tab_title); 
     $tabs.tabs('add', '#tabs-'+tab_counter, tab_title);     

     var newTableDiv = $("<div />",{id: 'dialog'+tab_counter});   
     newTableDiv.appendTo("body");   
     alert("div appended to body"+" "+'dialog'+tab_counter); 
     //var newTable = $('<table id="myTable'+tab_counter+'" class="draggable" ></table>'); 
********************Class which i am adding in table but not working*********** 
     var newTable = $("<table />",{"class": "draggable ui-widget",id: 'myTable'+tab_counter,width: '100%',border: '0',cellspacing: '1px',cellpadding: '2px'});      
     newTable.appendTo('#dialog'+tab_counter);   
     alert('myTable'+tab_counter); 
     newTable.append('<thead class="ui-widget-header"><tr></tr></thead>'); 
     alert("Header Created"); 
     var thead = $('thead tr', newTable); 
     thead.append('<th><strong>Symbol</strong></th>'); 
     thead.append('<th><strong>Price</strong></th>'); 
     thead.append('<th><strong>Volume</strong></th>'); 
     thead.append('<th><strong>Buy</strong></th>'); 
     thead.append('<th><strong>Sell</strong></th>'); 
     alert("Header Created");    

究竟是我在這裏做錯了什麼。我對jquery非常陌生,仍然在努力。

回答

0

其實你可以創建一個表中的jQuery非常簡單:

var table = "<table class='draggable ui-widget'>" + 
      "<thead><tr><th></th></tr></thead>"+ 
      "<tbody>"+ 
      "<tr><td></td></tr>"+ 
      "<tr><td></td></tr>"+ 
      "</tbody></table>"; 

然後通過(#divid).append(table); 其附加到你的DIV這應該工作,也實在是很容易的。

+0

@tittletipz ....我也試過這個,但它不工作:(沒有類也工作,它創建了一個表,但類 – 2011-05-21 10:14:14

+0

@tittletipz .....當我還在中增加一個班級,但是當我在

班上班時,它無法正常工作 – 2011-05-21 10:31:46

+0

@Java_NewBie: - hei試試@Yury Tarabanko的回答 – 2011-05-21 12:06:19

0

添加類是不夠的。 AFAIK你應該明確指出你的元素是可拖動的。例如,將id添加到您的表格元素<table id='draggable'...,然後使其可拖動$('#draggable').draggable();

順便說一下,DOM操作是非常昂貴的。因此,在性能成本方面更好,首先創建整個表格html,像@tittletipz指出的那樣,然後將它附加到你的div上。

+0

Yury Tarabanko ....基本上我也創建動態ID的,我也嘗試過這個解決方案,但它不工作:(當我手動創建一個表,並提供一個ID到一個表。好,但是當我去動態創建一個表時,會出現一個問題 – 2011-05-23 06:46:18

+0

正如我已經說過的那樣,添加類「draggable」不足以使元素可拖動。http://jsfiddle.net/YV7Pf/2/。 $(「#draggable」).draggable();'這個觀點不是關於id。 – 2011-05-24 04:57:49

+0

Yury Tarabanko ...謝謝你的關注。我試過$(「#draggable」).draggable();它拖動整個表,但不是列,但至少它工作了一點:)。我想拖動列。手動它工作正常,但動態拖動整個表格而不是列。 – 2011-05-24 05:41:05

0

動態添加類無法通過事件訪問正常

在您的例子,如果我要觸發click事件的函數,我不能使用

$('.draggable').click(function{ 
}); 

我應該使用

$('.draggable').live('click', function{ 
    }); 

作爲綁定事件可以發生只有這樣。但你沒有提到你想如何使用類

+0

@ sathishn..i也嘗試過這種解決方案,但沒有工作......我陷入了這個:(。 – 2011-05-21 12:01:12