当前位置:   article > 正文

ExtJS——继承CheckboxGroup,添加远程获取item的功能_a-checkbox-group v3 远程获取元素

a-checkbox-group v3 远程获取元素
remote.php
  1. <?php
  2. echo json_encode(array(array('id'=>1,'name'=>'类别一','inputValue'=>'1'),array('id'=>2,'name'=>'类别二','inputValue'=>'2'),array('id'=>3,'name'=>'类别三','inputValue'=>'3')));
  3. ?>


index.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title></title>
  6. <link rel="stylesheet" type="text/css" href="resources/css/ext-all.css" />
  7. <script type="text/javascript" src="ext-all-debug.js"></script>
  8. <script type="text/javascript" src="ext-all.js"></script>
  9. <script type="text/javascript" src="bootstrap.js"></script>
  10. </head>
  11. <body>
  12. <script type="text/javascript">
  13. Ext.onReady(function(){
  14. Ext.namespace("Ext.ux");
  15. Ext.ux.RemoteCheckboxGroup = Ext.extend(Ext.form.CheckboxGroup,{
  16. url:'',
  17. boxLabel:'',
  18. inputValue:'',
  19. setValue:function(val){
  20. if(val.split){ //split() 方法用于把一个字符串分割成字符串数组
  21. val = val.split(',');
  22. }
  23. this.reset();
  24. for(var i=0;i<val.length;i++){
  25. this.items.each(function(c){
  26. if(c.inputValue==val[i]){
  27. c.setValue(true);
  28. }
  29. });
  30. }
  31. },
  32. reset:function(){
  33. this.items.each(function(c){
  34. c.setValue(false);
  35. });
  36. },
  37. getValue:function(){
  38. var val=[];
  39. this.items.each(function(c){
  40. if(c.getValue()==true)
  41. val.push(c.inputValue);
  42. });
  43. return val.join(',');
  44. },
  45. initComponent:function(ct,position){
  46. var items=[];
  47. if(!this.items){//如果没有指定items就从url远程获取数据
  48. Ext.Ajax.request({
  49. url:this.url,
  50. scope:this,
  51. async:false,
  52. success:function(response){
  53. var data=Ext.JSON.decode(response.responseText);
  54. var Items2 = this.items;
  55. if(this.panel){
  56. var columns = this.panel.items;
  57. if(columns){
  58. for(var i=0;i<columns.items.length;i++){
  59. column=columns.items[i];
  60. column.removeAll();
  61. }
  62. Items2.clear();
  63. }
  64. }
  65. for(var i=0;i<data.length;i++){
  66. var d= data[i];
  67. var chk = {boxLabel:d[this.boxLabel],name:this.name,inputValue:d[this.inputValue]};
  68. items.push(chk);
  69. }
  70. }
  71. });
  72. this.items=items;//关键一步
  73. }
  74. Ext.ux.RemoteCheckboxGroup.superclass.initComponent.call(this,arguments);
  75. }
  76. });
  77. // Ext.reg('remotecheckboxgroup',Ext.ux.RemoteCheckboxGroup);类似于别名
  78. var checksource = new Ext.ux.RemoteCheckboxGroup({
  79. name:'checksource',//名称,当客户端提交的时候,服务端会根据该名称接收值,当值为多选时post 结果如下[1,2,3],用','分隔。
  80. boxLabel:'name',
  81. inputValue:'id',
  82. url:'php/remote.php',//数据源的地址
  83. fieldLabel:'招聘来源',
  84. width:1000,//非常重要,不设置根本显示不出来
  85. style:'padding-top:3px;height:17px;'
  86. });
  87. //创建panel
  88. var panel = new Ext.Panel({
  89. title: '动态复选框',
  90. width: 1000,
  91. height: 200,
  92. frame: true,
  93. items: [checksource]
  94. });
  95. //创建窗体
  96. var win = new Ext.Window({
  97. title: '窗口',
  98. id: 'window',
  99. width: 1100,
  100. height: 374,
  101. resizable: true,
  102. modal: true,
  103. closable: true,
  104. maximizable: true,
  105. minimizable: true,
  106. items: [panel]
  107. });
  108. win.show();
  109. });
  110. </script>
  111. </body>
  112. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/322140
推荐阅读
相关标签
  

闽ICP备14008679号