简体   繁体   English

如何默认选择第一个项目,然后在AngularJs中突出显示所选项目

[英]how to select first item by default and then highlight the selected item in AngularJs

I am working on a app, that displays all items in left side and by default it should highlight the first item and show the item details in rightside view. 我正在开发一个应用程序,该应用程序在左侧显示所有项目,默认情况下,它应突出显示第一个项目并在右侧视图中显示该项目的详细信息。 when user clicks any item in the list it should highlight that item and show the selected item details in right side view. 当用户单击列表中的任何项目时,应突出显示该项目并在右侧视图中显示所选项目的详细信息。

here is my code (leftside view): 这是我的代码(左侧视图):

<div ng-repeat="item in itemList()| filter:{status: itemType}">
    <div  id="itemRow" ng-click="selectedItem(item)" ng-class="{'active':selectedItemid === item.id}">     
            <div id="itemRowContent">               
                    <div id="itemName" class="appFont"><b>{{item.name}}</b></div>

    </div>
 </div>
</div>

In controller: 在控制器中:

 $scope.selectedItem= function(item){
        $scope.selectedItem = item;        
        $scope.selectedItemid = item.id;       
   };

Right side view: 右侧视图:

            <div class="headersInfoRow2"> {{selectedItem.type}}:</div>
            <div class="headersInfoRow2"> {{selectedItem.price}}</div>
            <div class="headersInfoRow3"> {{selectedItem.quantity}}</div> 
 ...

... ...

currently, it is displaying selected item, how to highlight first item and display first item details in right side view by default.then highlight the slected item and display selected item details. 当前,它会显示所选项目,默认情况下如何突出显示第一项并在右侧视图中显示第一项详细信息,然后突出显示选中的项目并显示所选项详细信息。

Very simple. 很简单。

Let's say you have a class .selected which is attached to the element which is active; 假设您有一个.selected类,它连接到活动元素上; then, you can modify your HTML/Angular code to use the ng-class directive. 然后,您可以修改HTML / Angular代码以使用ng-class指令。

ng-class="{selected: $index==0}"

What you've tried is pretty similar, but it needs to be on that track. 您尝试过的内容非常相似,但必须遵循这一步。 Try this code and it should work. 试试这个代码,它应该可以工作。

One option would be to have a ternary operator in your ng-class checking to see if $scope.selectedItemid is undefined. 一种选择是让ng-class的三元运算符检查$scope.selectedItemid是否未定义。 If $scope.selectedItemid is undefined check to see if the item is the first in the repeat with $first property of ng-repeat: 如果未定义$scope.selectedItemid ,请检查该项目是否是重复项中的第一个,并具有ng-repeat的$ first属性:

ng-class="{'active':selectedItemid==undefined ? $first : selectedItemid === item.id}"

Assuming $scope.selectedItem works correctly for clicking, as you said it did, just call the function against the first item as part of the initialization of your control or immediately after loading of data. 假设$ scope.selectedItem可以正确地单击,如您所说,只需在控件初始化的过程中或在加载数据后立即针对第一个项目调用函数。 For example, at the bottom of your control file, or at the end of the function that loads data into itemList, call: 例如,在控制文件的底部,或将数据加载到itemList的函数的末尾,调用:

$scope.selectedItem( itemList[0] );

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

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