【问题标题】:How to check ion-checkbox from database如何从数据库中检查 ion-checkbox
【发布时间】:2016-07-04 21:57:09
【问题描述】:

我已经填充了这个离子标签并且所有项目都未选中:

            <ion-checkbox ng-repeat="categoria in listaCategorias"
                           ng-model="categoria.checked"
                           ng-checked="categoria.checked"
                           ng-change="recuperarServicos(categoria)">
                           {{ categoria.nmCategoria }}
            </ion-checkbox>

这里是我的控制器代码,其中包含“类别 ID”列表:

//here I have the ids recovered from database that I split into an array of ids
var idsCategoria = $scope.meuanuncio.idsCategoria.trim().split(',');

if($scope.listaCategorias.length > 0)
{
    //for each item in my listaCategorias (used in ng-repeat)
    for (var i = 0; i < $scope.listaCategorias.length; i++) {

        var item = $scope.listaCategorias[i];

        //I compare id from each item with my list recovered from database
        if(idsCategoria.indexOf($scope.listaCategorias[i].idCategoria) != -1)
        {
            //If the item id exist in database list, I check the item
            item.checked = true;

            // Below there are other ways that I tried to use
            // $scope.listaCategorias[i].Selected = true;
            // $scope.listaCategorias[i].checked = true;
            $scope.listaCategorias[0].checked = true;
        }
    }
};

但我无法检查我的 ion-checkbox 项目。

我做错了什么?

谢谢。

【问题讨论】:

  • 使用标志,例如 isChecked = true 或 isChecked = false

标签: ionic-framework


【解决方案1】:
ng-model="categoria.checked"

看起来不错,但认为您不需要 ng-checked。

 var item = $scope.listaCategorias[i];
item.checked = true;

不,该项目在循环中丢失了。我看到你在尝试:

$scope.listaCategorias[i].checked = true;

您是否遇到错误或其他问题?因为这看起来像这样做的方法。

也许尝试在 ion-checkbox 周围的 div 上循环?又名

<div ng-repeat="categoria in listaCategorias">
   <ion-checkbox ng-model="categoria.checked"
                 ng-change="recuperarServicos(categoria)">
                 {{ categoria.nmCategoria }}
   </ion-checkbox> 
</div>

【讨论】:

  • 你好乔纳斯蒂。我使用您的建议将 div 放在周围并更改了项目。没啥事儿 !!!而且我没有收到任何错误。
【解决方案2】:

试试这个:

<div ng-repeat="categoria in listaCategorias track by $index">
      <ion-item class="item item-checkbox">
           <label class="checkbox">
              <input type="checkbox" ng-model="categoria.checked" ng-change="recuperarServicos(categoria)">
           </label>
         {{categoria.nmCategoria}}
      </ion-item>
</div>

控制器:

$scope.recuperarServicos = function(categoria){
        if(categoria.selected && ($scope.selectedItems.indexOf(categoria.name) < 0)){
          $scope.selectedItems.push(categoria.name);
        }else{
            $scope.selectedItems.splice($scope.selectedItems.indexOf(categoria.name), 1);
        }
    };

希望这对您有所帮助..以某种方式..!

【讨论】:

  • 嗨。我的问题是来自数据库的路径。你的例子很好,但我的问题是当我有一个'categorias' id 列表(listaCategorias)并且我需要在加载视图时检查所有项目时如何检查我的'categorias'。你现在明白了吗?
  • 你的意思是,像.. 你有 10 个类别.. 而你的类别是 4..so..当视图加载时.. 应该只检查 4 个类别,你也可以通过以下方式更新更多选择更多..!我对..!?
  • 是的!确切地。我正在从数据库中获取数据并使用这些数据填充此项目和其他表单字段,以进行用户更新。所以'categorias'项将只检查数据库数据项,用户将能够在表单上更改它。
【解决方案3】:

我的问题是当我将我的项目数组归因于 $scope.listaCategorias 时。

我正在这样做:

$scope.listaCategorias = listaCategorias;

但我需要这样做:

$scope.listaCategorias.push.apply($scope.listaCategorias, listaCategorias);

我正在构建一个内部包含已选中属性的数组,但是当我关联我构建的列表时,我正在关联第一个未设置已选中属性的数组。

现在让我展示我的代码。

我的看法:

        <div ng-repeat="item in listaCategorias track by $index">
              <ion-item class="item item-checkbox">
                   <label class="checkbox">
                      <input type="checkbox" ng-model="item.checked" ng-checked="item.checked" ng-change="recuperarServicos(item)">
                   </label>
                  {{ item.nmCategoria }}
              </ion-item>
        </div> 

我的控制器:

//here I get all my 'categorias' from datatable
listaCategorias = appFactory.recuperarCategorias();

//If list is not null go ahead   
if(listaCategorias != null) {

    //split into an array all my 'categoria' ids
    var idsCategoria = $scope.meuanuncio.idsCategoria.split(',');

    //if list has items go ahead
    if(listaCategorias.length > 0) {

        for (var i = 0; i < listaCategorias.length; i++) {

            //if 'categoria' id exists in datatable list set true, else false
            if(idsCategoria.indexOf(listaCategorias[i].idCategoria) != -1) {
                listaCategorias[i].checked = true;
            }
            else {
                listaCategorias[i].checked = false;
            }
        }
    };

    //Here is the point !!! I need to load my $scope variable this way to build all my items correctly
    $scope.listaCategorias.push.apply($scope.listaCategorias, listaCategorias);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    相关资源
    最近更新 更多