如果您想訂閱本博客內(nèi)容,每天自動(dòng)發(fā)到您的郵箱中, 請(qǐng)點(diǎn)這里
    
    
        
            前言
        
    
    
        - 
            最近在研究bootstrap table的表格的單元格編輯功能,實(shí)現(xiàn)點(diǎn)擊單元格修改內(nèi)容,其中包括文本(text)方式修改,下拉選擇(select)方式修改,日期(date)格式修改等。
        
- 
            本文著重解決x-editable編輯的數(shù)據(jù)動(dòng)態(tài)添加和顯示數(shù)據(jù)為Empty的問(wèn)題,還有給表格單元格的內(nèi)容設(shè)置多樣式,使得顯示多樣化。
        
- 
            由于官網(wǎng)給的demo的數(shù)據(jù)都是html文件里寫好的,select類型的不能動(dòng)態(tài)添加(所以網(wǎng)上的大多都是官網(wǎng)的類似例子,本篇博客就是在這種情況下以自己的經(jīng)驗(yàn)分享給大家,有問(wèn)題可以留言哦),一旦動(dòng)態(tài)添加就會(huì)出現(xiàn)顯示數(shù)據(jù)為Empty,我表格原本是有數(shù)據(jù)的,但是一用這個(gè)插件就把數(shù)據(jù)變成Empty了,這可不是我想要的,所以筆者就自行解決了這個(gè)問(wèn)題。
        
        
            對(duì)比網(wǎng)上的例子
        
    
    
        - 
            比如以下這種數(shù)據(jù)不是Empty的例子,但是是由于在html中寫死了數(shù)據(jù)(awesome),不適合動(dòng)態(tài)添加。
        
<a href="#" id="username" data-type="text" data-pk="1">awesome</a> <script> $(function(){ $('#username').editable({
        url: '/post',
        title: 'Enter username' });
}); </script>
    
    
        - 
            另外一種就是使用bootstrap table動(dòng)態(tài)添加的,但是select類型就會(huì)出現(xiàn)數(shù)據(jù)為Empty的情況。
        
$('#db_dependences').bootstrapTable({
        method:'POST',
        dataType:'json',
        contentType: "application/x-www-form-urlencoded",
        cache: false,
        striped: true,  sidePagination: "client",  showColumns:true,
        pagination:true,
        minimumCountColumns:2,
        pageNumber:1,  pageSize: 10,  pageList: [10, 15, 20, 25],  uniqueId: "id",  showExport: true,                    
        exportDataType: 'all',
        exportTypes:[ 'csv', 'txt', 'sql', 'doc', 'excel', 'xlsx', 'pdf'],  onEditableSave: function (field, row, oldValue, $el) { $.ajax({
                success: function (data, status) { if (status == "success") {
                        alert("編輯成功");
                    }
                },
                error: function () { alert("Error");
                },
                complete: function () { }
            });
        },
        data: [{
            id: 1,
            name: '張三',
            sex: '男',
            time: '2017-08-09' }, {
            id: 2,
            name: '王五',
            sex: '女',
            time: '2017-08-09' }, {
            id: 3,
            name: '李四',
            sex: '男',
            time: '2017-08-09' }, {
            id: 4,
            name: '楊朝來(lái)',
            sex: '男',
            time: '2017-08-09' }, {
            id: 5,
            name: '蔣平',
            sex: '男',
            time: '2017-08-09' }, {
            id: 6,
            name: '唐燦華',
            sex: '男',
            time: '2017-08-09' }],
        columns: [{
            field: 'id',
            title: '序號(hào)' }, {
            field: 'name',
            title: '姓名',
            editable: {
                type: 'text',  
                validate: function (value) { if ($.trim(value) == '') { return '姓名不能為空!';  
                    }  
                }
            } 
        }, {
            field: 'sex',
            title: '性別',
            editable: {
                type: 'select',
                pk: 1,
                source: [
                    {value: 1, text: '男'},
                    {value: 2, text: '女'},
                ]
            }
        },  {
            field: 'time',
            title: '時(shí)間',
            editable: {
                type: 'date',
                format: 'yyyy-mm-dd',    
                viewformat: 'yyyy-mm-dd',    
                datepicker: {
                    weekStart: 1 }
            } 
        }]
    });
    
        - 
            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
        
- 
            69
        
- 
            70
        
- 
            71
        
- 
            72
        
- 
            73
        
- 
            74
        
- 
            75
        
- 
            76
        
- 
            77
        
- 
            78
        
- 
            79
        
- 
            80
        
- 
            81
        
- 
            82
        
- 
            83
        
- 
            84
        
- 
            85
        
- 
            86
        
- 
            87
        
- 
            88
        
- 
            89
        
- 
            90
        
- 
            91
        
- 
            92
        
- 
            93
        
- 
            94
        
- 
            95
        
