Bootstrap Table使用整理(三)

来源:互联网 发布:python 矩阵相乘 编辑:程序博客网 时间:2024/06/06 02:24

一、单元格内容格式化 

[javascript] view plain copy
 print?
  1. /* 
  2. * data-formatter 扩展处理单元格内容 
  3. */  
  4. $('#table1').bootstrapTable({  
  5.     columns: [  
  6.         {  
  7.             field: 'sno', title: '编号', formatter: function (value, row, index) {  
  8.                 //return index + 1;  
  9.                 return '<span class="badge">'+(index+1)+'</span>';  
  10.             }  
  11.         },  
  12.         {  
  13.             field: 'sno', title: '学生编号', formatter: function (value) {  
  14.                 return '<a  href="#">'+ value + '</a>';  
  15.             }  
  16.         },  
  17.         { field: 'sname', title: '学生姓名' },  
  18.         {  
  19.             field: 'ssex', title: '性别', formatter: function (value) {  
  20.                 return '<i class="glyphicon  glyphicon-star"></i>' + value;  
  21.             }  
  22.         },  
  23.         { field: 'sbirthday', title: '生日' },  
  24.         { field: 'class', title: '课程编号' },  
  25.     ],  
  26.     url:'@Url.Action("GetStudent","DataOne")'  
  27. });  
[html] view plain copy
 print?
  1. <table id="table1"  
  2.        data-classes="table table-hover table-condensed"></table>  


二、列显示控制,CardView面板显示

[javascript] view plain copy
 print?
  1. /* 
  2. * data-show-columns 设置是否开启显示列,默认为false 
  3. * data-switchable 设置是否参与显示隐藏 
  4. * data-visible 设置默认是否显示 
  5. * data-height 设置table表格固定高度 
  6. * data-card-view 设置table 的显示方式是否是card view 
  7. */  
  8. var $table=  $('#table1').bootstrapTable({  
  9.     columns: [  
  10.         { field: 'sno', title: '学生编号', switchable: false },  
  11.         { field: 'sname', title: '学生姓名' },  
  12.         { field: 'ssex', title: '性别' },  
  13.         { field: 'sbirthday', title: '生日' },  
  14.         { field: 'class', title: '课程编号', visible:false },  
  15.     ],  
  16.     url:'@Url.Action("GetStudent","DataOne")'  
  17. });  
[html] view plain copy
 print?
  1. <table id="table1"  
  2.        data-classes="table table-hover"  
  3.        data-show-columns="true"  
  4.        data-height="300"  
  5.        data-card-view="true"></table>  


三、单选处理 -radio

[javascript] view plain copy
 print?
  1. /* 
  2. * data-click-to-select 设置行点击是否选中 
  3. * data-select-item-name 设置选中项的值 
  4. * data-radio 设置列是否显示为radio单选框 
  5. * click-row.bs.table 绑定行点击事件 
  6. * getData 获取指定索引的行数据对象 
  7. */  
  8. var $table= $('#table1').bootstrapTable({  
  9.     columns: [  
  10.         { field: 'sno', title: '学生编号' ,radio:true},  
  11.         { field: 'sname', title: '学生姓名' },  
  12.         { field: 'ssex', title: '性别' },  
  13.         { field: 'sbirthday', title: '生日' },  
  14.         { field: 'class', title: '课程编号' },  
  15.     ],  
  16.     url:'@Url.Action("GetStudent","DataOne")'  
  17. });  
  18. $table.on('click-row.bs.table'function (e, row, $element) {  
  19.     $('.success').removeClass('success');  
  20.     $($element).addClass('success');  
  21. });  
  22. $('#btn1').click(function () {  
  23.     //获取选中行索引  
  24.     var index = $table.find('tr.success').data('index');  
  25.     //获取选中行数据对象  
  26.     var result = $table.bootstrapTable('getData')[index];  
  27.     console.info(result);  
  28.     alert(result.sname);  
  29. });  
[html] view plain copy
 print?
  1. <button class="btn btn-primary" id="btn1">获取表格选中结果</button>  
  2. <table id="table1"  
  3.        data-classes="table table-hover"  
  4.        data-show-columns="true"  
  5.        data-click-to-select="true"  
  6.        data-select-item-name="sno"></table>  


四、多选处理-checkbox

[javascript] view plain copy
 print?
  1. /* 
  2. * data-click-to-select 设置行点击是否选中 
  3. * data-checkbox 设置列为多选框,特别说明:设置为checkbox的列不能绑定字段,否则全为选中状态 
  4. * formatter 中返回对象的diabled属性控制是否禁用多选框,checked属性控制当前是否被选中 
  5. */  
  6. var $table= $('#table1').bootstrapTable({  
  7.     columns: [  
  8.         {  
  9.             fileid: 'state', checkbox: true, formatter: function (value, row, index) {  
  10.                 if (index === 2) {  
  11.                     return {  
  12.                         disabled: true,  
  13.                         checked:true  
  14.                     }  
  15.                 }  
  16.                 if (index === 0) {  
  17.                     return {  
  18.                         disabled: true,  
  19.                         checked: true  
  20.                     }  
  21.                 }  
  22.                 console.info(value);  
  23.                 return value;  
  24.             }  
  25.         },  
  26.         {  
  27.             field: 'sno', title: '学生编号'  
  28.         },  
  29.         { field: 'sname', title: '学生姓名' },  
  30.         { field: 'ssex', title: '性别' },  
  31.         { field: 'sbirthday', title: '生日' },  
  32.         { field: 'class', title: '课程编号' },  
  33.     ],  
  34.     url:'@Url.Action("GetStudent","DataOne")'  
  35. });  
  36. $table.on('click-row.bs.table'function (e, row, $element) {  
  37.     $('.success').removeClass('success');  
  38.     $($element).addClass('success');  
  39. });  
  40. $('#btn1').click(function () {  
  41.     //获取选中结果行,返回数组  
  42.     //返回结果中包括多选框字段 state=true  
  43.     var result = $table.bootstrapTable('getSelections');  
  44.     console.info(result);  
  45.     var ids = [];  
  46.     for (var i = 0; i < result.length; i++) {  
  47.         var item = result[i];  
  48.         ids.push(item.sno);  
  49.     }  
  50.     alert(ids);  
  51. });  
[html] view plain copy
 print?
  1. <button class="btn btn-primary" id="btn1">获取表格选中结果</button>  
  2. <table id="table1"  
  3.        data-classes="table table-hover"  
  4.        data-show-columns="true"  
  5.        data-click-to-select="true"  
  6.       ></table>  

五、多选框单选模式

[html] view plain copy
 print?
  1. <button class="btn btn-primary" id="btn1">获取表格选中结果</button>  
  2. <table id="table1"  
  3.        data-classes="table table-hover"  
  4.        data-show-columns="true"  
  5.        data-click-to-select="true"  
  6.        data-single-select="true"  
  7.        >  
  8.     <thead>  
  9.         <tr>  
  10.             <th data-field="state" data-checkbox="true" data-formatter="checkHandle">选择框</th>  
  11.             <th data-field="sno" >编号</th>  
  12.             <th data-field="sname">姓名</th>  
  13.             <th data-field="ssex">性别</th>  
  14.             <th data-field="sbirthday">生日</th>  
  15.             <th data-field="class">课程编号</th>  
  16.         </tr>  
  17.     </thead>  
  18. </table>  
[javascript] view plain copy
 print?
  1. /* 
  2. * data-click-to-select 设置行点击是否选中 
  3. * data-checkbox 设置列为多选框 
  4. * data-formatter 中返回对象的diabled属性控制是否禁用多选框,checked属性控制当前是否被选中 
  5. * data-single-select 指定单选模式,即使使用多选框也是单选模式 
  6. */  
  7. var $table= $('#table1').bootstrapTable({  
  8.     url:'@Url.Action("GetStudent","DataOne")'  
  9. });  
  10.   
  11. $table.on('click-row.bs.table'function (e, row, $element) {  
  12.     $('.success').removeClass('success');  
  13.     $($element).addClass('success');  
  14. });  
  15. $('#btn1').click(function () {  
  16.     //获取选中结果行,返回数组  
  17.     //返回结果中包括多选框字段 state=true  
  18.     var result = $table.bootstrapTable('getSelections');  
  19.     console.info(result);  
  20.     var ids = [];  
  21.     for (var i = 0; i < result.length; i++) {  
  22.         var item = result[i];  
  23.         ids.push(item.sno);  
  24.     }  
  25.     alert(ids);  
  26. });  
  27. function checkHandle(value, row, index) {   
  28.     if (index === 2) {  
  29.         return {  
  30.             disabled: true  
  31.         };  
  32.     }  
  33.     if (index === 0) {  
  34.         return {  
  35.             disabled: true,  
  36.             checked: true  
  37.         }  
  38.     }  
  39.     return value;  
  40. }  
原创粉丝点击