Search code examples
javascriptvue.jsmodal-dialogvariable-assignmentassign

Vue.js Modal Dialog doesn't show value


I have a modal dialog that should get filled automatically with values. It looks like this:

enter image description here

As you can see in the console, listCategory has the value Social (Comment), but it doesn't appear in the modal dialog and I don't know why.

It only has a value if I write this:

return {
  commentData: {
     comment: "",
     customDate: ""
  },
  selectedCategory: "Social (Comment)",
  lang: {
      default: "en"
  }
};

I'm not too familiar with Vue.js which is why I want to know how one can assign listCategory to selectedCategory.

This doesn't work:

selectedCategory: listCategory,

Nor does this:

selectedCategory: this.listCategory,

Here is the code:

export default {
        props: ["text"],
        components: { DatePicker },
        data: function() {
            var listCategory;
            var listComment;
            var listDate;

            let siteUrl = 'https://thesite.sharepoint.com/sites/Playercard/';
            let clientContext = new SP.ClientContext(siteUrl);
            let oList = clientContext.get_web().get_lists().getByTitle('Comment');
            let camlQuery = new SP.CamlQuery();

            camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'ID\'/>' +
                '<Value Type=\'Number\'>100</Value></Eq></Where></Query></View>');
            var collListItem = oList.getItems(camlQuery);
            clientContext.load(collListItem);
            console.log("clientContext loaded!");

            // First we much execute the query to retrieve the items
            clientContext.executeQueryAsync(()=> {
                    // Now colListItem is a collection, we must iterate through it
                    var listItemEnumerator = collListItem.getEnumerator();

                    while (listItemEnumerator.moveNext()) {
                        var oListItem = listItemEnumerator.get_current();
                        listDate = oListItem.get_item('Date');
                        listCategory = oListItem.get_item('Category');
                        listComment = oListItem.get_item('Comment');
                    }

                    console.log("listDate: " + listDate);
                    console.log("listCategory " + listCategory);
                    console.log("listComment: " + listComment);
                    this.commentData.customDate = listDate;
                    this.commentData.selectedCategory = listCategory;
                    this.commentData.comment = listComment;

                    clientContext.executeQueryAsync(
                        () => console.log('Item(s) edited')),
                        () => console.log('Failed to edit item(s)');
                },
                ()=>console.log('Failed to retrieve items'));

            return {
                commentData: {
                    comment: "",
                    customDate: ""
                },
                selectedCategory: "",
                lang: {
                    default: "en"
                }
            };
        },

Here's the relevant part of the template:

<div class="row">
    <div class="d-inline col-lg-4 col-md-4 col-sm-4" padding="0px">
        Category
        <font color="red">*</font>
    </div>
    <div class="d-inline col-lg-8 col-md-8 col-sm-8" padding="0px">
        <select v-model="selectedCategory">
            <option>Social (Comment)</option>
            <option>Sport (Comment)</option>
            <option>School (Comment)</option>
            <option>Others (Comment)</option>
        </select>
    </div>
</div>

Solution

  • error is in line this.commentData.selectedCategory = listCategory;

    replace it with this.selectedCategory = listCategory;