- 
            96
        
- 
            97
        
- 
            98
        
- 
            99
        
- 
            100
        
- 
            101
        
        結(jié)果圖如下:
    
    
         
    
    
        由于開(kāi)源,很快就找到原因,由于formatter我們沒(méi)有寫這個(gè)function導(dǎo)致調(diào)用的默認(rèn)的formatter,默認(rèn)的沒(méi)有把表格的值傳入html中,bootstrap-table-editable.js源碼如下,初始定義_dont_edit_formatter為false,我們沒(méi)有實(shí)現(xiàn)noeditFormatter的function就會(huì)執(zhí)行第二個(gè)if語(yǔ)句,其中的標(biāo)簽中沒(méi)有對(duì)內(nèi)容賦值,導(dǎo)致最后顯示結(jié)果為它默認(rèn)的Empty:
    
column.formatter = function(value, row, index) { var result = column._formatter ? column._formatter(value, row, index) : value;
                $.each(column, processDataOptions);
                $.each(editableOptions, function(key, value) {
                    editableDataMarkup.push(' ' + key + '="' + value + '"');
                }); var _dont_edit_formatter = false; if (column.editable.hasOwnProperty('noeditFormatter')) {
                    _dont_edit_formatter = column.editable.noeditFormatter(value, row, index);
                } if (_dont_edit_formatter === false) { return ['<a href="javascript:void(0)"', ' data-name="' + column.field + '"', ' data-pk="' + row[that.options.idField] + '"', ' data-value="' + result + '"',
                        editableDataMarkup.join(''), '>' + '</a>' ].join('');
                } else { return _dont_edit_formatter;
                }
            };
    
        - 
            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
        
        由于要實(shí)現(xiàn)多樣式,則把上面的代碼改變,并在使用的時(shí)候?qū)崿F(xiàn)noeditFormatter:function(value){…}就是了。將上面的代碼改為如下(此為我自己改的,你可以根據(jù)自己的需要做修改):
    
column.formatter = function(value, row, index) { var result = column._formatter ? column._formatter(value, row, index) : value;
                $.each(column, processDataOptions);
                $.each(editableOptions, function(key, value) {
                    editableDataMarkup.push(' ' + key + '="' + value + '"');
                }); var _dont_edit_formatter = false; if (column.editable.hasOwnProperty('noeditFormatter')) { var process = column.editable.noeditFormatter(value, row, index); if(!process.hasOwnProperty('class')){
                        process.class = '';
                    } if(!process.hasOwnProperty('style')){
                        process.style = '';
                    }
                    _dont_edit_formatter = ['<a href="javascript:void(0)"', ' data-name="'+process.filed+'"', ' data-pk="1"', ' data-value="' + process.value + '"', ' class="'+process.class+'" style="'+process.style+'"', '>' + process.value + '</a>' ].join('');
                } if (_dont_edit_formatter === false) { return ['<a href="javascript:void(0)"', ' data-name="' + column.field + '"', ' data-pk="' + row[that.options.idField] + '"', ' data-value="' + result + '"',
                        editableDataMarkup.join(''), '>' + value + '</a>' ].join('');
                } else { return _dont_edit_formatter;
                }
            };
    
        - 
            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
        
        結(jié)果如下:
    
    
         
    
    
         
    
    
        然后是bootstrap table的使用js文件,在其中實(shí)現(xiàn)noeditFormatter函數(shù)。返回的result必須包含filed和value,class和style可以不需要,class可以額外用其它插件之類,比如badge,style是增加樣式(背景,顏色,字體等)。
    
