layui学习之路——删除数据表格记录
上一篇写了基本的数据展现,今天写删除记录。 html 把删除按钮放在表格的顶端工具栏位置
<script type="text/html" id="toolbarDemo">
<div class="layui-btn-container">
<asp:button class="layui-btn layui-btn-sm" lay-event="delete">删除</asp:button>
</div>
</script>
<table id="demo" lay-filter="test" ></table>
js
$(document).ready(function() {
reloadTable();
});
function reloadTable(){
layui.use(['upload', 'element', 'layer','table'],function(){
var table=layui.table;
var index = layer.load(1);
table.render({
elem:'#demo',
height:600,
url:'global/DataService_Test.ashx' ,
loading:false,
method:'post',
page:false,
defaultToolbar: [],
request: {
pageName: 'page',
limitName: 'limit'
},
id: 'table1',
parseData: function(res) {
return {
"code": res.code,
"msg": res.msg,
"count": res.count,
"data": res.data
};
},
cols:[[
{type:'radio'}
,{field: 'col1', title: 'col1', align:'center'}
,{field: 'col2', title: 'col2',align:'center'}
,{field: 'col3', title: 'col3',width:100, align:'center'}
]],
done: function(res, curr, count) {
console.log(res);
console.log(curr);
console.log(count);
layer.close(index);
}
});
table.on('toolbar(test)',function(obj){
var checkStatus = table.checkStatus(obj.config.id);
var data = checkStatus.data;
switch(obj.event){
case 'delete':
if (data.length > 0) {
var col1= data[0].col1;
layer.open({
title: '你确定删除' + col1+ '?'
, btn: ['确定', '取消']
, yes: function(index, layero) {
var p1 = new Object();
p1.name = "col1";
p1.value = col1;
var array = [];
array.push(p1);
$.ajax({
url: "global/ActionService.ashx?action=deleteRow",
type: "POST",
cache: false,
dataType: "json",
async: false,
data: JSON.stringify(array),
success: function(result) {
if (result == 1) {
layer.msg('删除成功!');
window.parent.location.reload();
}
else {
layer.msg('删除失败!');
return false;
}
},
error: function(result) {
}
});
}
, btn2: function(index, layero) {
layer.close(index);
}
});
} else { layer.msg('请先选择一条数据'); }
break;
}
});
});
}
ashx
public void ProcessRequest(HttpContext context)
{
string action = context.Request["action"].ToString();
SqlConnection DB = new SqlConnection(WebConfigurationManager.AppSettings["key"].ToString());
if (action == "deleteRow")
{
DB.Open();
string json = string.Empty;
using (var reader = new StreamReader(context.Request.InputStream))
{
json = reader.ReadToEnd();
}
string col1= string.Empty;
if (!String.IsNullOrEmpty(json))
{
var paraList = new JavaScriptSerializer().Deserialize<List<Para>>(json);
foreach (Para p in paraList)
{
switch (p.name)
{
case "col1":
col1= p.value;
break;
}
}
}
string sql_del = "delete from table where col1='" + col1+ "'";
SqlCommand sqlCommand = new SqlCommand(sql_del, DB);
sqlCommand.ExecuteNonQuery();
DB.Close();
context.Response.Write("1");
}
public class Para
{
public string name { get; set; }
public string value { get; set; }
}
}
这样删除就实现了,但是性能在IE上很不理想,删除会卡好久,谷歌就正常。
|