1. 程式人生 > 實用技巧 >bootstrap table中使用bootstrap-swich

bootstrap table中使用bootstrap-swich

這個外掛有坑!!!

載入需要檔案

<script src="~/Scripts/bootstrap-switch.js"></script>
<link href="~/Content/bootstrap-switch.css" rel="stylesheet" />

bootstrap-switch元件初始化一直是關閉狀態,bootstrap-switch開關元件簡單夠用,作為bs的擴充套件元件經常被使用,不過在github下載的程式碼有個bug,就是不論你的input是否添加了checked屬性,渲染出來的元件都是關閉狀態,也就是無法根據你程式碼裡的checked進行正確顯示。解決方案很簡單,到bootstrap-switch.js(或min.js)原始碼檔案,將this.ope替換為this.options.state即可

在 bootstraptable中載入時進行初始化,還有客戶端分頁的話,翻頁時會失效,所以pagechange時也要初始化下

columns: [{
                checkbox: true
            }, {
                field: 'ProductName',
                title: '名稱',
                editable: {
                    type: 'text',
                    title: '名稱',
                    validate: function (v) {
                        if (!v) return '名稱不能為空';
                    }
                }
                //formatter: nameFormatter
            }, {
                field: 'ProductDescription',
                title: '描述',
                editable: {
                    type: 'text',
                    title: '描述',
                    validate: function (v) {
                        if (!v) return '描述不能為空';
                    }
                }
            }, {
                field: 'Integral',
                title: '積分',
                editable: {
                    type: 'text',
                    title: '積分',
                    validate: function (v) {
                        if (!v) return '積分不能為空';
                    }
                }
            }, {
                field: 'Pic',
                title: '圖片',
                formatter: function (value, row, index) {
                    return "<img style='width: 50px;height: 50px;' src='/ProductPic/" + value + "' alt=''>"
                }
            }, {
                field: 'Status',
                title: '狀態',
                formatter: project_status
                }],
            onLoadSuccess: function (data) {
                initSwitch();
            },
            onPageChange: function (data) {
                initSwitch();
            }

  

function initSwitch() {
   
        $("[id='project_status_switch']").bootstrapSwitch({
            onText: "啟用",      // 設定ON文字  
            offText: "停用",    // 設定OFF文字   
            onColor: "success",// 設定ON文字顏色(info/success/warning/danger/primary)
            offColor: "danger", // 設定OFF文字顏色 (info/success/warning/danger/primary)  */
            size: "mini",    // 設定控制元件大小,從小到大  (mini/small/normal/large)  
            // 當開關狀態改變時觸發  
            onSwitchChange: function (event, state) {
                var step = this.value;

                if (state == true) {
                    editEnable(step, state);
                } else {
                    editEnable(step, state);
                }
            }
        });
    }

  

//改變狀態
function editEnable(editId, isEnable) { $.ajax({ url: "/Manage/PStatusChange", data: { id: editId, isEnable: isEnable }, type: "POST", dataType: "JSON", success: function (data) { result = $.parseJSON(data); if (result.success == true) { //alert("上傳成功"); RefreshData(); } else { alert(result.errorMsg); } } }); }

  

function project_status(value, row, index) {
        if (value == true) {
            return "<input type='checkbox' id='project_status_switch'  name='mycheck' value='" + row.Id + "'checked>";
        } else {
            return "<input type='checkbox' id='project_status_switch' name='mycheck' value='" + row.Id + "'>";
        }
 }

  


參考部落格:     https://www.cnblogs.com/gcgc/p/11195206.html https://blog.csdn.net/weixin_30709929/article/details/98959496?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-3&spm=1001.2101.3001.4242 https://blog.csdn.net/wanghui_0924/article/details/84975106?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-2&spm=1001.2101.3001.4242     https://blog.csdn.net/weixin_43691098/article/details/101364153