2011-05-21 170 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