填充对象jquery并在Bootstrap-Table中添加数据

时间:2017-08-04 14:01:31

标签: javascript jquery bootstrap-table

要学习,我正在复制必须填写 obj_devedor 对象的代码,并在单击确认按钮时将数据添加到引导表。 我的目的是存储所有表单数据,然后将它们一次性发送到服务器;

<a href="#" id="btn_confirmar_email" class="btn btn-success"> Confirmar </a>

我收到以下错误。

  

未捕获的TypeError:无法读取null的属性“电子邮件”

enter image description here

以下代码:
元素

<input type="text" id="txt_email" class="email form-control input-sm" name="txt_email" value="">

<table class="table table-bordered table-striped table-hover" id="tbEmails" data-toggle="table" data-sort-name="Email">
     <thead>
           <tr>
                <th data-field="txt_email" >Email</th>
                <th data-field="sel_preferencial" >Preferêncial</th>
                <th data-field="sel_situacao" >Situação</th>
           </tr>
     </thead>
     <tbody>
           <tr>
                 <td data-field="txt_email" ></td>
                 <td data-field="sel_preferencial"></td>
                 <td data-field="sel_situacao"></td>
           </tr>
     </tbody>
</table>

JS

//Page Objects
var obj_devedor = null;

//Page Control Variables
var _editandoEmail = false;
var _idEmails = 0;
var _indexEmail = -1;
var _indexEmailGrid = -1;
var _txtEditandoEmail = '';

//Event
$('#btn_confirmar_email').click(function () {
    var pattern = /^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*(([ \t]*\r\n)?[ \t]+)?")@(([a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.)+([a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.?$/i;
    if (pattern.test($('#txt_email').val())) {
	confirma_email();
    } else {
        toastr.warning("Formato de email inválido.");
    }
});

//Confirm E-mail Function
function confirma_email() {
	campos = ['#txt_email'];
    var jaexiste = 0;
    var txt_email = $('#txt_email').val
	if (_txtEditandoEmail != txt_email) {
        jaexiste = $(obj_devedor.Emails).filter(function () {
	        return this.Email == $('#txt_email').val();
		}).length;
    }
	_txtEditandoEmail = '';
	
	if (valida_campo_obrigatorio(campos) && jaexiste == 0)
	{
        var item = {
            Id: novo_id(obj_devedor.Emails),
            Crud: 'C',
            IdPessoa: obj_devedor.Id,
            Email: $('#txt_email').val(),
            Situacao: $('#situacao').val(),
            IdOrigemDados: 1,
        }

        //inserting
        if (!_editandoEmail) {
            obj_devedor.Emails.push(item);
            $('#tbEmails').bootstrapTable('append', item);
            //editing     
        } else {
            Localiza('ema');

            if (obj_devedor.Emails[_indexEmail].Crud == 'R' || obj_devedor.Emails[_indexEmail].Crud == 'U') {
                item.Crud = "U";
            }

            item.Id = obj_devedor.Emails[_indexEmail].Id;
            item.Inclusao = obj_devedor.Emails[_indexEmail].Inclusao;
            obj_devedor.Emails[_indexEmail] = item;

            $('#tbEmails').bootstrapTable('updateRow', {
                index: _indexEmailGrid,
                row: item
            });
        }
        habilita_painel('!ema');
    }
    else if (jaexiste > 0) {
        toastr.warning("Este email já está sendo utilizado.");
    }
}

//New ID Function
function novo_id(lista) {
    var id = 0;
    if (lista.length > 0) {

        for (var i = 0; i < lista.length; i++) {
            if (lista[i].Id > id) {
                id = lista[i].Id
            }
        }
        id = id + 1;
    }
    return (id == 0 ? 1 : id);
}

//Valid Field Function
function valida_campo_obrigatorio(campos) {
    var ret = true;
    for (i = 0; i < campos.length; i++) {
        var $formgroup = $(campos[i]).parent();
        if (!$(campos[i]).val()) {
            if (!$formgroup.hasClass('has-error')) {
                $formgroup.addClass('has-error');
                $formgroup.append('<label class="text-danger2">Campo obrigatório.</label>');
            }
            ret = false;
        }
        else {
            if ($formgroup.hasClass('has-error')) {
                $formgroup.removeClass('has-error');
                $formgroup.find('.text-danger2').remove();
            }
        }
    }
    return ret;
}

0 个答案:

没有答案