如何更改extJS组合框下拉列表中的数据?

dzhpxtsq  于 2022-09-26  发布在  其他
关注(0)|答案(2)|浏览(221)

如何更改extJS组合框下拉列表中的数据?
我有组合框,我正在尝试加载数据。我想对一些Reg表达式的数据进行分类。
这是我的Stroe代码。

  1. Ext.define("MyApp.store.combo.country", {
  2. extend: "Ext.data.Store",
  3. alias:"widget.country",
  4. //autoLoad:true,
  5. fields: [{
  6. name: 'name',type:'string'
  7. },{
  8. name: 'key',type:'int'
  9. }],
  10. proxy: {
  11. type: 'ajax',
  12. method: 'GET',
  13. url: '/myCountry/getName',
  14. reader: {
  15. type: 'json'
  16. }
  17. },
  18. listeners: {
  19. beforeload ( store, operation, eOpts ) {
  20. console.log("combo before load");
  21. },
  22. load: function (_this, records, successful, operation, eOpts) {
  23. var length = records.length;
  24. var htmlRegex = new RegExp("<(\"[^\"]*\"|'[^']*'|[^'\">])*>");
  25. for(var i=0; i<length; i++){
  26. if(htmlRegex.test(records[i].data.name)){
  27. records[i].data.name = records[i].data.name.replace( /(<([^>]+)>)/ig, '');
  28. }
  29. }
  30. }
  31. }
  32. });

现在,当我单击COmbobox时,我看不到下拉列表中的数据是sanatize(Executing without passing RegExp)。这是第二次工作良好。
因此,我的问题是,在这种情况下,我如何更改我的数据。
我已经尝试了加载方法中可以看到的内容。(即使在加载前方法中也没有发生任何事情。)
任何解决方法

dluptydi

dluptydi1#

在我看来,最好的方法是使用计算功能。
这可以确保每次加载或更改存储中的记录时,都会进行正则表达式验证。唯一的缺点是你有另一个领域。

  1. Ext.define("MyApp.store.combo.country", {
  2. extend: "Ext.data.Store",
  3. alias: "widget.country",
  4. fields: [{
  5. name: 'regexedName',
  6. type: 'string',
  7. calculate: function (data) {
  8. const htmlRegex = new RegExp("<(\"[^\"]*\"|'[^']*'|[^'\">])*>");
  9. if (htmlRegex.test(data.name)) {
  10. return data.name.replace(/(<([^>]+)>)/ig, '');
  11. } else {
  12. return data.name;
  13. }
  14. }
  15. }, {
  16. name: 'name', type: 'string'
  17. }, {
  18. name: 'key', type: 'int'
  19. }],
  20. proxy: {
  21. type: 'ajax',
  22. method: 'GET',
  23. url: '/myCountry/getName',
  24. reader: {
  25. type: 'json'
  26. }
  27. }
  28. });
展开查看全部
ngynwnxp

ngynwnxp2#

您可以在所需字段上定义convertcalculate配置,从而完全避免使用存储侦听器。
主要区别在于,第一个选项可以让您快速转换字段的值,而最后一个选项可以根据其他记录信息的详细说明创建新的属性/字段。

  1. // Example *convert* to change the case of a property
  2. {
  3. name: 'firstName',
  4. type: 'string',
  5. convert: function (value) {
  6. return value.toUpperCase();
  7. }
  8. }
  9. // Example *calculate* to create the new property "fullName" based on other properties
  10. {
  11. name: 'fullName',
  12. type: 'string',
  13. convert: function (data) {
  14. return data.firstName + ' ' + data.lastName;
  15. }
  16. }
展开查看全部

相关问题