jquery - BootstrapTable - 动态添加和删除行

标签 jquery twitter-bootstrap-3 bootstrap-table

我正在使用BootstrapTable插入。我想动态添加和删除一些行。我的问题是,每次添加新行时,在输入中键入一些字符,然后删除一些行,所有数据都会丢失。 我做错了什么?底部的示例和 fiddle

http://jsfiddle.net/71na5kne/

HTML 表格

<div id="modal_toolbar_new" class="btn-group " >
    <button type="button" class="btn btn-default" name="modal new add">
        ADD
    </button>
    <button type="button" class="btn btn-default" name="modal new delete">
        DEL
    </button>

</div>
<table id="table_new"
class="table table-condensed table-hover table-striped" 
name="new"
data-toolbar="#modal_toolbar_new"
data-sortable="true"
data-toggle="table"
data-show-toggle="true"
data-show-columns="true"
data-height="350"
data-search="true"
>
    <thead>
        <tr>
            <th data-field="state" data-checkbox="true" data-formatter="stateFormatter"></th>
            <th data-sortable="true" data-field="outage_id">ID</th>
            <th data-sortable="true" data-field="system_code">System</th>
            <th data-sortable="true" data-field="type">Type</th>
        </tr>
    </thead>

Javascript

 var selected = {
    new: []
};
$(function() {

    $("button[name='modal new delete']").click(function() {
        $('#table_new').bootstrapTable('remove', {
            field: 'id',
            values: selected.new
        });
        selected.new = [];
    });


    $("button[name='modal new add']").click(function() {
        var randomId = Number(new Date());

        $('#table_new').bootstrapTable('insertRow', {index: 1, row: {
                id          : randomId,
                outage_id   : '<input type="text" class="form-control" name="outage_id" value="change this value, next delete one row'+randomId+'"/>',
                system_code : '<select class="form-control" name="system_code"><option value=""></option></select>',
                type : '<input type="checkbox" name="dotyczy_atv"/>'
            }
        });

    });

  /* click input actions */    
    $('.table').on('check.bs.table', function(e, name, args) {
        setSelectedItems($(this).attr('id'), $(this).attr('name'));
    });
    $('.table').on('uncheck.bs.table', function(e, name, args) {
        setSelectedItems($(this).attr('id'), $(this).attr('name'));
    });

    $('.table').on('check-all.bs.table', function(e, name, args) {
        setSelectedItems($(this).attr('id'), $(this).attr('name'));
    });
    $('.table').on('uncheck-all.bs.table', function(e, name, args) {
        setSelectedItems($(this).attr('id'), $(this).attr('name'));
    });

    });

function setSelectedItems(id, name) {

    switch( id ) {
        case 'new':
            selected.new = [];
            $($('#table_new').bootstrapTable('getSelections')).each(function(index) {
                selected[name].push(this.id);
            });   
            break;
        default:            
            selected[name] = [];
            $($('#' + id).bootstrapTable('getSelections')).each(function(index) {
                selected[name].push(this.id);
            });            
            break;
    }
}

最佳答案

要动态添加行,请使用 Bootstrap 的“insertRow”选项:

 var rowId = $("#tableName >tbody >tr").length;
        rowId = rowId + 1;
        console.log("RowId is "+rowId);
        $('#tableName').bootstrapTable('insertRow',{
            index: rowId,
            row: myList[0] //myList is an array containing json data in each row
        });

要动态删除一行,可以直接使用jquery:

$("#tableName tr:eq("+delrowId+")").remove(); //to delete row 'i', delrowId should be i+1

关于jquery - BootstrapTable - 动态添加和删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28503985/

相关文章:

jquery - 为大屏幕制作 3 行菜单

python - 使用 Dash 将工具提示添加到 Bootstrap Table 中的单元格

javascript - keyup 更改未在 js 文件中触发

javascript - 根据 json 查找更改列表项的背景

javascript - 如何删除给定脚本中的 brs

html - 外部的 Bootstrap-3 旋转木马控件

Javascript 删除表单数据

javascript - jqueryui slider 在 bootstrap 弹出窗口内不起作用

html - Bootstrap 4 table-bordered 失去了它的风格

javascript - 隐藏 javascript 元素 - 不是常见问题