Monday 16 May 2022

Add a new column Priority to API - PUT

 Now updating priority into database

We first need to make Edit button work, then make Arrow button work.



View

Add Priority value input box

index.html

<input type="text" id="edit-priority">

site.js

updateItem() {

...

const item = {

        id: parseInt(itemId, 10),

        isComplete: document.getElementById('edit-isComplete').checked,

        name: document.getElementById('edit-name').value.trim(),

        priority: document.getElementById('edit-priority').value.trim(),

    };

Model

no change

Controller

no change

All working well.


Now let's update Arrow button.

View

function increasePriority(id) {

    const item = todos.find(item => item.id === id);

    item.priority++;

    callUpdateAPI(item);

    console.log('item to increase:', item);

}


Model

no change

Controller

no change

All good, arrow button works.


Now let's sort by priority desc.

We just need to sort array data.



Below site shows a simple sort function.

https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value

function compare( a, b ) {
  if ( a.last_nom < b.last_nom ){
    return -1;
  }
  if ( a.last_nom > b.last_nom ){
    return 1;
  }
  return 0;
}

objs.sort( compare );

We can modify it for priority and desc order.

View
function _displayItems(data) {
    data.sort(compare);

Not it shows sorted.


No comments:

Post a Comment