简体   繁体   English

具有动态内容的Bootstrap模式加载最后的数据

[英]Bootstrap Modal with Dynamic Content loads last data

I have a table that when I hit save, it would get all the input fields in the first column and check in the database if the data already exists. 我有一个表,当我单击保存时,它将获得第一列中的所有输入字段,并在数据库中检查数据是否已存在。 If the condition is true, it would show an icon per row. 如果条件为true,则每行将显示一个图标。 And when I click that icon, the info relevant to that specific data will show as a bootstrap modal. 当我单击该图标时,与该特定数据相关的信息将显示为引导模式。

I've been working on my problem the whole day. 我整天都在解决我的问题。 I first tried to make it work with only one data. 我首先尝试使其仅处理一个数据。 When I got what I wanted, I started to work on multiple data. 当我得到想要的东西时,我开始处理多个数据。

When multiple data is checked and they are duplicates, only the last info is shown even if there are 2 or more. 如果检查了多个数据并且它们是重复的,则即使有2个或更多,也仅显示最后一个信息。

Here's my code: 这是我的代码:

The save button: 保存按钮:

$( "#save_Boxes" ).click(function() {
    $.ajax({
        type: "POST", 
        url: window.base_url+'oss/admin/check_receive_data', 
        data: $.param($('form#receiving-form').serializeArray()),
        dataType : 'JSON',
        success: function (response) {
            var new_arr = response.receive_array;
            console.log(new_arr);
            var no_duplicate = 0;
            //THIS IS WHERE THE  PROCESS SHOULD TAKE PLACE
            $('table#receiving-box-table tbody tr').each(function(index){ 
                var ctno = $(this).find('td:first input').val(); // get courier trancking

                var td_id = $(this).find('td:last button.duplicate-data').attr('id');
                var target = $(this).find('td:last button.duplicate-data').attr('data-target');

                // check if ctno is present in response array or not
                var arr = $.grep(response.receive_array, function( n ) {
                   return ( n.courier_tracking_no === ctno);
                });
                if(arr.length){ // if present then show error message
                   // alert('wsdds');
                   no_duplicate = 1;
                   $(this).find('td:first input').attr('disabled', 'disabled');
                   $('button#'+td_id).show(); // let it be hidden by default
                   $(this).find('td:first input').closest('td').addClass('has-error');
                }

               var new_ctno = $('button#'+td_id).closest('tr').find('td:first input').val();
               $.each(new_arr, function(idx, obj){ 
                    console.log(idx + ": " + obj.courier_tracking_no);
                    console.log(target);
                    $(target).on('hidden.bs.modal', function(){
                                $(target+' .modal-title').html('');
                                $(target+' .modal-body').html('');
                            });
                    $('button#'+td_id).off('click').on('click', function(){
                        $(target).load(window.base_url+'oss/admin/box_duplicate',
                            function(){
                                $(target+' .modal-title').html('Duplicate Courier Tracking Number - '+obj.courier_tracking_no);
                                $(target+' .modal-body').html("<p class='text-left'>This box already exists. Please delete.</p><table class='table table-hover table-bordered table-striped'><tbody><tr><th scope='row'>Batch No.</th><td>"+obj.batch_no+"</td></tr><tr><th scope='row'>Courier Name</th><td>"+ucword(obj.courier_name)+"</td></tr><tr><th scope='row'>Vendor Name</th><td>"+ucword(obj.vendor_name)+"</td></tr><tr><th scope='row'>Status</th><td>"+ucword(obj.status)+"</td></tr></tbody></table>");
                                 $(target).modal('show');
                        });
                    });
                });

            });
            if(no_duplicate == 0){
                var check_if_empty = 0;
                $('input[name^="courier_tracking_no[]"]').each(function(){
                    if($(this).val() != ""){
                        check_if_empty += 1;
                    }
                });
                if(check_if_empty > 0){
                    $('#receiving-form').submit();
                }
            }
        },
            error: function (MLHttpRequest, textStatus, errorThrown) {
            console.log("There was an error: " + errorThrown);  
        }
    });
});

The html table: html表格:

