0

In the application, we implemented the laravel-vue-pagination and in our pagination our columns are checkbox and paid amount(input field) where the checkbox is what user want to pay the item.

Once the page is load, it will save in one variable which is called items.

enter image description here

We have an event on clicking the checkbox where when the user check it will push to another variable which is called selected_items. When it is uncheck it will remove the value in the selected_items

getPerSelectedItems(i,purchase_item_id){
  if(this.items.data[i].checked == true)
  {
    this.selected_items.push({
      amount_due: this.items.data[i].amount_due,
      balance: this.items.data[i].balance,
      checked: this.items.data[i].checked,
      date_due: this.items.data[i].date_due,
      net_of_vat: this.items.data[i].net_of_vat,
      number: this.items.data[i].number,
      paid_amount: this.items.data[i].paid_amount,
      purchase_item_id: this.items.data[i].purchase_item_id,
      purchase_tag: this.items.data[i].purchase_tag,
      selected_chart_of_account: { id: this.items.data[i].selected_chart_of_account.id, name: this.items.data[i].selected_chart_of_account.code+" : "+this.items.data[i].selected_chart_of_account.name },
      selected_responsibility_center: { id: this.items.data[i].selected_responsibility_center.id, name: this.items.data[i].selected_responsibility_center.name },
      selected_wht_list: { id: this.items.data[i].selected_wht_list.id, name: this.items.data[i].selected_wht_list.name, rate: this.items.data[i].selected_wht_list.rate },
      total_amount_payable: this.items.data[i].total_amount_payable,
      total_wht: this.items.data[i].total_wht,
      transaction: this.items.data[i].transaction,
      transaction_id: this.items.data[i].transaction_id,
      wht_payable: this.items.data[i].wht_payable,
      with_tax: this.items.data[i].with_tax
    })
  }else{
    this.selected_items = this.selected_items.filter(function( obj ) {
        // return obj.purchase_item_id !== this.items.data[i].purchase_item_id;
        return obj.purchase_item_id !== purchase_item_id;
    });
  }
},

For example I checked the two items, the variable items will look like this.

enter image description here

Below is the selected_items.

enter image description here

Now my question is it possible to change the index of selected_items? In my example above, the selected_items should be like this

selected_items: [
   5: {},
   6: {}
]

Not like this

selected_items: [
       0: {},
       1: {}
    ]
Angel
  • 966
  • 4
  • 25
  • 60

2 Answers2

1

Javascript array cant skip indexes or have "string indexes". A Javascript Array is exclusively numerically indexed. When you set a "string index", you're setting a property of the object.

If you set the index 5 and 6 of an array, it will look like this.

selected_items: [
   0: undefined,
   1: undefined,
   2: undefined,
   3: undefined,
   4: undefined,
   5: {},
   6: {}
]
N69S
  • 16,110
  • 3
  • 22
  • 36
0

this.selected_items.push() will always set numeric key index.

You should use this to set a specific index.

this.selected_items[i] = this.items.data[i];
Priyanka
  • 171
  • 1
  • 4