简体   繁体   English

Angularjs根据DB值动态突出显示td单元格

[英]Angularjs highlight td cell dynamically based on DB value

How to highlight TD cell dynamically based on DB value of weekday and shift number using below code. 如何使用下面的代码基于工作日的DB值和移位数动态突出显示TD单元格。

Able to display weekday and Date using function getDates(). 能够使用函数getDates()显示工作日和日期。 after binding to TD cell as $scope.dates = getDates(); 绑定到TD单元格后,$ scope.dates = getDates();

For example if the below function getCurrentShiftDtls returns weekday = fri and shift no = 2 then I want to highlight(change Bg color) of 例如,如果下面的函数getCurrentShiftDtls返回weekday = fri并且shift no = 2那么我想要突出显示(更改Bg颜色)

<td colspan="3" id="tdFri"> and <td id="tdFri2">B</td>

How can I do this 我怎样才能做到这一点

code:
function getCurrentShiftDtls(PlId, A_Id) {
  $http({
    method: 'GET',
    url: 'http://xxx/api/Shift/GetShiftDtls',
    params: {
      PlId: PlId,
      A_Id: A_Id
    },
    headers: {
      'Content-Type': 'application/json; charset=utf-8',
      'dataType': 'json'
    }
  }).then(function successCallback(response) {
    var ShiftDtls = response.data;
    shiftDay = ShiftDtls[0].WeekDay; // shiftDay = Fri
    shiftNo = ShiftDtls[0].ShiftNo; // shiftNo = 2
  }, function errorCallback(response) {
    // alert(response);
  });
}
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
    <table class="tbWeek">
      <tbody>
        <tr>
          <td colspan="3" id="tdMon">{{dates[0]}}</td> // Mon 3/25
          <td colspan="3" id="tdTue">{{dates[1]}}</td> // Tue 3/26 .. ..
<td colspan="3" id="tdFri" ng-class="{'highlight': shiftDay === 'Fri'} ">{{dates[4]}}</td>
          <td colspan="3" id="tdSun">{{dates[6]}}</td> // Sun 3/31
        </tr>
        <tr>
          <td id="tdMon1">A</td>
          <td id="tdMon2">B</td>
          <td id="tdMon3">C</td>
          ... ...
          <td id="tdSun1">A</td>
          <td id="tdSun2">B</td>
          <td id="tdSun3">C</td>
        </tr>
      </tbody>
    </table>

     <style>
    .highlight {
                background: red;
            }

So you have an ID of tdFri and tdFri2 , and you get data back that is shiftDay = Fri and shiftNo = 2 所以你有一个ID为tdFritdFri2 ,你得到的数据是shiftDay = FrishiftNo = 2

It seems to me that you just need to combine these and make some strings to select the elements by ID. 在我看来,你只需要组合这些并制作一些字符串来按ID选择元素。

var selectors = '#td' + shiftDay + ', #td' + shiftDay + shiftNo;  //'#tdFri, #tdFri2'
var elements = document.querySelectorAll(selectors);
elements.forEach(function(el) {
    el.classList.add('highlighted');
});
.highlighted {
    background: yellow;
}

Or perhaps a better more "angularJS" way to do this would be to put those values onto the angular scope and then add a class in the template 或者更好的“angularJS”方法可能是将这些值放在角度范围内,然后在模板中添加一个类

<td colspan="3" id="tdFri" ng-class="{'highlight': shiftDay ==='Fri'}">
or maybe
<td colspan="3" id="tdFri" ng-class="{'highlight': shiftDay === date[4]}">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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