answer.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <!-- アンケート回答集計結果 -->
  2. <template>
  3. <div class="app-container">
  4. <div v-if="loading">
  5. </div>
  6. <div v-else>
  7. <div class="survey-title">アンケート</div>
  8. <div class="deadline">
  9. 回答締切:{{ formatDate(surveyData.publicEndTime || '無し') }}
  10. </div>
  11. <!-- 動的レンダリング問題リスト -->
  12. <div v-for="(question, index) in surveyQuestions" :key="question.surveyId + index" class="question">
  13. <span class="question-number">Q{{ question.sortOrder }}.</span>
  14. <span class="question-text">{{ question.questionText }}</span>
  15. <!-- テキストボックス(タイプ1:50文字制限) -->
  16. <div v-if="question.questionType === '1'" class="text-area">
  17. <textarea
  18. rows="3"
  19. :maxlength="50"
  20. v-model="question.answerText"
  21. disabled
  22. ></textarea>
  23. </div>
  24. <!-- テキストボックス(タイプ2:100字制限) -->
  25. <div v-if="question.questionType === '2'" class="text-area">
  26. <textarea
  27. rows="3"
  28. :maxlength="100"
  29. v-model="question.answerText"
  30. disabled
  31. ></textarea>
  32. </div>
  33. <!-- チェックボックス(タイプ3) -->
  34. <div v-if="question.questionType === '3'" class="options">
  35. <label v-for="option in parsedOptions(question)" :key="option.optionId" class="option-item">
  36. <input
  37. type="checkbox"
  38. :checked="option.isAnswered"
  39. @change="option.isAnswered = !option.isAnswered"
  40. disabled
  41. >
  42. {{ option.optionText }}
  43. </label>
  44. </div>
  45. <!-- ラジオボタン(タイプ4) -->
  46. <div v-if="question.questionType === '4'" class="options">
  47. <label v-for="option in parsedOptions(question)" :key="option.optionId" class="option-item">
  48. <input
  49. type="radio"
  50. :name="'q' + question.sortOrder"
  51. :checked="option.isAnswered"
  52. @change="handleRadioChange(question, option)"
  53. disabled
  54. >
  55. {{ option.optionText }}
  56. </label>
  57. </div>
  58. <!-- ドロップダウンボックス(タイプ5) -->
  59. <div v-if="question.questionType === '5'" class="dropdown">
  60. <select v-model="question.selectedOptionId" disabled>
  61. <option value="">選択してください</option>
  62. <option
  63. v-for="option in parsedOptions(question)"
  64. :key="option.optionId"
  65. :value="option.optionId"
  66. >
  67. {{ option.optionText }}
  68. </option>
  69. </select>
  70. </div>
  71. </div>
  72. <button class="back-button" @click="goBack">戻る</button>
  73. </div>
  74. </div>
  75. </template>
  76. <script setup>
  77. import { ref, onMounted } from 'vue'
  78. import { listSurveyAnswer } from "@/api/fcbi/survey.js"
  79. import { useRouter } from "vue-router";
  80. import { useRoute } from 'vue-router'
  81. import useTagsViewStore from '@/store/modules/tagsView'
  82. // アンケートデータと状態管理
  83. const surveyData = ref({ publicEndTime: '' })
  84. // アンケートコード(戻るページに渡す)
  85. const surveyCode = ref('')
  86. const surveyQuestions = ref([])
  87. const loading = ref(true)
  88. const router = useRouter();
  89. const route= useRoute()
  90. const tagsViewStore = useTagsViewStore()
  91. const currentRoute = router.currentRoute.value
  92. const surveyId = route.params.surveyId
  93. // アンケートデータをAPIから取得
  94. const loadSurveyData = async () => {
  95. try {
  96. const response = await listSurveyAnswer({ surveyId: surveyId })
  97. if (response.code === 200) {
  98. surveyData.value = { publicEndTime: response.rows[0].publicEndTime || '' }
  99. surveyCode.value = response.rows[0].surveyCode || ''
  100. surveyQuestions.value = processSurveyQuestions(response.rows)
  101. }
  102. } catch (error) {
  103. console.error('データ取得失敗:', error)
  104. } finally {
  105. loading.value = false
  106. }
  107. }
  108. // 質問リストをソートし、初期値を設定
  109. const processSurveyQuestions = (data) => {
  110. return data?.map(question => {
  111. const selectedOptionId = question.questionType === '5'
  112. ? parsedOptions(question).find(opt => opt.isAnswered)?.optionId || ''
  113. : ''
  114. return { ...question, selectedOptionId }
  115. })?.sort((a, b) => Number(a.sortOrder) - Number(b.sortOrder)) || []
  116. }
  117. // JSON形式のオプションを解析
  118. const parsedOptions = (question) => {
  119. try {
  120. return JSON.parse(question.options || '[]')
  121. } catch (error) {
  122. console.error(`オプション解析エラー(${question.sortOrder}):`, error)
  123. return []
  124. }
  125. }
  126. // ラジオボタン選択時の処理
  127. const handleRadioChange = (question, selectedOption) => {
  128. parsedOptions(question).forEach(opt => {
  129. opt.isAnswered = opt.optionId === selectedOption.optionId
  130. })
  131. }
  132. // 日付フォーマット変換(YYYY-MM-DD → YYYY/MM/DD)
  133. const formatDate = (dateStr) => dateStr?.replace(/-/g, '/') || ''
  134. // surveyCodeをパラメータにして前のページに戻る
  135. const goBack = () => {
  136. tagsViewStore.delView(currentRoute).then(() => {
  137. router.push({
  138. name: 'surveyResults',
  139. params: { surveyCode: surveyCode.value }
  140. })
  141. })
  142. }
  143. // コンポーネント初期化時にデータロード
  144. onMounted(() => loadSurveyData())
  145. </script>
  146. <style scoped>
  147. .app-container {
  148. font-family: Arial, sans-serif;
  149. width: 600px;
  150. margin: 0 auto;
  151. padding: 20px;
  152. position: relative;
  153. min-height: 500px;
  154. }
  155. .survey-title {
  156. font-size: 18px;
  157. font-weight: bold;
  158. margin-bottom: 10px;
  159. }
  160. .deadline {
  161. margin-left: 50%;
  162. color: #666;
  163. margin-bottom: 20px;
  164. }
  165. @media (max-width: 48em) {
  166. .app-container {
  167. width: 100%;
  168. padding: 15px;
  169. }
  170. .deadline {
  171. margin-left: 0;
  172. text-align: right;
  173. }
  174. }
  175. .question {
  176. margin-bottom: 25px;
  177. padding-bottom: 15px;
  178. border-bottom: 2px solid #0070c0;
  179. }
  180. .question:last-child {
  181. border-bottom: none;
  182. }
  183. .question-number {
  184. font-weight: bold;
  185. margin-right: 8px;
  186. }
  187. .question-text {
  188. margin-bottom: 10px;
  189. display: inline-block;
  190. }
  191. .options {
  192. margin-top: 10px;
  193. display: flex;
  194. flex-wrap: wrap;
  195. gap: 10px 20px;
  196. }
  197. .option-item {
  198. cursor: pointer;
  199. display: flex;
  200. align-items: center;
  201. }
  202. .option-item input {
  203. margin-right: 5px;
  204. }
  205. .dropdown select {
  206. margin-top: 10px;
  207. width: 260px;
  208. height: 30px;
  209. padding: 0 5px;
  210. border: 1px solid #ddd;
  211. }
  212. .text-area textarea {
  213. margin-top: 10px;
  214. width: 70%;
  215. box-sizing: border-box;
  216. padding: 5px;
  217. border: 1px solid #ddd;
  218. resize: vertical;
  219. line-height: 1.7;
  220. }
  221. .back-button {
  222. background-color: #FF0066;
  223. border: none;
  224. padding: 10px 40px;
  225. cursor: pointer;
  226. width: 180px;
  227. height: 40px;
  228. display: block;
  229. margin-top: 20px;
  230. transition: background-color 0.2s;
  231. color: white;
  232. font-weight: bold;
  233. }
  234. .back-button:hover {
  235. background-color: #FF0066;
  236. }
  237. </style>