javascript - Dynamically create objects and dynamically add data
女神的闺蜜爱上我
女神的闺蜜爱上我 2017-07-05 10:44:16
0
2
809

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

The following is the code that only implements the function of adding tr and deleting tr as I mentioned. I haven't created objects or added data. Does anyone have any ideas? Wait online. Urgent! ! !

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();
        })
女神的闺蜜爱上我
女神的闺蜜爱上我

reply all(2)
我想大声告诉你

Let me briefly describe my thoughts: Each DOM structure maintains a data object, assuming your HTML structure is (emmet syntax):

.app-test > .col-md-5.app-test-taxpayer + .col-md-5.app-test-report

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:

(function($){
    // 左侧 tr 组件
    var TaxesReportTaxpayer = function(trs){
        return this;
    };
    
    // 右侧 tr 组件
    var TaxesReportReporter = function(trs, taxpayer){
        
        // 当前纳税人的报表,对应右侧的列表条目
        this.reports = [];
    
        return this;
    };
    
    // 一个独立的功能模块
    var TaxesReport = function(element){
        // 全部纳税人,即左侧列表对应的数据源
        this.taxpayers = [];
        
        // 当前选择的纳税人,即左侧列表中的紫色高亮行
        this.currentTaxpayer = null;
    };
    
    // 应用实例 - 假设: .test > .col-md-5.clearfix
    var trDemo = new TaxesReport( $('.app-test') );
    
})(jQuery);

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:

// TaxesReport() 中
var _that = this,
    $taxpayerAdd = $('.button1),
    $taxpayerList = $('.zType_tbody');
   
// 新建纳税人 
$taxpayerAdd.on('click', function(){
    // 实例化纳税人组件
    var taxpayer = new TaxesReportTaxpayer(_that);
    
    // 增加行
    $taxpayerList.append( taxpayer.item );
    
    // 新增数据
    _that.setTaxpayer( taxpayer.data );
    
    // 自动高亮
    if(!_that.currentTaxpayer) {
        _that.currentTaxpayer = taxpayer;
    }
});

Add the taxpayer’s report type on the right, such as:

// TaxesReport() 中
var _that = this,
    $reportAdd = $('.button2'),
    $reportList = $('.zType_all');
    
// 新增纳税人报表类型
$reportAdd.on('click', function(){
    // 请从左侧选择一个纳税人
    if( !_that.currentTaxpayer ) {
        console.log('请从左侧选择一个纳税人');
        return;
    }
    
    // 实例化报表组件
    var report = new TaxesReportReporter(_that);
    
    // 增加行
    $reportList.append( report.item );
    
    // 增加数据
    // _that.currentTaxpayer.setReport(report.data)
    _that.currentTaxpayer.item.trigger('setReport', report.data);
    
    // 查看是否添加成功
    console.log( _that.currentTaxpayer.reports );

});
    

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 and reports 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).

小葫芦
  1. 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

  2. When switching on the left, find the corresponding model.children directly from the key-model data cache and redraw the list on the right

  3. When adding or deleting on the left side, just add and delete from the key-model data cache

  4. 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 problems

This is the idea, you can try to write down the code first

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template