【问题标题】:Set pre-defined value for filter in Kendo UI Grid在 Kendo UI Grid 中设置过滤器的预定义值
【发布时间】:2013-11-28 09:47:02
【问题描述】:

我想在剑道网格的过滤器中设置用户定义的搜索值。一旦用户打开过滤器,该值就会被放入搜索框中。任何建议将不胜感激。

这与Set default filter for Kendo UI Grid 的问题类似,只是我使用的是 Angular js 并且我想要一个用户定义的字符串过滤器值:

这就是我构建网格的方式。我正在使用 angular js 创建一个具有自定义属性的 div。最值得注意的属性是sg-grid(剑道网格本身)、sg-filterable(设置为 true 表示该网格应该是可过滤的)和sg-predefine-filter(也设置为 true 表示该网格的过滤器应该有一个字符串打开时在搜索框中输入):

  1. 标记

    <div sg-grid sg-data="api/grid/accounts"
     sg-columns="accountId,name,pricingFrequency,shortName,status"
     sg-filterable="true"
     sg-predefine-filter-value="true"                     
    </div>
    
  2. 脚本(此处简化为演示)

    angular.module('sgComponents').directive('sgGrid', [         
       return {
          restrict: 'AE',
          scope: { filterable: @sgFilterable, predefineFilterValue: @sgPredefineFilterValue},
          template: '<div class="sg-grid">\
                        <div class="pager-bar">\
                           <div></div>\ // THE KENDO GRID
                        </div>\                                
                     </div>',
          link: function(scope, element, attrs) {
             buildGrid();
             function buildGrid() {
                var grid = element.find(':nth-child(2)'); // 2nd DIV IN THE TEMPLATE
                var gridOptions = buildGridOptions(scope, attrs, grid);
                grid.kendoGrid(gridOptions); // build the grid
              };
              /**
               Builds the options for the grid
              */
              function buildGridOptions(scope, attrs, grid) {
                 if (scope.filterable === 'true') {
                    opts.filterable = {};
                    opts.filterable.operators = {};
                    opts.filterable.operators.string = {}
                    if (scope.predefineFilterValue === 'true') { // set a pre-defined value if true
                       opts.filterable.operators.string = {
                          eq: 'Is equal to', value:'Test'
                       }
                    } else { // just show the filter option
                       opts.filterable.operators.string = {
                          eq: 'Is equal to'
                       }
                    }                                                 
                 }
              }
    
           }   
       };                   
    ]);
    
  3. 这是控制台日志的图像:

结果。如您所见,我的值被添加为另一个过滤器选项。我不要这个,我要它在输入框中作为一个值!

【问题讨论】:

    标签: angularjs kendo-ui kendo-grid


    【解决方案1】:

    终于找到了一个kendo forum question,它让我朝着正确的方向前进!

    解决方案不是在构建网格时添加预设的过滤器值,而是在网格构建完成后使用 kendoGrid.dataSource.filter 对象添加:

    angular.module('sgComponents').directive('sgGrid', [         
       return {
          restrict: 'AE',
          scope: { filterable: @sgFilterable, predefineFilterValue: @sgPredefineFilterValue},
          template: '<div class="sg-grid">\
                    <div class="pager-bar">\
                       <div></div>\ // THE KENDO GRID
                    </div>\                                
                 </div>',
          link: function(scope, element, attrs) {
             buildGrid();
             function buildGrid() {
                //code same as in original question
                grid.kendoGrid(gridOptions); // build the grid
             };
             /*
              * Builds the options for the grid
              */
             function buildGridOptions(scope, attrs, grid) {             
                //code same as in original question
             }
    
             /*
              * the grid has finished building so 
              * now get hold of it and pre-set the 
              * filter value.
              * The values (the field to filter, the type of filter and the value) 
              * are hard-coded here but ultimately would 
              * come from a JSON object on the scope, constructed
              * using values from the model
             */
             kendoGrid = gridElem.data('kendoGrid'); // the grid
    
             //If the attribute to pre-set a filter value is true...             
             if (scope.predefineFilterValue === 'true') {             
                var ds = kendoGrid.dataSource; // the datasource object has a filter object
                ds.filter([
                {
                   "logic":"or", // could be 'and'
                   "filters":[{
                      "field":"accountId", // the column you want to filter
                      "operator":"eq", // the type of filter
                      "value":105 // the value, hard-coded for testing
                   }]
                }
             }
          }
       }                           
    ]);
    

    【讨论】:

      【解决方案2】:

      您应该在网格的数据源上指定filter 选项。

      var dataSource = new kendo.data.DataSource({
          data: [
              { name: "Jane Doe", age: 30 },
              { name: "John Doe", age: 33 }
          ],
          filter : [{
              field: "name", operator: "eq", value: "John Doe"
          }]
      });
      

      这应该可以解决问题。

      【讨论】:

      猜你喜欢
      • 2012-12-28
      • 2017-08-01
      • 1970-01-01
      • 2014-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多