bootstrap-table 表格行内编辑实现


本文摘自PHP中文网,作者angryTom,侵删。

这篇文章向大家介绍一下如何使用bootstrap table插件实现表格的行内编辑功能。

推荐教程:Bootstrap框架

先放一张效果图:

bootstrap71.gif

应用场景

之前的项目也是采用bootstrap table,添加和修改数据都是通过模态框来编辑的,后来有了点击行来编辑和新增的需求,于是乎试试……

html

1

2

3

4

5

6

7

<div class="table-box" style="margin: 20px;">

    <div id="toolbar">

        <button id="button" class="btn btn-default">insertRow</button>

        <button id="getTableData" class="btn btn-default">getTableData</button>

    </div>

    <table id="table"></table>

</div>

script

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

$(function() {

    let $table = $('#table');

    let $button = $('#button');

    let $getTableData = $('#getTableData');

 

    $button.click(function() {

        $table.bootstrapTable('insertRow', {

            index: 0,

            row: {

                id: '',

                name: '',

                price: ''

            }

        });

    });

 

    $table.bootstrapTable({

        url: 'data2.json',

        toolbar: '#toolbar',

        clickEdit: true,

        showToggle: true,

        pagination: true,       //显示分页条

        showColumns: true,

        showPaginationSwitch: true,     //显示切换分页按钮

        showRefresh: true,      //显示刷新按钮

        //clickToSelect: true,  //点击row选中radio或CheckBox

        columns: [{

            checkbox: true

        }, {

            field: 'id',

            title: 'Item ID'

        }, {

            field: 'name',

            title: 'Item Name'

        }, {

            field: 'price',

            title: 'Item Price'

        }, ],

        /**

         * @param {点击列的 field 名称} field

         * @param {点击列的 value 值} value

         * @param {点击列的整行数据} row

         * @param {td 元素} $element

         */

        onClickCell: function(field, value, row, $element) {

            $element.attr('contenteditable', true);

            $element.blur(function() {

                let index = $element.parent().data('index');

                let tdValue = $element.html();

 

                saveData(index, field, tdValue);

            })

        }

    });

 

    $getTableData.click(function() {

        alert(JSON.stringify($table.bootstrapTable('getData')));

    });

 

    function saveData(index, field, value) {

        $table.bootstrapTable('updateCell', {

            index: index,       //行索引

            field: field,       //列名

            value: value        //cell值

        })

    }

 

});

实现原理

阅读剩余部分

相关阅读 >>

bootstrap怎么设置行高

当 position:sticky 遇到 bootstrap 浮动布局时候的踩坑记录

html form标签与table标签的区别是什么?这里有说明!

如何使用bootstrap制作form表单

bootstrap下拉列表怎么设置

bootstrap怎么设置背景图片自适应

bootstrap 怎么安装

bootstrap是一款软件吗

bootstrap 如何修改css样式

html里关于表格table嵌套的注意事项

更多相关阅读请进入《bootstrap》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...