table 增加和删除行

1.table

<table id="table1" cellpadding="5" cellspacing="0" border="1" style="border: 1px solid #000000;margin-left: 40px;width: 95%;margin-bottom: 20px;">
									
										<tr class="tr_height">
											<td colspan="2" width="40%">内容</td>
											<td colspan="2"  width="40%">金额(人民币)</td>
											<td style="border-right: none;text-align: right;">
												<div style="margin-left: 27px;">操作</div>
											</td>
											<td style="border-left: none;text-align: right;">
												<button onclick="create()" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-normal"  >添加</button>
											</td>
										</tr>
										<tbody id="tbody1">
										<tr class="tr_height" id="tr1">
											<td contenteditable="true" colspan="2" ></td>
											<td contenteditable="true" colspan="2" ></td>
											<td colspan="2">
												<button class="layui-btn layui-btn-sm layui-btn-radius layui-btn-danger" onclick="del(1)" >删除</button>
											</td>
										</tr>
										</tbody>
										<tfoot>
										<tr class="tr_height">
											<td style="border-right: none;">合计(大写)</td>
											<td contenteditable="true" style="border-left: none;width: 25%;"></td>
											<td style="border-right: none;">合计(小写)</td>
											<td contenteditable="true" style="border-left: none;width: 25%;"></td>
											<td colspan="2" ></td>
										</tr>
										</tfoot>
								</table>
							</div>
						</table>

2.事件

<script>
		var count=1;
		function create (){
			count+=1;	
			var tr2 = '<tr class="tr_height"'+'id="tr'+count+'">'+
			'<td contenteditable="true" colspan="2" ></td>'+
			'<td contenteditable="true" colspan="2" ></td>'+
			'<td colspan="2">'+
				'<button class="layui-btn layui-btn-sm layui-btn-radius layui-btn-danger" onclick="del('+count+')" >删除</button>'+
			'</td>'+
			'</tr>';
			
			$("#tbody1").append(tr2);
		}
		
		function del(num){
			//console.log("num"+num)
			var trx="#tr"+num;
			console.log(trx)
			var tbody = document.getElementById("tbody1"); 
			//var deltr = document.getElementById(trx);
			$("#tbody1").find(trx).remove();
			
		}
	</script>

 

以下是一个 SWT Table 增加删除的示例代码,其中第一列为一个 CCombo 控件,第二列为一个字符串: ```java import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.*; public class TableDemo { private Shell shell; private Table table; private int rowCount = 0; public static void main(String[] args) { Display display = new Display(); TableDemo demo = new TableDemo(); demo.open(display); display.dispose(); } private void open(Display display) { shell = new Shell(display); shell.setText("SWT Table Demo"); shell.setLayout(new GridLayout(2, false)); table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION); table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1)); table.setHeaderVisible(true); table.setLinesVisible(true); TableColumn column1 = new TableColumn(table, SWT.NONE); column1.setText("Column 1"); column1.setWidth(100); TableColumn column2 = new TableColumn(table, SWT.NONE); column2.setText("Column 2"); column2.setWidth(100); final TableItem item1 = new TableItem(table, SWT.NONE); final CCombo ccombo = new CCombo(table, SWT.READ_ONLY); ccombo.add("Option 1"); ccombo.add("Option 2"); ccombo.add("Option 3"); ccombo.select(0); item1.setText(0, ""); item1.setControl(0, ccombo); item1.setText(1, "Row " + (++rowCount)); Button addButton = new Button(shell, SWT.PUSH); addButton.setText("Add Row"); addButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { TableItem item = new TableItem(table, SWT.NONE); final CCombo ccombo = new CCombo(table, SWT.READ_ONLY); ccombo.add("Option 1"); ccombo.add("Option 2"); ccombo.add("Option 3"); ccombo.select(0); item.setText(0, ""); item.setControl(0, ccombo); item.setText(1, "Row " + (++rowCount)); } }); Button deleteButton = new Button(shell, SWT.PUSH); deleteButton.setText("Delete Row"); deleteButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { int index = table.getSelectionIndex(); if (index != -1) { table.remove(index); } } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } } ``` 在这个示例中,我们首先创建了一个带有两个列的 SWT Table,并在第一的第一列中放置了一个 CCombo 控件。我们还创建了两个按钮,一个用于添加新,另一个用于删除选定的。 添加时,我们创建一个新的 TableItem,然后向第一列添加一个新的 CCombo 控件,并将其添加到 TableItem 上。我们还设置了第二列的文本,以便显示新号。 删除时,我们首先获取当前选定的的索引,然后使用 `Table.remove()` 方法将其从表中删除
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值