按键查找模型

时间:2014-05-26 11:57:36

标签: javascript angularjs

我想从按键查询的模型中获取“行”。 ¿我怎样才能实现这一目标? 我正在寻找像

这样的东西

{{mysettings.backcolor | searchby(datuak.ref)}}或类似的东西。

事实是我有一个带有数据的模型,以及带有bacground和forecolor信息的其他模型。它们之间的关系是“ref”栏。

当打印表格时,在ng-repeat上,我想获取通过id查询它的前景/背景颜色。

任何帮助或线索?

我的控制器中有两个模型:

$scope.mysettings = [
      { ref: '3CI00001', backcolor: '#000000', forecolor: '#ffffff' },
      { ref: '3CI00002', backcolor: '#5cb85c', forecolor: '#000000' }
    ];

这是另一种模式:

$scope.datuak = [
      {
        linea: '1',
        egunak:[{
          fetxa: '2014/05/19',
            turnoak: [
            { 
              turno: "1", 
              ordenes: [
                { ref: '3CI00001'},
                { of: 'OF000013'}
              ]  
            },
            { 
              turno: "2", 
              ordenes: [
                { ref:'3CI00001'},
                { of:'112233'},
                { ref:'3CI00001'}
              ]
            },
            {
              turno: "3",
              ordenes: [
                { ref:'3CI00001'}
              ]
            }
            ]},
          {fetxa: '2014/05/20'},
          {fetxa: '2014/05/21',
            turnoak: [
            { 
              turno: "1", 
              ordenes: [
                { ref: '3CI00001'},
                { of: 'OF200013'}
              ]  
            },
            { 
              turno: "2", 
              ordenes: [
                { ref:'3CI00001'},
                { of:'OF232233'},
                { of:'OF289977'}
              ]
            },
            {
              turno: "3",
              ordenes: [
                { ref:'3CI00001'},
                { of:'OF200000'},
                { ref:'3CI00001'},
                { of:'OF200000'},
                { ref:'3CI00001'}
              ]
            }
          ]},
          {fetxa: '2014/05/22'},
          {fetxa: '2014/05/23'},
          {fetxa: '2014/05/24'},
          {fetxa: '2014/05/25'}
        ]
      },
      {
        linea: '2',
        egunak:[
          { fetxa: '2014/05/19'},
          {
          fetxa: '2014/05/20',
            turnoak: [
            { 
              turno: "1", 
              ordenes: [
                { ref: '3CI00001'},
                { of: '2OF000013'}
              ]  
            },
            { 
              turno: "2", 
              ordenes: [
                { ref:'3CI00001'},
                { of:'2OF2233'},
                { ref:'3CI00001'}
              ]
            },
            {
              turno: "3",
              ordenes: [
                { ref:'3CI00001'}
              ]
            }
          ]},
          {fetxa: '2014/05/21'},
          {
          fetxa: '2014/05/22',
            turnoak: [
            { 
              turno: "1", 
              ordenes: [
                { ref: '3CI00001'},
                { of: '2OF200013'}
              ]  
            },
            { 
              turno: "2", 
              ordenes: [
                { ref:'3CI00001'},
                { of:'2OF232233'},
                { ref:'3CI00001'}
              ]
            },
            {
              turno: "3",
              ordenes: [
                { ref:'3CI00001'},
                { of:'2OF200000'},
                { ref:'3CI00001'},
                { of:'2OF200000'},
                { ref:'3CI00001'}
              ]
            }
          ]},
          {fetxa: '2014/05/23'},
          {fetxa: '2014/05/24'},
          {fetxa: '2014/05/25'}
        ]
      }
    ];

有了这两个模型,我有这样的观点:

        <tbody>
          <tr ng-repeat="lineas in datuak">
            <td class="tdlinea">Linea:{{ lineas.linea }}</td>
            <td data-ng-repeat="nireindex in [0,1,2,3,4,5,6]">
              <table class="table text-center table-condensed">
                <thead>
                  <th>Mañana</th>
                  <th>Tarde</th>
                  <th>Noche</th>
                </thead>
                <tbody>
                  <tr>
                      <td>
                        <table>
                          <tr ng-repeat="orden in lineas.egunak[nireindex].turnoak[0].ordenes" ng-if="checkStatus(lineas.egunak[nireindex].fetxa,nireindex)">
                            <td>{{ orden.ref }} {{ orden.of }}   </td>
                          </tr>
                        </table>
                      </td>
                      <td>
                        <table>
                          <tr ng-repeat="orden in lineas.egunak[nireindex].turnoak[1].ordenes" ng-if="checkStatus(lineas.egunak[nireindex].fetxa,nireindex)"><td>{{ orden.ref }} {{ orden.of }}</td></tr>
                        </table>
                      </td>
                      <td>
                        <table>
                          <tr ng-repeat="orden in lineas.egunak[nireindex].turnoak[2].ordenes" ng-if="checkStatus(lineas.egunak[nireindex].fetxa,nireindex)"><td>{{ orden.ref }} {{ orden.of }}</td></tr>
                        </table>
                      </td>                             
                  </tr>
                </tbody>
              </table>
            </td>
          </tr> <!-- lineas -->
        </tbody>

3 个答案:

答案 0 :(得分:0)

实现这一目标的简单方法是重构模型,使其行为像关联数组而不是常见数组。它应该像这样结束:

$scope.mysettings = {
  3CI00001: { ref: '3CI00001', backcolor: '#000000', forecolor: '#ffffff' },
  3CI00002: { ref: '3CI00002', backcolor: '#5cb85c', forecolor: '#000000' }
};

这样你只需通过mysettings [id] .backcolor

就可以通过它来识别对象

干杯!

答案 1 :(得分:0)

您可以查看角度js过滤器过滤器https://docs.angularjs.org/api/ng/filter/filter

如果你需要更进一步,你可以使用array.filter()方法创建自己的方法:

app.filter('searchBy', function() {
    return function( array, prop , val ) {
         // filter has polyfills for older browsers. Check underscore.js if needed
         return array.filter( function(row) {
             return row[prop] == val;
         } );
         // this returns an array. You can pick the first element with [0]
    }
} );

然后在你的循环中使用它:

{{ mySetting | searchBy : 'ref' : linea.ref }} 

答案 2 :(得分:0)

您可以做的是循环设置,然后遍历每个设置对象并返回匹配的项目:

function findSetting(s) {

   //loop through all settings-->
   for (var i = 0; i < mySettings.length; i++) {

       //if setting is an object loop through keys in object-->
       if (typeof (mySettings[i]) === 'object') {

           for (var key in mySettings[i]) {
               if (mySettings[i][key] === s) {

                   //return match-->
                   return mySettings[i];
               }
           }
       }
   }
}

Here是一个小提琴