ag-grid cellEditor选择对象值

时间:2016-08-08 16:37:59

标签: typescript ag-grid

我想从用户列表中进行选择:

user.ts

 searchButton1.Click += delegate
 {
      connection.Open();
      MySqlCommand createCommand = new MySqlCommand("SELECT * FROM reg_user WHERE mob LIKE '%+380675090373%'", connection);
      createCommand.ExecuteNonQuery();
 };

列定义如下所示:

System.ArgumentException was unhandled by user code
  HResult=-2147024809
  Message='windows-1252' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.
Parameter name: name
  ParamName=name
  Source=mscorlib
  StackTrace:
       at System.Globalization.EncodingTable.internalGetCodePageFromName(String name)
       at System.Globalization.EncodingTable.GetCodePageFromName(String name)
       at System.Text.Encoding.GetEncoding(String name)
       at MySql.Data.MySqlClient.CharSetMap.GetEncoding(DBVersion version, String CharSetName)
       at MySql.Data.MySqlClient.MySqlField.SetFieldEncoding()
       at MySql.Data.MySqlClient.MySqlField.SetTypeAndFlags(MySqlDbType type, ColumnFlags flags)
       at MySql.Data.MySqlClient.NativeDriver.GetColumnData(MySqlField field)
       at MySql.Data.MySqlClient.NativeDriver.GetColumnsData(MySqlField[] columns)
       at MySql.Data.MySqlClient.Driver.GetColumns(Int32 count)
       at MySql.Data.MySqlClient.ResultSet.LoadColumns(Int32 numCols)
       at MySql.Data.MySqlClient.ResultSet..ctor(Driver d, Int32 statementId, Int32 numCols)
       at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
       at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
       at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
       at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
       at Milano.NewOrder.<>c__DisplayClass0_0.<.ctor>b__0(Object <sender>, RoutedEventArgs <e>)
  InnerException: 

我希望能够从列表中选择一个用户并获取cellValueChanged对象。

是否有一个选项export class User { constructor (public id: number, public userName : string){} } 将是字段而不是字符串值,而this.columns = [ {headerName: "Assigned", field:"user.userName", editable: true ,cellEditor: "select", cellEditorParams: {values : this.users.map(u=> u.userName)}, ] 仍会显示在单元格中?

3 个答案:

答案 0 :(得分:2)

最后,我找到了一种解决方法,让'select'keyvalue合作。

var colorsNames = [];
colors.forEach(color=> {
  colorsNames.push(color.name);
})
...

{
  headerName: "Color",
  field: "colorId",
  width: 150,
  editable: true,
  cellEditor: 'select',
  cellRenderer: function (data: any) {
    var color = colors.find(color => color.id == data.value || color.name == data.value);
    // here I've added the check for 'color.id' and 'color.name' because initailly from DB will com the id and afterwards form selectparams will come the name
    return color.name;
  },
  onCellValueChanged: function (data: any) {
    /**
     * because 'select' does not offer us the possibility to use 'key-value' as traditional,
     * we will use only values in 'select' and changed to 'id' when will be saved.
     */
    var serviceTypeName = data.data.serviceTypeId;
    data.data.serviceTypeId = serviceTypes.find(serviceType => serviceType.name == serviceTypeName).id;
  },
  cellEditorParams: {
    values: colorsNames
  }
},

我们的想法是,在select params内,我们只会提供字符串,我们会尝试根据id找到对象的name。重要的是,我们同意name是唯一的字段。

在努力让它运作之后,我意识到select确实是一个非常糟糕的解决方案。工作不正常,我不建议使用它。

@Yonatan Lilling如有任何问题,请告诉我。

答案 1 :(得分:2)

我在https://www.ag-grid.com/javascript-grid-reference-data/?framework=angular#gsc.tab=0

中找到了javascript的解决方案

我创建了我的数组:

var Etat_acces = {"1": "Annulée", "2": "Validée", "3": "A valider CEX", "4": "Demandée", "5":"Initialisée"};

和我的columnDefs:

{
headerName: "Etat Ni", field: "etat_acces", editable: true, cellEditor:'select',

            cellEditorParams: {
                values: extractValues(Etat_acces)
            },
            valueFormatter: function (params) {
                return lookupValue(Etat_acces, params.value);
            },
            valueParser: function (params) {
                return lookupKey(Etat_acces, params.newValue);
            }

}

和三个功能:

function extractValues(mappings) {
return Object.keys(mappings);
}

function lookupValue(mappings, key) {
    return mappings[key];
}

function lookupKey(mappings, name) {
    for (var key in mappings) {
        if (mappings.hasOwnProperty(key)) {
            if (name === mappings[key]) {
                return key;
            }
        }
    }
}

我希望这可能有用;)

答案 2 :(得分:1)

传递带有字符串化对象的数组,并在值格式化程序中解析回字符串化对象以获取json对象。可以用于简单的对象。

使用字符串对象创建数组

someObject.forEach(element => {
  someArray.push(JSON.stringify(element))       
});

在值格式化程序中调用解析JSON对象中的字符串化对象的函数

valueformatter : jsonValueFormatter

用于复制对象的功能

function jsonValueFormatter(params){
  obj = JSON.parse(params.value)
  return obj.someProperty;
}