0

我有一个自定义表格,其中包含通过在 Rows 上设置值的模式弹出窗口执行的操作。事情大部分都很好(对 Foo 和 Bar 的更新被发送到后端并设置在数据库中,重新加载页面会从数据库中提取数据并显示 foo/bar 已正确设置)。唯一不太好的部分是关于 Foo 的设置,表中的值不会更新。栏得到更新/反应。(模板使用 foo.name 和 bar.id)。有人对如何让 Foo 在表格中更新有任何想法吗?我已将 mustache 模板更改为使用 {{ foo.id }} 并且它会更新,但我需要 foo.name。

<template>
  <div>
    <c-dialog
      v-if="foo_modal"
      title="Set Foo"
      :actions="foo_modal_options.actions"
      @cancel="foo_modal = null">
      <slot>
        <div class="form-group">
          <label>Foo:</label>
          <select class="form-control" v-model="foo_modal.thing.foo.id">
            <option v-for="foo in foos" :key="foo.id" :value="foo.id">{{ foo.name }}</option>
          </select>
        </div>
      </slot>
    </c-dialog>
    <c-dialog
      v-if="bar_modal"
      title="Set Rod/Stick"
      :actions="bar_modal_options.actions"
      @cancel="bar_modal = null">
      <slot>
        <div class="form-group">
          <label>Rod:</label>
          <select class="form-control" v-model="bar_modal.thing.rod.id">
            <option v-for="bar in bars" :key="bar.id" :value="bar.id" v-if="bar.type === 'rod'">{{ bar.id }}</option>
          </select>
        </div>
        <div class="form-group">
          <label>Stick:</label>
          <select class="form-control" v-model="bar_modal.thing.stick.id">
            <option v-for="bar in bars" :key="bar.id" :value="bar.id" v-if="bar.type === 'stick'">{{ bar.id }}</option>
          </select>
        </div>
      </slot>
    </c-dialog>

    <c-table-paginated
      class="c-table-clickable"
      :rows="grid.data"
      :columns="grid.columns"
      :actions="grid.actions"
      :key="componentKey">
    </c-table-paginated>
  </div>
</template>

<script>
    import fooModal from '../../components/fooModal.vue';
    import barModal from '../../components/barModal.vue';
    import CTablePaginated from "../../components/custom/cTable/cTablePaginated";
    import cTooltip from '../../components/custom/cTooltip/cTooltip.vue';
    import cDialog from '../../components/custom/cDialog/cDialog.vue';
    import moment from 'moment';

    export default {
        components: { CTablePaginated, cTooltip, cDialog },
        methods: {
            loadData() {
                let that = this;
                that.$http.get('/things', { params: that.param || {} })
                    .then(function (things) {
                        that.things = things.data;
                        that.grid.data = that.things;
                    });
            },
            setBar(thing_id, options, cb) {
                let that = this;
                this.$http.patch(`/things/${thing_id}`, { rod_id: options.rod, stick_id: options.stick })
                    .then(function () {
                        cb();
                    });
            },
            setFoo(thing_id, options, cb) {
                let that = this;
                this.$http.patch(`/things/${thing_id}`, { foo_id: options.foo_id })
                    .then(function () {
                        cb();
                    })
            },
        },
        data() {
            return {
                componentKey: 0,
                things: null,
                foos: [],
                bars: [],
                foo_modal: null,
                foo_modal_options: {
                    actions: [
                        {
                            label: "Save",
                            class: "btn-primary",
                            action: (function (ctx) {
                                return function () {
                                    const thing = ctx.foo_modal.thing;
                                    const options = {
                                        foo_id: thing.foo.id,
                                    };
                                    ctx.setFoo(thing.id, options, function () {
                                        ctx.foo_modal = null;
                                    });
                                }
                            })(this)
                        },
                        {
                            label: "Cancel",
                            action: (function (ctx) {
                                return function () {
                                    ctx.foo_modal = null;
                                }
                            })(this)
                        }
                    ]
                },
                bar_modal: null,
                bar_modal_options: {
                    actions: [
                        {
                            label: "Save",
                            class: "btn-primary",
                            action: (function (ctx) {
                                return function () {
                                    const thing = ctx.bar_modal.thing;
                                    const options = {
                                        rod: thing.rod.id,
                                        stick: thing.stick.id
                                    };
                                    ctx.setBar(thing.id, options, function () {
                                        ctx.bar_modal = null;
                                    });
                                }
                            })(this)
                        },
                        {
                            label: "Cancel",
                            action: (function (ctx) {
                                return function () {
                                    ctx.bar_modal = null;
                                }
                            })(this)
                        }
                    ]
                },
                grid: {
                    data: [],
                    columns: [
                        {
                            label: "Foo",
                            value: function (row) {
                                if (!row.foo) return "No foo set";
                                return `${row.foo.name }`;
                            }
                        },
                        {
                            label: "Rod/Stick",
                            value: function (row) {
                                if (!row.rod && !row.stick) return "No rod/stick set";
                                if (!row.rod) return `No rod set/${row.stick.id}`;
                                if (!row.stick) return `${row.rod.id}/no stick set`;
                                return `${row.rod.id}/${row.stick.id}`;
                            }
                        }
                    ],
                    actions: [
                        {
                            label: "Set Foo",
                            visible: function (thing) {
                                return !thing.active;
                            },
                            action: (function (ctx) {
                                return function (thing) {
                                    if (!thing.foo) thing.foo = {};
                                    ctx.foo_modal = {
                                        thing: thing
                                    };
                                }
                            })(this)
                        },
                        {
                            label: "Set Bar",
                            visible: function (thing) {
                                return !thing.active;
                            },
                            action: (function (ctx) {
                                return function (thing) {
                                    if (!thing.rod) thing.rod = {};
                                    if (!thing.stick) thing.stick = {};
                                    ctx.bar_modal = {
                                        thing: thing
                                    };
                                }
                            })(this)
                        },
                    ],
                }
            };
        },
        props: {
            title: {
                type: String
            },
            param: {
                type: Object,
                required: true
            },
            events: {
                type: Object,
                required: true
            }
        },
        created() {
            let that = this;
            this.loadData();
            this.$http.get('/bars')
                .then(function (bars) {
                    that.bars = bars.data;
                });
            this.$http.get('/foos')
                .then(function (foos) {
                    that.foos = foos.data;
                });
        },
    }
</script>
4

1 回答 1

0

如果其中任何一种可以帮助您,您可以尝试两种可能性。

  1. 您可以使用 Vuejs 的 this.$set 方法来设置值以进行深度反应。点击这里。
  2. 您可以使用 this.$nextTick(()=>{ // 在此处设置您的变量 })。点击这里。
于 2019-10-10T13:19:49.687 回答