bootstrap table 用 data-url="data.php" 的方式填充的数据
不能增加属性。怎么删除呢?
有办法,先将数据填充了再说,用firefox firedebug,查看bootstrap table生成的表格,鼠标悬停点击。
查看到tr有个data-index的属性,属性的值是json数组的数字升序来的。
这就好办了。
先在jquery的ajax中组织好数据,将json数组,增加一个按升序的值
在前端页面将idx的值返回给ajax作为删除的依据:
function actionFormatter(value, row, index) {
return [
"<button class='btn btn-primary btn-xs like'data-toggle=\"modal\" data-target=\"#viewpw\">查看密码</button> ",
"<button class='btn btn-info btn-default btn-xs' data-toggle=\"modal\" data-target=\"#addSource\">生成密码</button> ",
"<button class='btn btn-danger btn-xs unlike'data-toggle=\"modal\" data-target=\"#deleteSource\">删除资源</button>",
].join('');
}
window.actionEvents = {
'click .like': function (e, value, row, index) {
$.post("viewpw.php", {id: JSON.stringify(row.id)}, function (data) {
$("#viewpw #pwpanel").html(data);
});
return false;
},
'click .unlike': function (e, value, row, index) {
$("#delbutton").attr("rmid",row.id);
$("#delbutton").attr("filename",row.name);
$("#delbutton").attr("idx",row.idx);
return false;
},
};
ajax 删除时后端返回成功的标志后,用jquery 的xpath选择器,附带idx的值,删除行搞定问题
// Delete function
$(document).on('click', '#delbutton', function () {
var filePath = $(this).attr('filename');
var rmid = $(this).attr('rmid');
var idx = $(this).attr('idx');
$.ajax({
url: 'upload.php',
type: 'POST',
dataType: 'json',
data: {del: rmid, filePath: filePath},
success: function (res) {
if (res.type == 'success') {
$("[data-index="+idx+"]").remove();
}
flashMsg(res.msg);
$("#deleteSource").modal("hide");
window.location.href = window.location.href;
},
error: function (e) {
flashMsg(res.msg);
window.location.href = window.location.href;
}
});
});
总算写了一篇自己解决的心得,真不容易。
好吧,我承认我没有认真看文档:http://issues.wenzhixin.net.cn/bootstrap-table/#methods/remove.html
其他前端表格资源:http://www.yyyweb.com/4687.html