<table id="receiving-box-table" class="table table-hover table-bordered table-striped">
    <thead>
        <tr>
            <th>Courier Tracking #</th>
            <th>Courier</th>
            <th>Vendor</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><input type="text" form="receiving-form" class="form-control input-sm track_no" name="courier_tracking_no[]" id="courier_tracking_no_1" /></td>
            <td><input type="text" form="receiving-form" class="form-control input-sm" name="courier_name[]" id="courier_name_1" onkeypress="if (event.keyCode == 13) {return false;}"/></td>
            <td><input type="text" form="receiving-form" class="form-control input-sm" name="vendor_name[]" id="vendor_name_1" onkeypress="if (event.keyCode == 13) {return false;}"/></td>
            <td class="box-action"><button class="btn btn-danger btn-xs clear-data" data-toggle="tooltip" data-placement="right" title="Clear input fields"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button> <button style="display:none;" id="dup-0" data-toggle = "modal" data-target = "#dupli-modal-0" class="btn btn-warning btn-xs duplicate-data" title="Duplicate Data"><span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span></button><div class = "modal fade" id = "dupli-modal-0" tabindex = "-1" role = "dialog" aria-labelledby = "dupli-modal-0Label" aria-hidden = "true"></div></td>
        </tr>
    </tbody>
</table>

Note: Only the first row is shown because the following rows are dynamically created. 注意:因为第一行是动态创建的,所以仅显示第一行。

The html modal: html模态:

<div class="modal-dialog">
<div class="modal-content">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title"></h4>
    </div>
    <div class="modal-body">
        <div class="row" style="margin-left: 0; margin-right: 0;">
        </div>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Ok</button>
    </div>
</div>

What am I missing? 我想念什么? Even if the modal comes from only 1 file, it has unique ID's so it should not be a problem. 即使模态仅来自1个文件,它也具有唯一的ID,因此这不是问题。

Thanks for your help! 谢谢你的帮助! -Eli -Eli

I tried a different approach. 我尝试了另一种方法。 Instead of calling a url (I use CI) and showing the file that holds the modal template, I directly added the template inside my code: 我没有调用URL(我使用CI)并显示了包含模式模板的文件,而是直接在代码中添加了模板:

$( "#save_Boxes" ).click(function() {
    $.ajax({
        type: "POST", 
        url: window.base_url+'oss/admin/check_receive_data', 
        data: $.param($('form#receiving-form').serializeArray()),
        dataType : 'JSON',
        success: function (response) {
            var new_arr = response.receive_array;
            console.log(new_arr);
            var no_duplicate = 0;
            //THIS IS WHERE THE  PROCESS SHOULD TAKE PLACE
            var stored =    [];
            $('table#receiving-box-table tbody tr').each(function(index){ 
                var ctno = $(this).find('td:first input').val(); // get courier trancking
                var td_id = $(this).find('td:last button.duplicate-data').attr('id');
                var target = $(this).find('td:last button.duplicate-data').attr('data-mod_id');
                stored.push(target);    
                var arr = $.grep(response.receive_array, function( n ) {
                   return ( n.courier_tracking_no === ctno);
                });
                if(arr.length){ // if present then show error message
                   // alert('wsdds');
                   no_duplicate = 1;
                   $(this).find('td:first input').attr('readonly', 'readonly');
                   $('button#'+td_id).show(); // let it be hidden by default
                   $(this).find('td:first input').closest('td').addClass('has-error');
                }
            });
            $('div.modal_holder').html('');
            all_modals = '';
            var modal_list = $.each(new_arr, function(idx, obj){
                    all_modals += "<div class = 'modal fade' id = '"+stored[idx]+"' tabindex = '-1' role = 'dialog' aria-labelledby = '"+stored[idx]+"Label' aria-hidden = 'true'><div class='modal-dialog'><div class='modal-content'><div class='modal-header'><button type='button' class='close' data-dismiss='modal'>&times;</button><h4 class='modal-title'></h4></div><div class='modal-body'><div class='row' style='margin-left: 0; margin-right: 0;'><p class='text-left'>This box already exists. Please delete.</p><table class='table table-hover table-bordered table-striped'><tbody><tr><th scope='row'>Batch No.</th><td>"+obj.batch_no+"</td></tr><tr><th scope='row'>Courier Name</th><td>"+obj.courier_name+"</td></tr><tr><th scope='row'>Vendor Name</th><td>"+obj.vendor_name+"</td></tr><tr><th scope='row'>Status</th><td>"+obj.status+"</td></tr></tbody></table></div></div><div class='modal-footer'><button type='button' class='btn btn-default' data-dismiss='modal'>Ok</button></div></div></div></div>";
                });
            $('div.modal_holder').html(all_modals);
            console.log(no_duplicate);
            if(no_duplicate == 0){
                var check_if_empty = 0;
                $('input[name^="courier_tracking_no[]"]').each(function(){
                    if($(this).val() != ""){
                        check_if_empty += 1;
                    }
                });
                console.log(check_if_empty);
                if(check_if_empty > 0){
                    $('#receiving-form').submit();
                }
            }
        },
            error: function (MLHttpRequest, textStatus, errorThrown) {
            console.log("There was an error: " + errorThrown);  
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM