OnChange page rows using VueJS and axiosJS not working

Viewed 50

Context: Implementing Page Rows using VueJS and AxiosJS to allow users to select the number of rows to display on a table.

Problem: When selecting onChange from the dropDown box, the table rows do not change, whereas the console.log shows the correct number of records and data.

Ask: I need help to figure out what I am doing wrong.

Code JavaScript:

function get_trips_all(){
    // Initialize areas of display
    const urlParams = new URLSearchParams(window.location.search);
    const token = urlParams.get('token');
    const shipperid = urlParams.get('shipperid');
    const customerid = urlParams.get('customerid');
    let pagesize = document.getElementById('trip-pagination').options[document.getElementById('trip-pagination').selectedIndex].value;
    console.log('pagesize: '+pagesize);
    // Check token
    if(typeof token === 'undefined' || token === null){
        // redirect to login page
        window.location.href = loginPath;
    }else{
        
        new Vue({
            el: '#view-all-trips',
            data() {
                return {
                    isLoaded: false,
                    view_all_trips: [],
                    recordCount: 0,
                    pagesize: pagesize
                    
                };
                
            },
           mounted(){
                    
                    axios({
                        method: 'get',
                        url: apiClient+'/web/trips/all', 
                        params: {
                            'token': token,
                            'shipperid': shipperid,
                            'customerid': customerid,
                            'pagesize': pagesize
                        },
                        headers: {
                            'Content-Type': 'application/json;charset=UTF-8',
                            "Access-Control-Allow-Methods": "POST, GET, OPTIONS",
                            'Access-Control-Allow-Origin': apiClient,
                            'Access-Control-Allow-Headers': '*',
                            'Accept': 'application/json, text/plain;charset=UTF-8',
                        },
                        Vary: 'Origin'
                    })
                    
                    .then(response => {
                        this.isLoaded = true;
                        this.view_all_trips = response.data.trips;
                        this.recordCount = response.data.recordcount;
                    }
                    )
                    .then(
                        function(){
                            document.getElementById('trip-pagination').value = pagesize
                        }
                    )
                    .catch(
                        function (error) {
                            console.log(error);
                        });
                
            }
        });
        
    }
}

HTML

