行选择和分页-Vue

时间:2019-05-07 11:57:25

标签: javascript arrays vue.js pagination storage

使用vue,我将数据库显示在表中。 我的表包含很多行。因此,我使用分页。 使用选择功能,我可以选择多行并将它们存储在数组中。

问题:

  • 我在表格中选择了一些行。该行中的数据存储在名为selected的数组中。我让它们出现在我的桌子旁边。 使用分页,我转到下一页。我的数据仍存储在此数组中。现在,当我选择另一行时。我的阵列已清空, 我在本节中的数据不见了。

我该如何解决?为什么会这样?有没有办法存储这些数据,以免丢失? 我错过了什么?

亲爱的,您找到了我的代码。谢谢您的帮助。

Vue代码

   <!-- Main table -->
<b-row align-v="start">
    <b-col sm="8">
        <b-table class="col-sm-12"
            show-empty
            striped hover
            selectable
            stacked="md"
            :items="values"
            :fields="fields"
            :select-mode="selectMode"
            :current-page="currentPage"
            :filter="valueSearch"
            :sort-by.sync="sortBy"
            :sort-desc.sync="sortDesc"
            :sort-direction="sortDirection"
            @filtered="onFiltered"
            @row-selected="rowSelected"

            <template slot="name" slot-scope="row">
                {{row.item.name}}
            </template>
            <template slot="id" slot-scope="row">
                {{ row.item.id }}
            </template>
            <template slot="ref" slot-scope="row">
                {{row.item.ref}}
            </template>
            <template slot="specific" slot-scope="row">
                {{row.item.specific}}
            </template>
            <template slot="iso" slot-scope="row">
                {{row.item.iso}}
            </template>
            <template slot="tax" slot-scope="row">
                {{row.item.tax}}
            </template>
            <template slot="amount" slot-scope="row">
                <span class='dot largerSize' :class="statusColor[range(row.item.amount)]"></span>
            </template>
        </b-table>
    </b-col>
    <b-col sm="4">
        Here comes the value selected. 
        <div v-for="selectedvalue in selected" :key="selectedvalue.id">
            {{ selectedvalue.id }} - {{selectedvalue.name}}
            {{selected}}

        </div>
    </b-col>
</b-row>
 <!-- How is sorted? & Pagnation -->
<b-row align-v="end" class="mb-2">
    <b-col>
        Sorting By: <b>{{ sortBy | capitalize }}</b>, Sort Direction:
        <b>{{ sortDesc ? 'Descending' : 'Ascending' }}</b>
    </b-col>
    <b-col>
        <b-pagination v-model="currentPage" :total-rows="totalRows" :per-page="perPage" class="my-0"></b-pagination>
    </b-col>
    <b-col>One of three columns</b-col>
</b-row>

Javascript

<script>   
export default {
    props: ['valuedata','minamount','maxamount'],
    data() {
        return {
            values: this.valuedata,
            statusColor: ["red","orange","green"],
            totalRows: 1,
            currentPage: 1,
            perPage: 10,
            pageOptions: [5, 10, 20, 50,100],
            sortBy: "id",
            sortDesc: false,
            sortDirection: 'asc',
            filter: null,
            selectMode: 'multi',
            fixed: true,
            selected: [],
          fields: {
                id: {
                    label: "Id",
                    sortable: true
                },
                name: {
                    label: "Name",
                    sortable: true
                },
                ref: {
                    label: "Ref",
                    sortable: true
                },
                iso: {
                    label: "Iso",
                    sortable: true
                },
                tax: {
                    label: "Tax",
                    sortable: true
                },
                specific: {
                    label: "specific",
                    sortable: true
                },
                amount: {
                    label: "amount",
                    sortable: true
                }                    
            }
        } 
    },
     computed:{ 
        hits: function(){
            var hits = this.values.length
            return hits
        }            
    },
    mounted () {
        this.totalRows = this.values.length

    },
     methods: {
        onFiltered(filteredItems) {
            // Trigger pagination to update the number of buttons/pages due to filtering
            this.totalRows = filteredItems.length
            this.currentPage = 1
        },
        // Option: enables the storage of selected rows
        rowSelected(items){
            console.log(items[0].name)
            this.selected = items
        },
        // Function: color code amount
        range: function(x){
            if (x < this.minamount){
                return 0;
            }else if(x >= this.minamount && x <= this.maxamount ){
                return 1;
            }else if(x >= this.maxamount ){
                return 2;
            }
        }
    },
    filters: {
        // Function: captitalize the first letter
        capitalize: function (value) {
            value = value.toString()
            return value.charAt(0).toUpperCase() + value.slice(1)
         }
    }
}

1 个答案:

答案 0 :(得分:1)

据我了解,您替换了HttpSession session = request.getSession(false);//checking if there is session or not if(session!=null) { session.invalidate();// if session is not null then invalidate it } response.sendRedirect(request.getContextPath() + "/index.jsp"); 中的所有项目:

selected

您想要做的是添加值,而不是替换它们,所以恕我直言,您应该像这样:

// Option: enables the storage of selected rows
rowSelected(items){
    console.log(items[0].name)
    this.selected = items
},