As shown in the figure:
1. The tables on the left side are dynamically added by clicking the "Add" button above. Click on a row of the table on the left to create a table on the right (any number of rows will do), with a one-to-many relationship on the left and right.
2. When you want to add a new row on the left, create an independent object (meaning that each row corresponds to a different object), and then click Add on the right. The newly created row will have several In the drop-down menu, after the user has made a selection, he will click to start filling in the report. When clicking the start filling in report button, the data on the right and the data on the left need to be stored in an object in a row on the left. (Each row on the left corresponds to an object)
3. There are 'Delete' buttons above the left and right tables. If you click the 'Delete' button on the left, the storage object in this row will be deleted, so the data corresponding to it on the right Not hungry anymore either. If you select a row on the right and click 'Delete', then only the data storage of this row on the right will be deleted in the object on the left.
Framework:
Used Bootstrap jQuery
Code:
<p class="col-md-5 clearfix">
<button class="button1">新增</button>
<button class="button1_del">删除</button>
<table border="1px solid #000" class="text-center">
<thead class="zType_thead text-center">
<tr>
<th></th>
<th>纳税人识别号</th>
<th>纳税人名称</th>
</tr>
</thead>
<tbody class="zType_tbody">
</tbody>
</table>
</p>
<p class="col-md-offset-1 col-md-5">
<button class="button2">新增</button>
<button class="button2_del">删除</button>
<button class="baocun_button" type="submit">开始填报</button>
<form class="clearfix" method="post">
<table class="zType_table text-center" border="1px solid #000">
<thead>
<tr class="zType_table_th">
<th>计算期类型</th>
<th>期间</th>
<th>征收方式</th>
</tr>
</thead>
<tbody class="zType_all">
</tbody>
</table>
</form>
</p>
js code:
//点击选中右侧表格的某一行
$('.zType_all').on('click','tr',function(){
//点击某一行 此行背景色改变,其他行恢复白色。点击的那一行添加了name属性,然后删除其他tr的name值,为的是之后联系起来,有个name属性作为桥梁,比如删除,就可以在删除掉对应带有name值的tr
$('.zType_all').children().css({"background":"#fff","color":"#000"}).removeAttr('name');
$(this).css({"background":"#bfaadc","color":"#000"});
if(!$(this).attr('name')){
$(this).attr("name",'zType_tr_checked1');
}
})
//右侧删除按钮 点击删除带有name的tr
$('.button2_del').on('click',function(){
$('tr[name=zType_tr_checked1]').remove();
})
//左侧添加按钮
$('.button1').on('click',function(){
$('.zType_tbody').append('<tr><td><span class="glyphicon glyphicon-chevron-right"></span></td><td><input type="text" name="shibiehao"></td><td><input type="text" name="mingcheng"></td></tr>');
})
//选中左侧表格的tr
$('.zType_tbody').on('click','tr',function(){
//依然还是点击某一行 此行背景色改变,其他行恢复白色。点击的那一行添加了name属性,然后删除其他tr的name值,为的是之后联系起来,有个name属性作为桥梁,比如删除,就可以在删除掉对应带有name值的tr
$('.zType_tbody').children().css({"background":"#fff"}).removeAttr('name');
$(this).css({"background":"#bfaadc"}).children().eq(0).children()
if(!$(this).attr('name')){
$(this).attr("name",'zType_tr_checked2');
}
})
//点击左侧删除按钮,删掉左侧选中的tr
$('.button1_del').on('click',function(){
$('tr[name=zType_tr_checked2]').remove();
})
Let me briefly describe my thoughts: Each DOM structure maintains a data object, assuming your HTML structure is (emmet syntax):
Then:
The above is divided into three components
.app-test
is responsible for maintaining all taxpayer data (i.e. the table on the left);Each tr in the list on the left maintains the data of the corresponding taxpayer (i.e. the table on the right);
All tr on both sides can be regarded as a small component - a sparrow is small but has all the internal organs;
summed up as:
Entire functional component > left tr component + right tr component
;You can understand it as passing values between parent and child components;
The implementation is roughly as follows:
First, the two tables belong to a functional module, assuming
TaxesReport
:Both TaxesReportTaxpayer and TaxesReportReporter need to pass in a TaxesReport instance for [child component to call parent component] method or data
Click the new button on the left to create a new taxpayer:
Add the taxpayer’s report type on the right, such as:
When deleting, in addition to deleting the corresponding tr structure and deleting the data corresponding to tr, the two data objects involved (
taxpayers
on the left andreports
on the right) are both array structures, and you can customize them The key name method creates a unique identifier for the new instance (taxpayer or report), and uses<tr data-identify="unique identifier">···</tr>
to establish value parameters.Since you may have static data (data with primary key identification that is not taken out from the database), and you may delete any entry in the array and cause the array subscript to be lost, so the best way is to create a createable A non-duplicate value method used to generate and add a unique identifier to the generated component (left or right).
Generate a data cache and save the data in the form of key-model. There is a children in the model on the right to save the corresponding data on the right
When switching on the left, find the corresponding model.children directly from the key-model data cache and redraw the list on the right
When adding or deleting on the left side, just add and delete from the key-model data cache
The same is true when adding and deleting on the right side. At this time, you can find the corresponding model.children
Since jQuery is used, jQuery can append data to the DOM through
data()
, so the above key-model can directly attach each model to the DOM of each row (on the left), and append on the right It’s not a big problem if there are no additional problemsThis is the idea, you can try to write down the code first