将属性添加到复选框Backgrid

时间:2016-02-25 06:11:27

标签: javascript jquery backgrid

大家好我正在使用backgrid来渲染我的表格,偶然也有一些复选框。现在我想为这些复选框添加2个属性,但我无法弄清楚如何做到这一点。有人能告诉我怎样才能实现它。谢谢

HTML

<div id="dayResult" class="backgrid-container" style="width: 139PX; height: initial; "></div>

JS

var grid1 = new Backgrid.Grid({
  columns: [ {
    name: "name",
    label: "",
    cell: "string"
  },{
    // name is a required parameter, but you don't really want one on a select all column
    name: " ",
    // Backgrid.Extension.SelectRowCell lets you select individual rows
    cell: "select-row",
    // Backgrid.Extension.SelectAllHeaderCell lets you select all the row on a page
    // headerCell: "select-all",

  }],

  collection: new Backbone.Collection([
    {"name": "Monday"},
    {"name": "Tuesday"},
    {"name": "Wednesday"},
    {"name": "Thursday"},
    {"name": "Friday"},
    {"name": "Saturday"},
    {"name": "Sunday"}
  ])
});

$("#dayResult").append(grid1.render().el);

包含的文件
  

CSS

<link href="../assets/css/backgrid.css" rel="stylesheet" />
    <link href="../assets/plugins/backgrid-selectall/css/backgrid-select-all.css" rel="stylesheet" />

    <link href="../assets/plugins/switchery/switchery.min.css" rel="stylesheet" />
  

JS

<script src="https://cdn.rawgit.com/jashkenas/underscore/1.5.2/underscore.js"></script>
    <script src="https://cdn.rawgit.com/jashkenas/backbone/1.1.0/backbone.js"></script>
    <script src="https://cdn.rawgit.com/wyuenho/backbone-pageable/master/lib/backbone-pageable.js"></script>
<script src="https://cdn.rawgit.com/wyuenho/backgrid/master/lib/backgrid.js"></script>

    <script src="./assets/plugins/backgrid-selectall/js/backgrid-select-all.js" ></script>


<script src="./assets/plugins/switchery/switchery.js"></script>

CDN网址

Switchery 
   https://cdn.rawgit.com/abpetkov/switchery/master/switchery.css
   https://cdn.rawgit.com/abpetkov/switchery/master/switchery.js

Backgrid
    https://cdn.rawgit.com/wyuenho/backgrid/master/lib/backgrid.css
    https://cdn.rawgit.com/wyuenho/backgrid-select-all/master/backgrid-select-all.css
    https://cdn.rawgit.com/wyuenho/backgrid/master/lib/backgrid.js
    https://cdn.rawgit.com/wyuenho/backgrid-select-all/master/backgrid-select-all.js

1 个答案:

答案 0 :(得分:0)

您需要扩展复选框列单元格的render()方法。

我做了类似的事情,为我的复选框图片添加属性。我不使用“选择行”方法,但我相信你可以像这样扩展它:

   cell: Backgrid.Cell.extend({
    className: '4YearDegree',
    render: function() {
        var degree = this.model.get("Degree");
        var src = '';

        var altText = '';
        if (degree) {                
            src = '../Content/Images/enabled_checkmark.png'; //display enabled checkmark image 
            altText = 'is checked';
            } else {
            src = '../Content/Images/disabled_checkmark.png'; //display disabled checkmark image 
            altText = 'is unchecked';
            }

        this.$el.html($('<img>').attr('src', src).attr('alt', altText));
        return this;
        }
    })

上面代码的关键部分是这一行:

this.$el.html($('<img>').attr('src', src).attr('alt', altText));

我用它将“altText”附加到我的复选标记图像上。

尝试以这种方式添加自己的自定义属性。它可以/应该工作。