酒店预订平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

115 line
5.7 KiB

  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {
  2. var Controller = {
  3. index: function () {
  4. // 初始化表格参数配置
  5. Table.api.init({
  6. extend: {
  7. index_url: 'example/tabletemplate/index',
  8. add_url: '',
  9. edit_url: '',
  10. del_url: 'example/tabletemplate/del',
  11. multi_url: '',
  12. }
  13. });
  14. var table = $("#table");
  15. Template.helper("Moment", Moment);
  16. // 初始化表格
  17. table.bootstrapTable({
  18. url: $.fn.bootstrapTable.defaults.extend.index_url,
  19. templateView: true,
  20. columns: [
  21. [
  22. {field: 'state', checkbox: true, },
  23. {field: 'id', title: 'ID', operate: false},
  24. //直接响应搜索
  25. {field: 'username', title: __('Username'), formatter: Table.api.formatter.search},
  26. //模糊搜索
  27. {field: 'title', title: __('Title'), operate: 'LIKE %...%', placeholder: '模糊搜索,*表示任意字符', style: 'width:200px'},
  28. //通过Ajax渲染searchList
  29. {field: 'url', title: __('Url'), align: 'left', formatter: Controller.api.formatter.url},
  30. //点击IP时同时执行搜索此IP,同时普通搜索使用下拉列表的形式
  31. {field: 'ip', title: __('IP'), searchList: ['127.0.0.1', '127.0.0.2'], events: Controller.api.events.ip, formatter: Controller.api.formatter.ip},
  32. //browser是一个不存在的字段
  33. //通过formatter来渲染数据,同时为它添加上事件
  34. {field: 'browser', title: __('Browser'), operate: false, events: Controller.api.events.browser, formatter: Controller.api.formatter.browser},
  35. //启用时间段搜索
  36. {field: 'createtime', title: __('Create time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
  37. {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  38. ],
  39. ],
  40. //禁用默认搜索
  41. search: false,
  42. //启用普通表单搜索
  43. commonSearch: false,
  44. //可以控制是否默认显示搜索单表,false则隐藏,默认为false
  45. searchFormVisible: false,
  46. //分页大小
  47. pageSize: 12
  48. });
  49. // 为表格绑定事件
  50. Table.api.bindevent(table);
  51. //指定搜索条件
  52. $(document).on("click", ".btn-toggle-view", function () {
  53. var options = table.bootstrapTable('getOptions');
  54. table.bootstrapTable('refreshOptions', {templateView: !options.templateView});
  55. });
  56. //点击详情
  57. $(document).on("click", ".btn-detail[data-id]", function () {
  58. Backend.api.open('example/bootstraptable/detail/ids/' + $(this).data('id'), __('Detail'));
  59. });
  60. //获取选中项
  61. $(document).on("click", ".btn-selected", function () {
  62. //在templateView的模式下不能调用table.bootstrapTable('getSelections')来获取选中的ID,只能通过下面的Table.api.selectedids来获取
  63. Layer.alert(JSON.stringify(Table.api.selectedids(table)));
  64. });
  65. },
  66. add: function () {
  67. Controller.api.bindevent();
  68. },
  69. edit: function () {
  70. Controller.api.bindevent();
  71. },
  72. api: {
  73. bindevent: function () {
  74. Form.api.bindevent($("form[role=form]"));
  75. },
  76. formatter: {
  77. url: function (value, row, index) {
  78. return '<div class="input-group input-group-sm" style="width:250px;"><input type="text" class="form-control input-sm" value="' + value + '"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a></span></div>';
  79. },
  80. ip: function (value, row, index) {
  81. return '<a class="btn btn-xs btn-ip bg-success"><i class="fa fa-map-marker"></i> ' + value + '</a>';
  82. },
  83. browser: function (value, row, index) {
  84. //这里我们直接使用row的数据
  85. return '<a class="btn btn-xs btn-browser">' + row.useragent.split(" ")[0] + '</a>';
  86. }
  87. },
  88. events: {
  89. ip: {
  90. 'click .btn-ip': function (e, value, row, index) {
  91. var options = $("#table").bootstrapTable('getOptions');
  92. //这里我们手动将数据填充到表单然后提交
  93. $("#commonSearchContent_" + options.idTable + " form [name='ip']").val(value);
  94. $("#commonSearchContent_" + options.idTable + " form").trigger('submit');
  95. Toastr.info("执行了自定义搜索操作");
  96. }
  97. },
  98. browser: {
  99. 'click .btn-browser': function (e, value, row, index) {
  100. Layer.alert("该行数据为: <code>" + JSON.stringify(row) + "</code>");
  101. }
  102. }
  103. }
  104. }
  105. };
  106. return Controller;
  107. });