template.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * Created by Maple on 2016/6/5.
  3. */
  4. /**
  5. * 弹出模板窗口 并插入数据到Id 为 TargetIds的元素中去
  6. * @param type 对应于数据库中 template.type 字段 模板类型
  7. * @param targetIds 需要插入对象的Id数据
  8. * @param callback 回调函数 将会回传用户选择的数据
  9. */
  10. function template(type,targetIds,callback){
  11. var templateModalId = type+"-template";
  12. var options = {
  13. templateModalId:type+"-template",
  14. type:type,
  15. container:{},
  16. targetIds:targetIds,
  17. callback:callback
  18. };
  19. createModal(options);
  20. //callback('ok');
  21. }
  22. function templateItemSelectedEvent(options){
  23. }
  24. function modalCloseEvent(options){
  25. options.container.on('hidden.bs.modal', function (e) {
  26. options.callback(null);
  27. });
  28. }
  29. function createModal(options){
  30. //var templateModalId = type+"-template";
  31. var templateModal = $("#"+options.templateModalId);
  32. if(templateModal.length==0){ //不存在Modal 那么创建一个
  33. var $templateModal = $(
  34. "<div class='modal fade' id='"+options.templateModalId+"' tabindex='-1' role='dialog' aria-labelledby='myModalLabel'>"+
  35. "<div class='modal-dialog modal-lg' role='document'>"+
  36. "<div class='modal-content'>"+
  37. "<div class='modal-header'>"+
  38. "<button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button>"+
  39. "<h4 class='modal-title' id='edit-admin-modal-title'>Test</h4>" +
  40. "</div>"+
  41. "<div class='modal-body' style='padding-bottom:45px'> "+
  42. "</div>" +
  43. "<div class='modal-footer'>"+
  44. "<button type='button' class='btn btn-info btn-lg' data-dismiss='modal'>关闭</button>"+
  45. "</div>"+
  46. "</div>" +
  47. "</div>" +
  48. "</div>");
  49. options.container = $templateModal;
  50. renderModalBody(options);
  51. $("body").append($templateModal);
  52. }
  53. templateModal = $("#"+options.templateModalId);
  54. options.container = templateModal;
  55. templateModal.modal('show');
  56. }
  57. function renderModalBody(options){
  58. var modalContainer = options.container;
  59. var modalBody = modalContainer.find(".modal-body");
  60. var tableId = options.type+"-table";
  61. var $modalTable = $("<table id='"+tableId+"' class='table table-hover table-striped table-bordered table-condensed'>"+
  62. "</table>");
  63. modalBody.append($modalTable);
  64. $(function(){
  65. $.get("/TQE/admin/template/getInfo",
  66. {type:options.type},
  67. function(data){
  68. if(data.success){
  69. var modalTable = renderTable(data.item);
  70. insertBtnClickEvent(modalTable,$modalTable,options);
  71. }else{
  72. alert(data.message);
  73. }
  74. });
  75. });
  76. }
  77. /**
  78. * 插入按钮返回该行数据
  79. */
  80. function insertBtnClickEvent(modalTable,$modalTable,options){
  81. $modalTable.find('tbody').on( 'click', 'button', function () {
  82. var data = modalTable.row( $(this).parents('tr') ).data();
  83. if(data){
  84. if(data[data.length-1]==null){
  85. data.pop();
  86. }
  87. if(options.callback){
  88. options.callback(data); //调用回调函数
  89. }
  90. if(options.targetIds){
  91. var ids = options.targetIds.split(',');
  92. if(ids){
  93. for(var i=0;i<ids.length;i++){
  94. if(ids[i] && data[i]){
  95. $("#"+ids[i]).val(data[i]);
  96. }
  97. }
  98. }
  99. }
  100. options.container.modal('hide');
  101. }
  102. } );
  103. }
  104. /**
  105. * 渲染 模板 表格
  106. */
  107. function renderTable(template){
  108. var columns = template.columns;
  109. var items = template.items;
  110. var dataArr = [];
  111. var columnArr = [];
  112. for(var i=0;i<items.length;i++){
  113. if(items[i] && items[i].values ){
  114. var data = items[i].values;
  115. data.push(null); // null for operation
  116. dataArr.push(data);
  117. }
  118. }
  119. for(i=0;i<columns.length;i++){
  120. var d = {"title":""};
  121. d.title = columns[i];
  122. columnArr.push(d);
  123. }
  124. columnArr.push({title:'操作',width:"80px"});
  125. var options = $.extend(true,{},dataTableDefaultOptions,{
  126. columns: columnArr,
  127. data:dataArr,
  128. autoWidth: false, //禁用自动调整列宽
  129. columnDefs: [ {
  130. "targets": -1,
  131. "data": null,
  132. "width":"80px",
  133. "defaultContent": "<button class='btn btn-primary'>插入</button>"
  134. } ]
  135. });
  136. return $("#"+template.type+"-table").DataTable(options);
  137. }