BootStrap-table 复选框 问题

来源:互联网 发布:淘宝泰州医药城地址 编辑:程序博客网 时间:2024/05/16 04:51

添加以下中的红色代码可实现,部分复选框默认勾选。

$(function() {
    $('.bs-example').each(function() {
        var source = $('<div></div>').text($(this).html()).html(),
            sources = source.split('\n'),
            codes = [],
            spaces = 0;


        $.each(sources, function(i, text) {
            if (!$.trim(text)) {
                return;
            }
            if (!spaces) {
                spaces = text.match(/(^\s+)/)[1].length;
            }
            codes.push(text.substring(spaces));
        });
        $(this).next().find('code').html(codes.join('\n'));
    });
    
    var $table = $("#table");
    selections = [];
    initTable();
    
    $("#select").click(function(){
    var ids = getIdSelections();
    alert(ids);
    })
    
   function getIdSelections() {
        return $.map($table.bootstrapTable('getSelections'), function (row) {
            return row.name;
        });
    }
    
    function initTable(){
    $table.bootstrapTable({
    columns:[ {
                field: 'state',
                checkbox: true,
                formatter:stateFormatter
            },{
                field: 'id',
                title: 'Item ID',
                sortable: true,
                editable: true,
                align: 'center',
                formatter:function(value, row, index){
                return value;
                }
            },  {
                field: 'name',
                title: 'Item Name',
                sortable: true,
                editable: true,
                align: 'center'
            },  {
                field: 'price',
                title: 'Item Price',
                sortable: true,
                editable: true,
                align: 'center'
            }]
    })
    }
    
  /*  $table.find('input[name="btSelectItem"]').each(function(){
    var value = $(this).attr('data-index');
    alert(value);
    })*/
    function stateFormatter(value, row, index) {
        if (row.id === 2) {
            return {
                disabled: true,
                checked: true
            };
        }
        if (row.id === 0) {
            return {
                disabled: true,
                checked: true
            }
        }
        return value;
    }

});

1 0