bootstrap-table 行内编辑简单实现

$('#table').bootstrapTable({
    url: createUrl(''),
    striped: true,
    uniqueId: 'attrValue',
    columns: [{
        title: 'ID',
        field: 'index',
        formatter: formatterIndex
    },{
        title: '规格',
        field: 'attrValue',
        class: 'editable'
    },{
        title: '价格',
        field: 'sellPrice',
        class: 'editable'
    },{
        title: '操作',
        field: 'operate',
        formatter: formatterOperate
    }]
});
function formatterIndex(value, row, index){
    var i = index + 1;
    if(i < 10){
        return "0" + i;
    }else{
        return i;
    }
}
function formatterOperate(value, row, index){
    return "<button onclick='saveRow("+index+")' class='btn small'><i class='fa fa-edit'></i> 保存</button><button onclick='editRow("+index+")' class='btn small blue'><i class='fa fa-edit'></i> 编辑</button><button onclick='delRow(\""+row.attrValue+"\")' class='btn small red'><i class='fa fa-trash-o'></i> 删除</button>";
}
$("#addBtn").click(function(){
    var data = {
        attrValue: '',
        sellPrice: ''
    };
    $("#table").bootstrapTable('append', data);
    $("#table tr:last-child td.editable").each(function(){
        $(this).html("<input>");
    });
});
function saveRow(index, value){
    var obj = $("#table tr:nth-child("+ (index+1) +") td.editable");
    var attrValue = obj.first().find("input").val().trim();
    var sellPrice = obj.last().find("input").val().trim();
    var newData = {
        attrValue: attrValue,
        sellPrice: sellPrice
    };
    $("#table").bootstrapTable('updateRow', {
        index: index,
        row: newData
    });
    obj.find("input").remove();
}
function editRow(index){
    $("#table tr:nth-child("+ (index+1) +") td.editable").each(function(){
        var value = $(this).text();
        $(this).html("<input value='"+value+"'>");
    });
}
function delRow(value){
    $("#table").bootstrapTable('removeByUniqueId', value);
}
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值