<div class="card-body" id="view-all-trips"> <!--table table-hover table-sm table-striped-->
                                   
                                    <table style="width: 100%;" class="table table-hover  horizontal-scrollable" id="myTable" v-if="isLoaded">
                                        <thead class="table-light">
                                            <tr style="
                                                        height: 55px;
                                                        line-height: 35px;
                                                        min-height: 55px;
                                                    ">
                                                <th style="padding-left: 16px;">Reference</th>
                                                <th style="padding-left: 16px;">Loading Location</th>
                                                <th style="padding-left: 16px;">Destination</th>
                                                <th style="padding-left: 16px;">Customer</th>
                                                <th style="padding-left: 16px;">Shipper</th>
                                                <th style="text-align: center;padding-left: 16px;">Loading date</th>
                                                <th style="text-align: center;padding-left: 16px;">Dispatch date</th>
                                                <th style="text-align: center;padding-left: 16px;">Trucks requested</th>
                                                <th style="text-align: center;padding-left: 16px;">Proposed rate (USD)</th>
                                                <th style="text-align: center;padding-left: 16px;">Active since</th>
                                                <th style="text-align: center;padding-left: 16px;">Status</th>
                                                <th colspan="4">Actions</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <tr style="
                                                        height: 52px;
                                                        line-height: 35px;
                                                        min-height: 52px;
                                                    " 
                                                    v-for="(trip, index) in view_all_trips" :key="trip.id">
                                                <td>{{trip.reference}}</td>
                                                <td style="padding-left: 16px;">
                                                    {{trip.pickup_location}}
                                                </td>
                                                <td style="padding-left: 16px;">
                                                    {{trip.dropoff_location}}
                                                </td>
                                                <td style="padding-left: 16px;">
                                                    {{trip.customername}}
                                                </td>
                                                <td style="padding-left: 16px;">
                                                    {{trip.shippername}}
                                                </td>
                                                <td style="text-align: center; padding-left: 16px;">
                                                    {{trip.loaded_date}}
                                                </td>
                                                <td style="text-align: center; padding-left: 16px;">
                                                    {{trip.dispatch_date}}
                                                </td>
                                                <td style="text-align: center; padding-left: 16px;">
                                                    {{trip.truck_requested}}
                                                </td>
                                                <td style="text-align: center; padding-left: 16px;">
                                                    {{trip.propposed_rate}}
                                                </td>
                                                <td style="text-align: center; padding-left: 16px;">
                                                    {{trip.trip_date_created}}
                                                </td>
                                                <td style="text-align: center; padding-left: 16px;">
                                                    <span v-if=" trip.trip_status === 'open' " class="
                                                                badge
                                                                badge-pill
                                                                badge-primary
                                                            ">{{trip.trip_status}}</span>
                                                    <span v-if=" trip.trip_status === 'loaded' " class="
                                                            badge
                                                            badge-pill
                                                            badge-success
                                                        ">{{trip.trip_status}}</span>
                                                </td>

                                                <td>
                                                    <a :id=" 'trip-detail-'+trip.reference " :reference="trip.reference"
                                                        :token="trip.token" :shipper="trip.shipperid"
                                                        v-if=" trip.tungsten_profile.includes('view_documents') "
                                                        @click="gotoTripDocuments(trip.reference, trip.token);"
                                                        style="cursor: pointer;"><i
                                                        class="fas fa-file-contract"></i></a>
                                                </td>
                                                <td>
                                                    <a :id=" 'trip-detail-'+trip.reference " :reference="trip.reference"
                                                        :token="trip.token" :shipper="trip.shipperid"
                                                        v-if=" trip.tungsten_profile.includes('view') "
                                                        onclick="viewTrip(this.id, this.reference, this.token);" style="cursor: pointer;"><i
                                                        class="fas fa-eye"></i></a>
                                                </td>
                                                <td>
                                                    <a :id=" 'trip-detail-'+trip.reference " :reference="trip.reference"
                                                        :token="trip.token" :shipper="trip.shipperid"
                                                        v-if=" trip.tungsten_profile.includes('edit') "
                                                        @click="getTripDetailUpdate(trip.reference, trip.token);selectShipperForUpdate(trip.reference); selectCustomerForUpdate(trip.reference);"
                                                        data-bs-toggle="modal" data-bs-target="#update-TripModal" style="cursor: pointer;"><i
                                                            class="
                                                                    far
                                                                    fa-edit
                                                                "></i></a>
                                                </td>

                                                <td>
                                                    <a :id=" 'trip-detail-'+trip.reference " :reference="trip.reference"
                                                        :token="trip.token" :shipper="trip.shipperid"
                                                        v-if=" trip.tungsten_profile.includes('delete') "
                                                        @click="goto_changeStatusTrip(trip.reference, 'delete');"
                                                        data-bs-toggle="modal" data-bs-target="#delete-TripModal" style="cursor: pointer;"><i
                                                            class="
                                                                    fas
                                                                    fa-trash-alt
                                                                "></i></a>
                                                </td>
                                            </tr>
                                        </tbody>
                                        
                                        <tfoot>
                                            <th>
                                                <select id="trip-pagination" onchange="get_trips_all();" >
                                                    <option value="5" :selected=" recordCount == '5' ">5</option>
                                                    <option value="10" :selected=" recordCount == '10' ">10</option>
                                                    <option value="20" :selected=" recordCount == '20' ">20</option>
                                                    <option value="50" :selected=" recordCount == '50' ">50</option>
                                                    <option value="100" :selected=" recordCount == '100' ">100</option>
                                                    <option value="all">All</option>
                                                </select>
                                            </th>
                                        </tfoot>
                                        
                                    </table>
                                </div>
1 Answers

Rely on Vue for the interactive features, it's designed to make those easier than using things like getElementById lookups.

The select component can assign the variable directly on change

<div>
  <div>Page size is {{pagesize}}</div>
  <select @change="pagesize = $event.target.value" >
    <option value="5" selected>5</option>
    <option value="10">10</option>
    <option value="20">20</option>
    <option value="50">50</option>
    <option value="100">100</option>
    <option value="all">All</option>
  </select>
</div>

Then the request code can use this.pagesize directly instead of looking it up.

  data() {
    return {
      pagesize: 5,
    };
  },
  mounted(){
    this.fetchData()
  },
  watch: {
    pagesize: function pagesize(new_val, old_val) {
      console.log('pagesize changed', new_val, old_val)
      this.fetchData()
    },
  },
  methods: {
    async fetchData(){
      console.log('fetch pagesize', this.pagesize)
    },
  },
Related