Advertisement
Guest User

Use case DataManager vuetable-2

a guest
Nov 20th, 2019
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 29.10 KB | None | 0 0
  1. <template>
  2.     <div class="ui container">
  3.         <div class="row">
  4.             <div class="col-4">
  5.                 <filter-bar></filter-bar>
  6.             </div>
  7.             <div class="col-8">
  8.                 <div class="pull-right">
  9.                     <button v-if="level" class="ui btn btn-primary" @click.prevent="addStudent()">Ajouter un Etudiant
  10.                     </button>
  11.                     <button class="btn btn-primary " @click.prevent="print()">Imprimer La Liste</button>
  12.                     <download-excel
  13.                        class="btn btn-primary"
  14.                        :data="students"
  15.                        :fields="json_fields"
  16.                        worksheet="My Worksheet"
  17.                        :name="getExcelFileName">
  18.                         Download Excel
  19.                     </download-excel>
  20.                 </div>
  21.             </div>
  22.         </div>
  23.         <div class="row">
  24.             <div class="col-3">
  25.                 <v-select v-model="currentDivision" label="name" :reduce="div => div.id" :placeholder="'Division'"
  26.                           :options="allDivisions"/>
  27.             </div>
  28.             <div class="col-3">
  29.                 <v-select v-model="currentFiliere" :get-option-label="getFiliereLabel" :placeholder="'Filiere'"
  30.                          :filterable="true" :options="allFilieres"
  31.                          v-if="allFilieres" :reduce="f => f.id" label="Filiere"/>
  32.             </div>
  33.             <div class="col-3">
  34.                 <v-select v-model="currentSex" :placeholder="'Sexe'" :options="['M','F']"/>
  35.             </div>
  36.             <div class="col-3  mb-1">
  37.                 <button v-if="currentFiliere && level" class="ui btn btn-primary" @click.prevent="manageNotes()">Gerer Les
  38.                     Notes
  39.                 </button>
  40.             </div>
  41.             <div class="col-3 mb-1">
  42.                 <button  class="ui btn btn-primary" @click.prevent="printStudentsCard()">Imprimer
  43.                     Les Cartes
  44.                 </button>
  45.             </div>
  46.         </div>
  47.         <vuetable id="print" ref="vuetable"
  48.                  :api-mode="false"
  49.                  :fields="fields"
  50.                  :per-page="perPage"
  51.                  :append-params="moreParams"
  52.                  :data-manager="dataManager"
  53.                  pagination-path="pagination"
  54.                  @vuetable:pagination-data="onPaginationData"
  55.        >
  56.             <div slot="actions" slot-scope="props">
  57.                 <button
  58.                    title="Imprimer le relever de Notes"
  59.                    class="ui small button"
  60.                    @click="onActionClicked('print-student-report', props.rowData)"
  61.                >
  62.                     <i class="id card icon"></i>
  63.                 </button>
  64.                 <button
  65.                    class="ui small button"
  66.                    @click="onActionClicked('edit-item', props.rowData)"
  67.                >
  68.                     <i class="edit icon"></i>
  69.                 </button>
  70.                 <button
  71.                    class="ui small button"
  72.                    @click="onActionClicked('delete-item', props.rowData)"
  73.                >
  74.                     <i class="user times icon"></i>
  75.                 </button>
  76.             </div>
  77.         </vuetable>
  78.         <div style="padding-top:10px">
  79.             <vuetable-pagination-info ref="paginationInfo"
  80.            ></vuetable-pagination-info>
  81.             <vuetable-pagination ref="pagination"
  82.                                 @vuetable-pagination:change-page="onChangePage"
  83.            ></vuetable-pagination>
  84.         </div>
  85.     </div>
  86. </template>
  87.  
  88. <script>
  89.     import Vuetable from "vuetable-2";
  90.     import VuetablePagination from "vuetable-2/src/components/VuetablePagination";
  91.     import VuetablePaginationInfo from "vuetable-2/src/components/VuetablePaginationInfo.vue";
  92.     import FieldsDef from "./studentFields.js";
  93.     import _ from "lodash";
  94.     import HelperMixin from '../helpers';
  95.     import NoteReportComponent from "./NoteReportComponent";
  96.     import StudentCard from "./StudentCardComponent";
  97.  
  98.     export default {
  99.         components: {
  100.             Vuetable,
  101.             VuetablePagination,
  102.             VuetablePaginationInfo
  103.         },
  104.         mixins: [HelperMixin],
  105.         data() {
  106.             return {
  107.                 json_fields: {
  108.                     'MATRICULE': 'matricule',
  109.                     // regular field (exported data 'as is')
  110.                     'NOM': 'name',
  111.                     'PRENOM': 'surname',
  112.                     'NIVEAU': {
  113.                         field: 'levels',
  114.                         callback: (value) => {
  115.                             return value[0].name;
  116.                         },
  117.                     },
  118.                     'OPTION': 'option.name',
  119.                     'FILIERE': 'filiere.name',
  120.                     'DIVISION': 'division.name',
  121.                     'SEXE': 'sex'
  122.                 },
  123.                 fields: FieldsDef,
  124.                 perPage: 10,
  125.                 students: [],
  126.                 level: null,
  127.                 currentFiliere: null,
  128.                 currentSex: null,
  129.                 currentDivision: null,
  130.                 allDivisions: [],
  131.                 allOptions: [],
  132.                 allFilieres: [],
  133.                 allLevel: [],
  134.                 moreParams: {}
  135.             };
  136.         },
  137.         computed: {
  138.             getExcelFileName: function () {
  139.                 return this.getPageName() + '.xls';
  140.             }
  141.         },
  142.  
  143.         watch: {
  144.             students(newVal, oldVal) {
  145.                 this.$refs.vuetable.refresh();
  146.             },
  147.             // whenever question changes, this function will run
  148.             currentFiliere: function (newClass, oldClass) {
  149.                 this.onCustomFilter();
  150.             },
  151.             currentSex: function (newClass, oldClass) {
  152.                 this.onCustomFilter();
  153.             },
  154.             currentDivision: function (newClass, oldClass) {
  155.                 console.log(newClass);
  156.                 this.onCustomFilter();
  157.             },
  158.             '$route.params.level_id': {
  159.                 handler(level_id) {
  160.                     this.level = level_id;
  161.                     this.moreParams.niveau = this.level;
  162.                     this.getData();
  163.                 },
  164.                 immediate: true,
  165.             }
  166.         },
  167.  
  168.         mounted() {
  169.             this.level = this.$route.params.level_id;
  170.             if (this.level) {
  171.                 console.log(this.level);
  172.                 this.moreParams.niveau = this.level;
  173.             }
  174.             this.getData();
  175.             this.$root.$emit('set-title', 'Gestion des Etudiants');
  176.             this.$root.$on('filter-set', eventData => this.onFilterSet(eventData));
  177.             this.$root.$on('filter-reset', e => this.onFilterReset());
  178.             this.getAddData();
  179.         },
  180.  
  181.         methods: {
  182.             manageNotes() {
  183.                 console.log(this.currentFiliere);
  184.                 const fil = this.allFilieres.find((f)=>f.id===this.currentFiliere);
  185.                 this.$router.push('/edit-notes/' + this.level + '/filiere/' + fil.id + '/' + fil.name + '-' + 'niveau' + this.level);
  186.             },
  187.             printStudentsCard() {
  188.                 console.log(this.students);
  189.                 let stylesHtml = '';
  190.                 for (const node of [...document.querySelectorAll('link[rel="stylesheet"], style')]) {
  191.                     stylesHtml += node.outerHTML;
  192.                 }
  193.  
  194.                 let prtHtml = '<div class="">';
  195.                 for (let s of this.students) {
  196.                     console.log(s);
  197.                     var studentCard = new Vue({
  198.                         ...StudentCard,
  199.                         parent: this,
  200.                         propsData: {
  201.                             name: s.name,
  202.                             surname: s.surname,
  203.                             matricule: s.matricule,
  204.                             sex: s.sex,
  205.                             photo: '/uploads/' + s.photo,
  206.                             filiere: s.filiere.name,
  207.                             level: s.levels[0].name
  208.                         }
  209.                     }).$mount();
  210.                     console.log(studentCard.$el);
  211.                     prtHtml += studentCard.$el.outerHTML;
  212.                 }
  213.                 // Get HTML to print from element
  214.                 // const prtHtml = document.getElementById('.row').innerHTML;
  215.  
  216.                 // Get all stylesheets HTML
  217.                 prtHtml += '</div>';
  218.                 // Open the print window
  219.                 const WinPrint = window.open('', '', 'left=0,top=0,width=800,height=900,toolbar=0,scrollbars=0,status=0');
  220.  
  221.                 WinPrint.document.write(`<!DOCTYPE html>
  222.                                     <html>
  223.                                       <head>
  224.                                         ${stylesHtml}
  225.                                       </head>
  226.                                       <body>
  227.                                         ${prtHtml}
  228.                                       </body>
  229.                                     </html>`);
  230.  
  231.                 WinPrint.document.close();
  232.                 WinPrint.focus();
  233.                 WinPrint.open();
  234.             },
  235.             getFiliereLabel(option) {
  236.                 return option.name.toUpperCase();
  237.             },
  238.             onPaginationData(paginationData) {
  239.                 this.$refs.pagination.setPaginationData(paginationData);
  240.             },
  241.             onChangePage(page) {
  242.                 this.$refs.vuetable.changePage(page);
  243.             },
  244.             dataManager(sortOrder, pagination) {
  245.                 if (this.students.length < 1) {
  246.                    pagination = this.$refs.vuetable.makePagination(
  247.                        this.students.length,
  248.                        this.perPage
  249.                    );
  250.                    this.$refs.paginationInfo.setPaginationData(pagination);
  251.                    return {
  252.                        pagination: pagination,
  253.                        data: [],
  254.  
  255.                    };
  256.                }
  257.  
  258.                let local = this.students;
  259.  
  260.                // sortOrder can be empty, so we have to check for that as well
  261.                if (sortOrder.length > 0) {
  262.                     console.log("orderBy:", sortOrder[0].sortField, sortOrder[0].direction);
  263.                     local = _.orderBy(
  264.                         local,
  265.                         sortOrder[0].sortField,
  266.                         sortOrder[0].direction
  267.                     );
  268.                 }
  269.  
  270.                 pagination = this.$refs.vuetable.makePagination(
  271.                     local.length,
  272.                     this.perPage
  273.                 );
  274.                 this.$refs.paginationInfo.setPaginationData(pagination);
  275.                 let from = pagination.from - 1;
  276.                 let to = from + this.perPage;
  277.  
  278.                 return {
  279.                     pagination: pagination,
  280.                     data: _.slice(local, from, to),
  281.  
  282.                 };
  283.             },
  284.             onActionClicked(action, data) {
  285.                 console.log(data);
  286.                 switch (action) {
  287.                     case 'edit-item' :
  288.                         this.updateStudent(data);
  289.                         break;
  290.                     case 'print-student-report':
  291.                         this.printStudentReport(data);
  292.                         break;
  293.                     case 'delete-item':
  294.                         this.gShowConfirm('Voulez Vous Vraiment supprimer "' + data.name + '" ?', this.deleteStudent, data.id);
  295.                         break;
  296.                 }
  297.             },
  298.             print() {
  299.                 this.$easyPrint('print', this.getPageName(), null, false);
  300.             },
  301.             printStudentReport(data) {
  302.                 axios.get('/api/examinations/getStudentReportData/' + data.id)
  303.                     .then((resp) => {
  304.                         const propsData = {
  305.                             data: resp.data.data,
  306.                             nom: data.name + ' ' + data.surname,
  307.                             matricule: data.matricule,
  308.                             cycle:data.option.name+' OPTION '+ data.filiere.name,
  309.                             niveau: parseInt(data.levels[0].name),
  310.                             annee_scolaire: resp.data.data.schoolYear.name
  311.                         };
  312.                         this.$easyPrintComponent(NoteReportComponent, propsData, null, 'portrait', true);
  313.                     })
  314.                     .catch((error) => {
  315.                         console.log(error);
  316.                     });
  317.             },
  318.             onFilterSet(filterText) {
  319.                 this.moreParams = {
  320.                     ...this.moreParams,
  321.                     'filter': filterText
  322.                 };
  323.                 this.getData();
  324.             },
  325.             onFilterReset() {
  326.                 this.moreParams.filter = null;
  327.                 this.getData();
  328.             },
  329.             onCustomFilter() {
  330.                 this.moreParams = {
  331.                     ...this.moreParams,
  332.                     filiere_id: this.currentFiliere,
  333.                     sex: this.currentSex,
  334.                     division_id: this.currentDivision
  335.                 };
  336.                 this.getData();
  337.             },
  338.             getAddData() {
  339.                 axios
  340.                     .get("/api/students/create")
  341.                     .then((response) => {
  342.                         console.log(response.data.data);
  343.                         this.allOptions = response.data.data.options;
  344.                         this.allDivisions = response.data.data.divisions;
  345.                         this.allFilieres = response.data.data.filieres;
  346.  
  347.                     })
  348.                     .catch(function (error) {
  349.                         console.log(error);
  350.                     });
  351.             },
  352.             addStudent() {
  353.                 let m = this.$modals.add({
  354.                     title: "Ajouter un Etudiant ",
  355.                     theme: "osx", // or 'osx'
  356.                     rtl: false, // default false
  357.                     items: [
  358.                         {
  359.                             label: "Nom",
  360.                             name: "name",
  361.                             type: "text",
  362.                             attr: {
  363.                                 required: true
  364.                             }
  365.                         },
  366.                         {
  367.                             label: "Prenom",
  368.                             name: "surname",
  369.                             type: "text",
  370.                             attr: {
  371.                                 required: true
  372.                             }
  373.                         },
  374.                         {
  375.                             label: "Sexe",
  376.                             name: "sex",
  377.                             type: "radio",
  378.                             attr: {
  379.                                 required: true
  380.                             },
  381.                             options: [
  382.                                 {text: 'M', value: 'M'},
  383.                                 {text: 'F', value: 'F'}
  384.                             ]
  385.  
  386.                         },
  387.                         {
  388.                             label: "Matricule",
  389.                             name: "matricule",
  390.                             type: "text",
  391.                             attr: {
  392.                                 required: true
  393.                             }
  394.                         },
  395.                         {
  396.                             label: "Division",
  397.                             name: "division_id",
  398.                             type: "select",
  399.                             value: "1",
  400.                             options: this.allDivisions.map((division) => {
  401.                                 return {text: division.name.toUpperCase(), value: division.id};
  402.                             })
  403.                         },
  404.                         {
  405.                             label: "Option",
  406.                             name: "option_id",
  407.                             type: "select",
  408.                             value: "1",
  409.                             options: this.allOptions.map((opt) => {
  410.                                 return {text: opt.name.toUpperCase(), value: opt.id};
  411.                             })
  412.                         },
  413.                         {
  414.                             label: "Filiere",
  415.                             name: "filiere_id",
  416.                             type: "select",
  417.                             value: "1",
  418.                             options: this.allFilieres.map((filiere) => {
  419.                                 return {text: filiere.name.toUpperCase(), value: filiere.id};
  420.                             })
  421.                         },
  422.                         {
  423.                             label: "Carte Photo",
  424.                             name: "photo",
  425.                             type: "file",
  426.                             attr: {
  427.                                 required: true,
  428.                                 placeholder: 'Choisir une photo 4x4'
  429.                             },
  430.                         },
  431.                         {
  432.                             label: "Telephone",
  433.                             name: "tel",
  434.                             type: "tel",
  435.                             attr: {
  436.                                 required: true
  437.                             }
  438.                         },
  439.  
  440.                         {
  441.                             label: "Nom Parent",
  442.                             name: "parent_name",
  443.                             type: "text",
  444.                             attr: {
  445.                                 required: true
  446.                             }
  447.                         },
  448.                         {
  449.                             name: "level_id",
  450.                             type: "hidden",
  451.                             value: this.level
  452.                         },
  453.                         {
  454.                             label: "Telephone Parent",
  455.                             name: "parent_tel",
  456.                             type: "tel",
  457.                             attr: {
  458.                                 required: true
  459.                             }
  460.                         }
  461.                     ],
  462.                     use: {
  463.                         // footer,
  464.                     }
  465.                 });
  466.                 m.open(); // open modal and add onopen event
  467.                 //m.onreload(getData);
  468.                 m.onsave(async m => {
  469.                     m.loading();
  470.                     axios
  471.                         .post("/api/students", m.formData())
  472.                         .then((response) => {
  473.                             console.log(response);
  474.                             m.loaded();
  475.                             this.$notify({
  476.                                 group: 'validation',
  477.                                 title: 'Important message',
  478.                                 text: response.data.message,
  479.                                 type: 'success'
  480.                             });
  481.                             this.getData();
  482.                             m.close();
  483.                         })
  484.                         .catch((error) => {
  485.                             m.loaded();
  486.                             this.$notify({
  487.                                 group: 'validation',
  488.                                 title: 'Important message',
  489.                                 text: 'Une erreur sest produite',
  490.                                 type: 'error'
  491.                             });
  492.                             console.log(error);
  493.                         });
  494.                 });
  495.             },
  496.             getData() {
  497.                 Vue.nextTick(() => {
  498.                     axios.get("/api/students", {params: this.moreParams}).then(response => {
  499.                         this.students = response.data.data.students;
  500.                     });
  501.                 });
  502.             },
  503.             getPageName() {
  504.                 let name = "Liste_Etudiants";
  505.                 let division;
  506.                 let sex;
  507.                 let cfiliere;
  508.                 if (this.moreParams.sex) {
  509.                     sex = (this.moreParams.sex === "M") ? 'Masculin' : 'Feminin';
  510.                     name += '_' + sex;
  511.                 }
  512.                 if (this.moreParams.filiere_id) {
  513.                     cfiliere = this.allFilieres.find((c) => {
  514.                         return c.id === this.moreParams.filiere_id
  515.                     });
  516.                     console.log(this.allFilieres, cfiliere);
  517.                     name += '_' + cfiliere.name;
  518.                 }
  519.                 if (this.moreParams.division_id) {
  520.                     division = this.allDivisions.find((c) => {
  521.                         return c.id === this.moreParams.division_id
  522.                     });
  523.                     name += '_' + division.name;
  524.                 }
  525.  
  526.                 if (this.moreParams.niveau) {
  527.                     name += '_level_' + this.moreParams.niveau;
  528.                 }
  529.                 return name;
  530.  
  531.             },
  532.             updateStudent(data) {
  533.                 let m = this.$modals.add({
  534.                     title: "Modifier un Etudiant ",
  535.                     theme: "osx", // or 'osx'
  536.                     rtl: false, // default false
  537.                     items: [
  538.                         {
  539.                             label: "Nom",
  540.                             name: "name",
  541.                             type: "text",
  542.                             value: data.name,
  543.                             attr: {
  544.                                 required: true
  545.                             }
  546.                         },
  547.                         {
  548.                             label: "Prenom",
  549.                             name: "surname",
  550.                             type: "text",
  551.                             value: data.surname,
  552.                             attr: {
  553.                                 required: true
  554.                             }
  555.                         },
  556.                         {
  557.                             label: "Sexe",
  558.                             name: "sex",
  559.                             type: "radio",
  560.                             attr: {
  561.                                 required: true
  562.                             },
  563.                             value: data.sex,
  564.                             options: [
  565.                                 {text: 'M', value: 'M'},
  566.                                 {text: 'F', value: 'F'}
  567.                             ]
  568.  
  569.                         },
  570.                         {
  571.                             label: "Matricule",
  572.                             name: "matricule",
  573.                             type: "text",
  574.                             value: data.matricule,
  575.                             attr: {
  576.                                 required: true
  577.                             }
  578.                         },
  579.                         {
  580.                             label: "Division",
  581.                             name: "division_id",
  582.                             type: "select",
  583.                             value: data.division.id,
  584.                             options: this.allDivisions.map((division) => {
  585.                                 return {text: division.name.toUpperCase(), value: division.id};
  586.                             })
  587.                         },
  588.                         {
  589.                             label: "Option",
  590.                             name: "option_id",
  591.                             type: "select",
  592.                             value: data.option.id,
  593.                             options: this.allOptions.map((opt) => {
  594.                                 return {text: opt.name.toUpperCase(), value: opt.id};
  595.                             })
  596.                         },
  597.                         {
  598.                             label: "Filiere",
  599.                             name: "filiere_id",
  600.                             type: "select",
  601.                             value: data.filiere.id,
  602.                             options: this.allFilieres.map((filiere) => {
  603.                                 return {text: filiere.name.toUpperCase(), value: filiere.id};
  604.                             })
  605.                         },
  606.                         {
  607.                             label: "Carte Photo",
  608.                             name: "photo",
  609.                             type: "file",
  610.                             attr: {
  611.                                 placeholder: 'Choisir une photo 4x4'
  612.                             },
  613.                         },
  614.                         {
  615.                             label: "Telephone",
  616.                             name: "tel",
  617.                             type: "tel",
  618.                             value: data.tel,
  619.                             attr: {
  620.                                 required: true
  621.                             }
  622.                         },
  623.  
  624.                         {
  625.                             label: "Nom Parent",
  626.                             name: "parent_name",
  627.                             type: "text",
  628.                             value: data.parent_name,
  629.                             attr: {
  630.                                 required: true
  631.                             }
  632.                         },
  633.                         {
  634.                             label: "Telephone Parent",
  635.                             name: "parent_tel",
  636.                             type: "tel",
  637.                             value: data.parent_tel,
  638.                             attr: {
  639.                                 required: true
  640.                             }
  641.                         },
  642.                         {
  643.                             type: "hidden",
  644.                             name: '_method',
  645.                             value: 'PUT',
  646.  
  647.                         },
  648.                         {
  649.                             name: "level_id",
  650.                             type: "hidden",
  651.                             value: this.level
  652.                         },
  653.  
  654.                     ],
  655.                     use: {}
  656.                 });
  657.                 m.open(); // open modal and add onopen event
  658.                 m.onsave(async m => {
  659.                     m.loading();
  660.                     m.formData().append('_method', 'PUT');
  661.                     axios
  662.                         .post("/api/students/" + data.id, m.formData())
  663.                         .then((response) => {
  664.                             console.log(response);
  665.                             m.loaded();
  666.                             this.$notify({
  667.                                 group: 'validation',
  668.                                 title: 'Message',
  669.                                 text: response.data.message,
  670.                                 type: 'success'
  671.                             });
  672.                             this.getData();
  673.                             m.close();
  674.                         })
  675.                         .catch(function (error) {
  676.                             m.loaded();
  677.                             this.$notify({
  678.                                 group: 'validation',
  679.                                 title: 'Message',
  680.                                 text: error.message,
  681.                                 type: 'error'
  682.                             });
  683.                             console.log(error);
  684.                         });
  685.                 });
  686.             },
  687.             deleteStudent(studentId) {
  688.                 let formData = new FormData();
  689.                 formData.append('_method', 'DELETE');
  690.                 axios
  691.                     .post('/api/students/' + studentId, formData)
  692.                     .then(resp => {
  693.                         this.getData();
  694.                         this.$notify({
  695.                             group: 'validation',
  696.                             title: 'Message',
  697.                             text: resp.data.message,
  698.                             type: 'success'
  699.                         });
  700.  
  701.                     })
  702.                     .catch(err => {
  703.                         this.$notify({
  704.                             group: 'validation',
  705.                             title: 'Message',
  706.                             text: 'Une Erreur s\'est produite',
  707.                             type: 'error'
  708.                         });
  709.                         console.log(err)
  710.                     })
  711.  
  712.             },
  713.             showConfirm(message, callback, data) {
  714.                 const options = {title: 'Confirmation', cancelLabel: 'No'};
  715.                 this.$dialogs.confirm(message, options)
  716.                     .then(res => {
  717.                         if (res.ok) {
  718.                             callback(data);
  719.                         }
  720.                         console.log(res) // {ok: true|false|undefined}
  721.                     })
  722.             }
  723.         }
  724.     };
  725. </script>
  726.  
  727. <style>
  728.     #app {
  729.         font-family: "Avenir", Helvetica, Arial, sans-serif;
  730.         -webkit-font-smoothing: antialiased;
  731.         -moz-osx-font-smoothing: grayscale;
  732.         color: #2c3e50;
  733.         margin-top: 20px;
  734.     }
  735.  
  736.     button.ui.button {
  737.         padding: 8px 3px 8px 10px;
  738.         margin-top: 1px;
  739.         margin-bottom: 1px;
  740.     }
  741. </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement