简体   繁体   中英

Angular material slider value is true but i want to transform it to 1

My problem is that I've got a slider in my component html file. This component got the value from the postgresql database. If in the database the data column is 1 the slider is true if it's 0 the slider is false. ( True = active, False = inactive ) In my service and component i would like to update these sliders value. So if i click to the slider change the value and put it into the database. In my component i try to give this value 1 and value 0 if it's true or false. The debugger isn't show any kind of error, and i see that it post the Object to the backend but didn't write it into the database. The object that i post is {username: username1, source_id: 4, **data: 1**} , but what i got back is wrong because it seems like that.

{username: username1, source_id: 4, **data: true**} 
{username:username2, source_id: 3, **data: 0**}

So what i want is that if the slider is true the component give that value 1 and put it into the database, if the slider value is false give it 0 value and put it into the database.

HTML:

<mat-slide-toggle
class="example-margin"
[(ngModel)]='user.data'
[color]="warn"
[checked]="user.data == 'true' ? true : false"
[disabled]="disabled"
(change)=updateUserActivation(user) >
</mat-slide-toggle>

COMPONENT:

updateUserActivation(user) {
    var slidervalue = user.data
    if (user.data == true ) slidervalue = 1
    else { slidervalue = 0 };
    console.log(slidervalue)
   var newUserActivationStatus = {
     data: slidervalue,
     username: user.username,
     country: user.source_id
   };
   console.log(newUserActivationStatus);
   console.log(`Updating USER Status : ${user.username}`);
   const result = this.usercontrolService.updateUserActivation(newUserActivationStatus)
   result.subscribe(newUserStatus => {
     this.users.push(newUserStatus)
     console.log(this.users, newUserActivationStatus);
   }) 
 }

SERVICE:

updateUserActivation(data) {
            var headers = new Headers();
            headers.append('Content-Type', 'application/json');
            console.log(data)
            return this.http.put(BASE_URL + 'user/' + data.username, data, { headers: headers })
                  .map(res => res.json());           
      }

BACKEND API QUERY:

function updateUserActivationStatus(req, res, next) {
    db.none('UPDATE accounts SET data=$1 WHERE username=$2 AND source_id=$3',
    [req.body.data, req.body.username, req.body.source_id, parseInt(req.body.launched), parseInt(req.params.source_id)] )
      .then(function (user) {
        res.status(200)
          .json({
            status: 'success',
            user: user,
            message: 'Updated UserStatus'
          });

          console.log(req.body)
      })
      .catch(function (err) {
        return next(err);
      });
  }

Is it possible to post user.data value here? Need to check what data you getting from backend

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM