template-items-select2.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var TemplateItemsSelect2 = {
  2. template: '<div style="height: 100%;">\
  3. <div class="title-name c-f16">\
  4. <span class="c-vam mr15">项目表</span><img @click="goBack" src="../../../images/fanhui02_icon.png" class="c-vam"/>\
  5. </div>\
  6. <div class="search-warp">\
  7. <input class="inp-search" v-model="searchName" placeholder="搜索项目名称" @input="debounceSearch"/>\
  8. </div>\
  9. <div class="mt10 pt10 pl10 ul-wrap">\
  10. <ul class="level-third">\
  11. <li v-for="(o, k) in serviceItems" class="ptb5">\
  12. <div class="list-item-name c-nowrap mr5" :class="{\'is-active\': !o.enable}">{{o.name}}</div>\
  13. <a href="javascript:void(0);" @click="addItem(o,k)" class="add-item-btn" :class="{\'disabled\': !o.enable}">\
  14. 添加\
  15. </a>\
  16. </li>\
  17. </ul>\
  18. </div>\
  19. </div>',
  20. props: [],
  21. data: function() {
  22. return {
  23. isNew: null,
  24. searchName: "",
  25. serviceItems: [],
  26. templateDetail: []
  27. }
  28. },
  29. beforeRouteEnter: function(to, from, next) {
  30. next(function(vm) {
  31. var query = vm.$route.query
  32. if(vm.isNew == true || vm.isNew === "true") {
  33. vm.isNew = true
  34. } else {
  35. vm.isNew = false
  36. }
  37. vm.templateId = query.templateId
  38. if((to.path == "/template-item-detail" || from.path == "/template-item-detail" ) && from.path != "/") {
  39. return
  40. }
  41. if(!vm.isNew) {
  42. vm.loading.show()
  43. vm.findTemplateDetail().then(function(res) {
  44. vm.loading.close()
  45. if(res.status == 200) {
  46. vm.templateDetail = res.data
  47. vm.getServiceItemList().then(function(){
  48. _.each(vm.templateDetail, function(o) {
  49. EventBus.$emit("add-template-item", o)
  50. })
  51. })
  52. } else {
  53. toastr && toastr.error(res.msg)
  54. }
  55. })
  56. } else {
  57. vm.getServiceItemList()
  58. }
  59. })
  60. },
  61. created: function() {
  62. },
  63. mounted: function() {
  64. var vm = this
  65. EventBus.$on("delete-template-item", function(obj, i){
  66. _.each(vm.serviceItems, function(item, index) {
  67. if(item.code==obj.code){
  68. item.enable = true
  69. vm.$set(vm.serviceItems, index, item)
  70. vm.templateDetail.splice(i, 1)
  71. }
  72. })
  73. })
  74. },
  75. methods: {
  76. goBack: function() {
  77. this.$router.replace('/');
  78. },
  79. addItem: function(o, k) {
  80. if(!o.enable){
  81. return
  82. }
  83. o.enable = false
  84. EventBus.$emit("add-template-item", o)
  85. this.$set(this.serviceItems, k, o)
  86. this.templateDetail.push(o)
  87. },
  88. debounceSearch: _.debounce(function() {
  89. var vm = this
  90. vm.getServiceItemList()
  91. }, 500, false),
  92. getServiceItemList: function(){
  93. var vm = this
  94. return rehaAPI.getServiceItemList({
  95. name: vm.searchName
  96. }).then(function(res) {
  97. vm.serviceItems = res.data
  98. var selected = vm.templateDetail
  99. var selectedIds = _.pluck(selected, "code")
  100. _.each(vm.serviceItems, function(o) {
  101. if(_.contains(selectedIds, o.code)) {
  102. o.enable = false
  103. } else {
  104. o.enable = true
  105. }
  106. })
  107. })
  108. },
  109. findTemplateDetail: function() {
  110. var vm = this
  111. return rehaAPI.findTemplateDetail({
  112. templateId: vm.templateId
  113. }).then(function(res) {
  114. return res
  115. })
  116. },
  117. }
  118. }