Advertisement
Guest User

Component C

a guest
Apr 11th, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2.     <div class="container mt-2">
  3.         <div class="row">
  4.             <div class="d-inline col-lg-4 col-md-4 col-sm-4" padding="0px">
  5.                 Date
  6.                 <font color="red">*</font>
  7.             </div>
  8.             <div class="d-inline col-lg-8 col-md-8 col-sm-8" padding="0px">
  9.                 <date-picker
  10.                         v-model="commentData.customDate"
  11.                         :lang="lang"
  12.                         type="date"
  13.                         format="DD.MM.YYYY"
  14.                         confirm
  15.                 ></date-picker>
  16.             </div>
  17.         </div>
  18.         <div class="row">
  19.             <div class="d-inline col-lg-4 col-md-4 col-sm-4" padding="0px">
  20.                 Comment
  21.                 <font color="red">*</font>
  22.             </div>
  23.             <div class="d-inline col-lg-8 col-md-8 col-sm-8" padding="0px">
  24.                 <input v-model="commentData.comment" type="text" class="form-control" size="16">
  25.             </div>
  26.         </div>
  27.  
  28.         <div class="row">
  29.             <div class="d-inline col-lg-4 col-md-4 col-sm-4" padding="0px">
  30.                 Category
  31.                 <font color="red">*</font>
  32.             </div>
  33.             <div class="d-inline col-lg-8 col-md-8 col-sm-8" padding="0px">
  34.                 <select v-model="selectedCategory">
  35.                     <option>Social (Comment)</option>
  36.                     <option>Sport (Comment)</option>
  37.                     <option>School (Comment)</option>
  38.                     <option>Others (Comment)</option>
  39.                 </select>
  40.             </div>
  41.         </div>
  42.         <button type="submit" class="btn btn-primary" @click.prevent="saveComment()">Save</button>
  43.         <button type="submit" class="btn btn-primary float-right" @click.prevent="$emit('close')">Cancel</button>
  44.  
  45.     </div>
  46. </template>
  47.  
  48. <script>
  49.     import * as $ from "jquery";
  50.     import DatePicker from "vue2-datepicker";
  51.  
  52.     let uuid = 0;
  53.  
  54.     export default {
  55.         props: ["text", "commentRecId"],
  56.         beforeCreate() {
  57.             this.uuid = uuid.toString();
  58.             uuid += 1;
  59.             console.log("beforeCreate() uuid: " + this.uuid);
  60.         },
  61.         components: { DatePicker },
  62.             data: function() {
  63.                 return {
  64.                     commentData: {
  65.                         comment: "",
  66.                         customDate: ""
  67.                     },
  68.                     selectedCategory: "",
  69.                     lang: {
  70.                         default: "en"
  71.                     },
  72.                 }
  73.             },
  74.             mounted: function() {
  75.                 // console.log("this._uid: " + this._uid);
  76.                 this.fillForm();
  77.             },
  78.         methods: {
  79.             fillForm: function(){
  80.                 console.log("fillForm called!");
  81.                 var listCategory;
  82.                 var listComment;
  83.                 var listDate;
  84.  
  85.                 let siteUrl = 'https://thesite.sharepoint.com/sites/Playercard/';
  86.                 let clientContext = new SP.ClientContext(siteUrl);
  87.                 let oList = clientContext.get_web().get_lists().getByTitle('Comment');
  88.                 let camlQuery = new SP.CamlQuery();
  89.  
  90.                 var statID = this.commentRecId;
  91.                 console.log("EditComment CommentRecId: " + this.commentRecId);
  92.                 console.log("EditComment statID: " + statID);
  93.  
  94.                 camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' +
  95.                     '<Value Type=\'Number\'>'+statID+'</Value></Geq></Where></Query><View>');
  96.  
  97.                 var collListItem = oList.getItems(camlQuery);
  98.                 clientContext.load(collListItem);
  99.                 console.log("clientContext loaded!");
  100.  
  101.                 // First we much execute the query to retrieve the items
  102.                 clientContext.executeQueryAsync(()=> {
  103.                         // Now colListItem is a collection, we must iterate through it
  104.                         var listItemEnumerator = collListItem.getEnumerator();
  105.  
  106.                         while (listItemEnumerator.moveNext()) {
  107.                             var oListItem = listItemEnumerator.get_current();
  108.                             listDate = oListItem.get_item('Date');
  109.                             listCategory = oListItem.get_item('Category');
  110.                             listComment = oListItem.get_item('Comment');
  111.                         }
  112.  
  113.                         console.log("listDate: " + listDate);
  114.                         console.log("listCategory " + listCategory);
  115.                         console.log("listComment: " + listComment);
  116.                         this.commentData.customDate = listDate;
  117.                         this.selectedCategory = listCategory;
  118.                         this.commentData.comment = listComment;
  119.  
  120.                         clientContext.executeQueryAsync(
  121.                             () => console.log('Item(s) edited')),
  122.                             () => console.log('Failed to edit item(s)');
  123.                     },
  124.                     ()=>console.log('Failed to retrieve items'));
  125.             },
  126.             getFormDigest: function() {
  127.                 return $.ajax({
  128.                     //url: "/DEV/BBUlm" + "/_api/contextinfo",
  129.                     url: this.$store.state.baseUrlContextinfo,
  130.                     method: "POST",
  131.                     async: false,
  132.                     headers: {
  133.                         Accept: "application/json; odata=verbose"
  134.                     }
  135.                 });
  136.             },
  137.             saveComment: function() {
  138.                 var playerID = this.$store.state.selectedPlayer.ID;
  139.                 var date = this.commentData.customDate.toISOString();
  140.                 var listName = "Comment";
  141.                 var $this = this;
  142.                 var itemType = this.GetItemTypeForListName(listName);
  143.                 var item = {
  144.                     __metadata: {
  145.                         type: itemType
  146.                     },
  147.                     Comment: this.commentData.comment,
  148.                     Category: this.selectedCategory,
  149.                     Date: date,
  150.                     PlayerNameId: playerID
  151.                 };
  152.                 var baseUrl = this.$store.state.baseUrl;
  153.                 baseUrl += "GetByTitle('" + listName + "')/items"; // + filter;
  154.                 return this.getFormDigest(baseUrl).then(function(data) {
  155.                     $.ajax({
  156.                         url: baseUrl,
  157.                         type: "POST",
  158.                         contentType: "application/json;odata=verbose",
  159.                         data: JSON.stringify(item),
  160.                         headers: {
  161.                             Accept: "application/json;odata=verbose",
  162.                             "X-RequestDigest": data.d.GetContextWebInformation.FormDigestValue
  163.                         },
  164.                         async: false,
  165.                         success: function(data, textStatus, xhr) {
  166.                             $this.$emit("close");
  167.                             $this.writeHistory(data.d.ID);
  168.                             console.log("success!");
  169.                         },
  170.                         error: function(xhr, textStatus, errorThrown) {
  171.                             alert("error:" + JSON.stringify(xhr));
  172.                             $("#dialog" + "records").html(" [0]");
  173.                         }
  174.                     });
  175.                 });
  176.             },
  177.             writeHistory: function(commentRecId) {
  178.                 var playerID = this.$store.state.selectedPlayer.ID;
  179.                 var playerName = this.$store.state.selectedPlayer.Name;
  180.                 var $this = this;
  181.                 var listName = "History";
  182.                 var CurrentDate = this.commentData.customDate.toISOString();
  183.                 var itemType = this.GetItemTypeForListName(listName);
  184.                 var item = {
  185.                     __metadata: {
  186.                         type: itemType
  187.                     },
  188.                     PlayerNameId: playerID,
  189.                     date: CurrentDate,
  190.                     act: this.selectedCategory,
  191.                     details: this.commentData.comment,
  192.                     CorrespondingListID: commentRecId.toString()
  193.                 };
  194.                 // var baseUrl = "/DEV/BBUlm" + "/_api/web/lists/";
  195.                 var baseUrl = this.$store.state.baseUrl;
  196.                 baseUrl += "GetByTitle('" + listName + "')/items";
  197.                 return this.getFormDigest(baseUrl).then(function(data) {
  198.                     $.ajax({
  199.                         url: baseUrl,
  200.                         type: "POST",
  201.                         contentType: "application/json;odata=verbose",
  202.                         data: JSON.stringify(item),
  203.                         headers: {
  204.                             Accept: "application/json;odata=verbose",
  205.                             "X-RequestDigest": data.d.GetContextWebInformation.FormDigestValue
  206.                         },
  207.                         async: false,
  208.                         success: function(data, textStatus, xhr) {
  209.                             $this.$emit("close");
  210.                         },
  211.                         error: function(xhr, textStatus, errorThrown) {
  212.                             alert("error:" + JSON.stringify(xhr));
  213.                             $("#dialog" + "records").html(" [0]");
  214.                         }
  215.                     });
  216.                 });
  217.             },
  218.             GetItemTypeForListName: function(name) {
  219.                 return (
  220.                     "SP.Data." +
  221.                     name.charAt(0).toUpperCase() +
  222.                     name
  223.                         .split(" ")
  224.                         .join("")
  225.                         .slice(1) +
  226.                     "ListItem"
  227.                 );
  228.             }
  229.         }
  230.     };
  231. </script>
  232.  
  233. <style scoped>
  234.     .v--modal {
  235.         padding: 10px;
  236.     }
  237.     .row {
  238.         margin-bottom: 20px;
  239.     }
  240.     select {
  241.         -webkit-appearance: menulist;
  242.         border-style: solid;
  243.     }
  244. </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement