简体   繁体   English

在智能表格中设置UI选择默认值

[英]Setting UI-Select default value within a Smart Table

Setting UI-Select default value within a Smart Table 在智能表格中设置UI选择默认值

I have an Angular based web project upon which I ran into an issue. 我有一个基于Angular的Web项目,遇到了一个问题。 I am loading data into a smart table. 我正在将数据加载到智能表中。 Within the smart table definition, I have a column defined as a ui-select (dropdown). 在智能表定义中,我有一列定义为ui-select(下拉列表)。 My issue is when I load the row data. 我的问题是何时加载行数据。 I cannot get the ui-select to default to the value returned from the database. 我无法将ui-select缺省设置为从数据库返回的值。 For example, my smart table definition is as follows: 例如,我的智能表定义如下:

Col 1 Col 2 Col 3 Col 4 列1列2列3列4

R1C1 R1C2 R1C3 R1C4 – Value 1 R1C1 R1C2 R1C3 R1C4 –值1

R2C1 R2C2 R2C3 R1C4 – Value 4 R2C1 R2C2 R2C3 R1C4 –值4

R3C1 R3C2 R3C3 R1C4 – Value 2 R3C1 R3C2 R3C3 R1C4 –值2

R4C1 R4C2 R4C3 R1C4 – Value 1 R4C1 R4C2 R4C3 R1C4 –值1

My data retrieval returns from Couchbase database as follows: 我的数据检索从Couchbase数据库返回,如下所示:

[
 {
  Datavalue1: C1,
  Datavalue2: C2,
  Datavalue1: C3,
  Datavalue2: C4-id
 },
 {
  Datavalue1: C1,
  Datavalue2: C2
  Datavalue1: C3,
  Datavalue2: C4-id
 },
 {
  Datavalue1: C1,
  Datavalue2: C2
  Datavalue1: C3,
  Datavalue2: C4-id
 },
 {
  Datavalue1: C1,
  Datavalue2: C2
  Datavalue1: C3,
  Datavalue2: C4-id
 }
]

I load the table as follows 我按如下方式加载表

<tr ng-repeat='row in dG' st-select-row="row" style="white-space: nowrap" st-select-mode="multiple">

    <td>{{ C1}}</td>
     <td>{{ C2 }}</td> 
     <td> {{ C3}} </td>
     <td>
     <ui-select ng-model="row.data.filters.C4" theme="bootstrap" name="C4Col" style="width:400px">
             <ui-select-match placeholder="Select C4"> {{ $select.selected.c4Name }}
             </ui-select-match>
              <ui-select-choices repeat="C4Array in C4Array | filter: $select.search" 
               style="position: relative;top: auto;left: auto; width: inherit">
               <span ng-bind-html="C4Array.C4Name| highlight: $select.search"></span>
              </ui-select-choices>
            </ui-select>
           </td>

In my attempts to load the C4 dropdown, I have performed the following: 在尝试加载C4下拉菜单时,我执行了以下操作:

  1. used ui-option ng-selected = “C4” 用过的ui-option ng-selected =“ C4”
  2. used ui-option ng-selected = “row.data.filters.C4 = C4” 所使用的ui选项ng-selected =“ row.data.filters.C4 = C4”
  3. used ui-option ng-selected = “$select.selected.c4Name = C4Name” 使用的ui选项ng-selected =“ $ select.selected.c4Name = C4Name”
  4. ui-select ng-model="row.data.filters.C4" theme="bootstrap" name="C4Col" style="width:400px" ng-selected = “C4” ui-select ng-model =“ row.data.filters.C4” theme =“ bootstrap” name =“ C4Col” style =“ width:400px” ng-selected =“ C4”
  5. Rebuild the dG array into include C4-id and C4Name data 将dG阵列重建为包含C4-id和C4Name数据
  6. used rebuild dG array as data for attempts 1, 2, 3 使用重建dG阵列作为尝试1、2、3的数据
  7. in ui-select ng-selected similare to attempts 1, 2, 3 在ui-select ng-selected中类似于尝试1、2、3

The above list is not all of the options tried. 上面列出的不是所有尝试过的选项。 I tried so many different options that if I knew I was going to have this much difficulty, I would have saved them. 我尝试了很多不同的选择,以至于如果我知道自己将面临如此巨大的困难,那我将可以挽救他们。

I know the ng-model for the ui-select must be loaded with the default value before the dropdown would show the value. 我知道ui-select的ng-model必须在下拉列表显示值之前加载默认值。 My issue revolves around how to set the ng-model for the each row in the smart table. 我的问题围绕如何为智能表中的每一行设置ng模型。

I will appreciate any assistance in resolving this issue. 感谢您为解决此问题提供的任何帮助。

I resolved this issue by a replacing the ng-model expression to a location stored within my client controller. 我通过将ng-model表达式替换为存储在客户端控制器中的位置来解决了此问题。 I preset the location in the client controller to the database returned value (dropdown structure) and the appropriate value display as the default value of the dropdown. 我将客户端控制器中的位置预设为数据库返回的值(下拉结构),并且适当的值显示为下拉菜单的默认值。

Additional information added 10/7/2016 添加附加信息10/7/2016

After discovering my initial solution to the problem failed after adding the ability to sort the smart table, I searched for another solution. 在添加对智能表进行排序的能力后,发现问题的最初解决方案失败后,我搜索了另一种解决方案。 After several redesigns of data, data placement, and data structures, I believe I hit upon a viable solution. 经过几次数据,数据放置和数据结构的重新设计,我相信我找到了可行的解决方案。 The solution appeared so simple, that I could not believe it would have worked. 解决方案看起来是如此简单,以至于我无法相信它会奏效。 Instead of tracking the ng-model for the ui-select for the dropdown outside of the data array, I added it to the actual data returned. 我没有为数据数组外部的下拉列表跟踪ui-select的ng-model,而是将其添加到返回的实际数据中。 Since I was performing a RestAPI retrieval of the data from couchbase, I needed to place the data into an array of objects for the smart table. 因为我正在执行RestAPI的检索,所以需要将数据放入智能表的对象数组中。 I just added additional fields to the array, set the various fields to the default value. 我只是向数组添加了其他字段,将各个字段设置为默认值。 Then I changed the ng-model to the new field within the array. 然后,我将ng-model更改为数组中的新字段。 After, I was able to sort the data and the default values stayed associated to the appropriate rows. 之后,我能够对数据进行排序,并且默认值保持与适当的行相关联。

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

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