$('#db_dependences').bootstrapTable({
        method:'POST',
        dataType:'json',
        contentType: "application/x-www-form-urlencoded",
        cache: false,
        striped: true,  sidePagination: "client",  showColumns:true,
        pagination:true,
        minimumCountColumns:2,
        pageNumber:1,  pageSize: 10,  pageList: [10, 15, 20, 25],  uniqueId: "id",  showExport: true,                    
        exportDataType: 'all',
        exportTypes:[ 'csv', 'txt', 'sql', 'doc', 'excel', 'xlsx', 'pdf'],  onEditableSave: function (field, row, oldValue, $el) { $.ajax({
                success: function (data, status) { if (status == "success") {
                        alert("編輯成功");
                    }
                },
                error: function () { alert("Error");
                },
                complete: function () { }
            });
        },          data: [{
            id: 1,
            name: '張三',
            sex: '男',
            time: '2017-08-09' }, {
            id: 2,
            name: '王五',
            sex: '女',
            time: '2017-08-09' }, {
            id: 3,
            name: '李四',
            sex: '男',
            time: '2017-08-09' }, {
            id: 4,
            name: '楊朝來(lái)',
            sex: '男',
            time: '2017-08-09' }, {
            id: 5,
            name: '蔣平',
            sex: '男',
            time: '2017-08-09' }, {
            id: 6,
            name: '唐燦華',
            sex: '男',
            time: '2017-08-09' }, {
            id: 7,
            name: '馬達(dá)',
            sex: '男',
            time: '2017-08-09' }, {
            id: 8,
            name: '趙小雪',
            sex: '女',
            time: '2017-08-09' }, {
            id: 9,
            name: '薛文泉',
            sex: '男',
            time: '2017-08-09' }, {
            id: 10,
            name: '丁建',
            sex: '男',
            time: '2017-08-09' }, {
            id: 11,
            name: '王麗',
            sex: '女',
            time: '2017-08-09' }],
        columns: [{
            field: 'id',
            title: '序號(hào)' }, {
            field: 'name',
            title: '姓名',
            editable: {
                type: 'text',  
                validate: function (value) { if ($.trim(value) == '') { return '姓名不能為空!';  
                    }  
                }
            } 
        }, {
            field: 'sex',
            title: '性別',
            editable: {
                type: 'select',
                pk: 1,
                source: [
                    {value: 1, text: '男'},
                    {value: 2, text: '女'},
                ],
                noeditFormatter: function (value,row,index) { var result={filed:"sex",value:value,class:"badge",style:"background:#333;padding:5px 10px;"}; return result;
                }
            }
        },  {
            field: 'time',
            title: '時(shí)間',
            editable: {
                type: 'date',
                format: 'yyyy-mm-dd',    
                viewformat: 'yyyy-mm-dd',    
                datepicker: {
                    weekStart: 1 },
                noeditFormatter: function (value,row,index) { var result={filed:"time",value:value,class:"badge",style:"background:#333;padding:5px 10px;"}; return result;
                }
            } 
        }]
    });
    
        - 
            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
        
- 
            69
        
- 
            70
        
- 
            71
        
- 
            72
        
- 
            73
        
- 
            74
        
- 
            75
        
- 
            76
        
- 
            77
        
- 
            78
        
- 
            79
        
- 
            80
        
- 
            81
        
- 
            82
        
- 
            83
        
- 
            84
        
- 
            85
        
- 
            86
        
- 
            87
        
- 
            88
        
- 
            89
        
- 
            90
        
- 
            91
        
- 
            92
        
- 
            93
        
- 
            94
        
- 
            95
        
- 
            96
        
- 
            97
        
- 
            98
        
- 
            99
        
- 
            100
        
- 
            101
        
- 
            102
        
- 
            103
        
- 
            104
        
- 
            105
        
- 
            106
        
- 
            107
        
- 
            108
        
- 
            109
        
- 
            110
        
- 
            111
        
- 
            112
        
- 
            113
        
- 
            114
        
- 
            115
        
- 
            116
        
- 
            117
        
- 
            118
        
- 
            119
        
- 
            120
        
- 
            121
        
- 
            122
        
- 
            123
        
- 
            124
        
- 
            125
        
- 
            126
        
- 
            127
        
- 
            128
        
- 
            129
        
- 
            130
        
- 
            131
        
- 
            132
        
- 
            133
        
- 
            134
        
- 
            135
        
- 
            136
        
- 
            137
        
- 
            138
        
- 
            139
        
- 
            140
        
- 
            141
        
- 
            142
        
- 
            143
        
        關(guān)于bootstrap table的導(dǎo)出及使用可以看我另外一篇博客。
    
    
        
            下載和引用
        
    
    
        下載x-editable,并如下引用。
    
<link href="js/bootstrap_above/x-editable-develop/dist/bootstrap-editable/css/bootstrap-editable.css" rel="stylesheet"> <script src="js/bootstrap_above/x-editable-develop/dist/bootstrap-editable/js/bootstrap-editable.js"></script> <script src="js/bootstrap_above/bootstrap-table-develop/dist/extensions/editable/bootstrap-table-editable.js"></script>
    
    
        然后講上訴的一些文件修改添加,就完成了。
    
    
        
            另外項(xiàng)目的結(jié)果展示
        
    
    
         
    
    
         
    
    
        其中的樣式都是自行在x-editable的基礎(chǔ)上添加的。如配置出問(wèn)題,以下是源碼鏈接。
    
    
        藍(lán)藍(lán)設(shè)計(jì)( m.monmeltingpot.net )是一家專注而深入的界面設(shè)計(jì)公司,為期望卓越的國(guó)內(nèi)外企業(yè)提供卓越的UI界面設(shè)計(jì)、BS界面設(shè)計(jì) 、 cs界面設(shè)計(jì) 、 ipad界面設(shè)計(jì) 、 包裝設(shè)計(jì) 、 圖標(biāo)定制 、 用戶體驗(yàn) 、交互設(shè)計(jì)、 網(wǎng)站建設(shè) 、平面設(shè)計(jì)服務(wù)。