可编辑的HTML表格 Jquery无刷新添加/删除/修改表格
删除数据库已经屏蔽了,更新限制了为5个字符
XML/HTML Code
- <?php
- require "../../conn.php";
- ?>
- <div class="container">
- <div class="row">
- <div class="col-md-12" style="padding:2em 0;">
- <p>点击 <i class="fa fa-pencil box"></i> 按钮可以对表格进行编辑,点击 <i class="fa fa-trash-o box"></i>按钮可以将该表格行删除。</p>
- <div class="table-responsive">
- <table class="table table-bordered table-striped" id="mytable" columnsEd="" addButton="add">
- <thead>
- <tr>
- <th class="hide">ID</th>
- <th>姓名</th>
- <th>年龄</th>
- <th>email</th>
- </tr>
- </thead>
- <tbody>
- <?php
- $sql="select * from `add_delete_record` where id>0";
- $rs=mysqli_query($lr,$sql);
- if ($row = mysqli_fetch_array($rs))
- {
- do {
- ?>
- <tr>
- <td class="hide"><?php echo $row['id']?></td>
- <td class="content"><?php echo $row['content']?></td>
- <td class="text"><?php echo $row['text']?></td>
- <td class="position"><?php echo $row['position']?></td>
- </tr>
- </Tr>
- <?php
- }
- while ($row = mysqli_fetch_array($rs));
- }?>
- </tbody>
- </table>
- </div>
- </div>
- <div class="col-md-12" style="padding-bottom:2em;">
- <button class="btn btn-info" id="add"><i class="fa fa-plus"></i> 添加新的表格行</button>
- </div>
- </div>
- </div>
JavaScript Code
- <script type="text/javascript">
- var table = SetEditable({
- tabedId:'mytable,mytable2',
- onEdit: function(row,values,tableid) {
- //编辑后回调
- //console.log(row);
- //console.log(values);
- //console.log(tableid);
- $.ajax({
- type: "post",
- url: "ajax.php",
- data: values,
- async: false,
- success: function (res) {
- //编辑后可以给第一个单元格id设置默认值
- row.children('td').eq(0).text(res.id);
- }
- });
- },
- onBeforeDelete: function(row) {
- //删除前回调
- console.log(row);
- //获取第一个单元格隐藏的id
- id=row.children('td').eq(0).text();
- $.ajax({
- type: "post",
- url: "del.php",
- data: "id="+id,
- async: false
- });
- //获取第一个单元格隐藏的id
- },
- onDelete: function(tableid) {
- //删除后回调
- //console.log(tableid);
- },
- onAdd: function(tableid) {
- //添加表格后回调
- console.log(tableid);
- }
- });
- </script>
原文地址:http://www.freejs.net/article_jquerywenzi_925.html