SlickGrid列类型

时间:2013-05-25 17:37:35

标签: javascript slickgrid

我想为SlickGrid过滤器做自己的过滤功能,它通常会统一并可以99%使用。我在Slickgrid中错过了什么 - 列中使用了哪种类型的数据?也许它已经存在,但在审查了我没有找到的来源之后。如果存在 - 如果你引导我走向真正的道路,我将感激不尽。 Slick.Editors类型?但是,如果列不是用于编辑? ...
在SlickGrid的示例中,通常使用不允许数据类型的过滤器,仅存在具有具体字段Id的一些示例。通常,数据是字符,日期,布尔值和数字。 对于nums类型,我想用<,>来改进过滤器和其他数字操作数符号,可以完成相同的日期类型。此时我只能使用字段ID来执行此操作 - 我可以使用fieldIds和类型指示自己的全局数组,然后从中识别列的类型。但是这个解决方案并不清楚 - 如果从网格中检测列类型会更好。

非常感谢任何帮助和想法!

增加:

经过一些搜索后发现,我可以使用SlickGrid数据值类型。我是Javascript的新手,所以欢迎任何有关改进源代码的帮助和建议......: - )

这是我的来源:

 function filter( item ) 
  {
     for ( var columnId in colFilt ) 
     {
        if ( columnId !== undefined && colFilt[ columnId ] !== "" ) 
        {
           var c   = grid.getColumns()[ grid.getColumnIndex( columnId ) ];
           var typ = varType( item[ c.field ] );
           if ( typ == "N" || typ == "D" ) 
           {
              var arr = date_num_filter( colFilt[ columnId ] )
              if ( arr.length > 0 )
              {
                 if ( arr.length == 2 )
                 {
                    switch ( arr[ 0 ] )
                    {
                       case "<" : 
                          if ( item[ c.field ] >= arr[ 1 ] )
                              return false;
                          break;
                       case ">" : 
                          if ( item[ c.field ] <= arr[ 1 ] )
                             return false;
                          break;
                       case "<=" :
                          if ( item[ c.field ] > arr[ 1 ] )
                             return false;
                          break;
                       case ">=" :   
                          if ( item[ c.field ] < arr[ 1 ] )
                             return false;
                          break;
                       default :
                          return false;                          
                    }
                 }   
                 else
                 {
                    if ( item[ c.field ] < arr[ 1 ] || item[ c.field ] > arr[ 3 ] ) 
                       return false;
                 }
              }   
              else
              {
                 if ( item[ c.field ] != colFilt[ columnId ] )
                    return false;
              }      
           }
           if ( typ == "C" ) // item[ c.field ].substring 
           {
              if ( item[ c.field ].toLowerCase().indexOf( colFilt[ columnId ] ) == -1 ) // item[ c.field ] != colFilt[ columnId ] && 
                 return false;
           }
        }   
     }
     return true;
  }

  function varType( o ) 
  {
     if ( o.toFixed )
        return "N";
     if ( o.substring )
        return "C";
     if ( o.getMonth )
        return "D";
     if ( o == true || o == false )
        return "L";
     return "U";
  }

  function date_num_filter( cVal )
  {
     var ret_arr = [];
     var p       = -1;
     var n1,n2,n3

     if ( cVal.length == 0 )
        return ret_arr;

     n1 = cVal.indexOf( ".." );
     n2 = cVal.indexOf( "<" );
     n3 = cVal.indexOf( ">" );
     if ( n1 >= 0 || n2 >= 0 || n3 >= 0 )
     {
        p = cVal.indexOf( ".." );
       if ( p >= 0 && cVal.length > 2 )
        {
           if ( p == 0 || p == cVal.length - 2 )
           {
              ret_arr[ 0 ] = ( p == 0 ? "<=" : ">=" );
              ret_arr[ 1 ] = ( p == 0 ? cVal.substr( 2 ) : cVal.substr( 0, p ) );
           }
           else
           {
              ret_arr[ 0 ] = ">=";
              ret_arr[ 1 ] = cVal.substr( 0, p );
              ret_arr[ 2 ] = "<=";
              ret_arr[ 3 ] = cVal.substr( p + 2 );
           }                  
           return ret_arr;
        }

        n1 = cVal.indexOf( "<=" );
        n2 = cVal.indexOf( ">=" );
        if ( n1 == 0 || n2 == 0 )
        {
           if ( cVal.length > 2 );
           {
              ret_arr[ 0 ] = cVal.substr( 0, 2 );
              ret_arr[ 1 ] = cVal.substr( 2 );
              return ret_arr;
           }
        }   
        n1 = cVal.indexOf( "<" );
        n2 = cVal.indexOf( ">" );
        if ( n1 == 0 || n2 == 0 ) 
        {
           if ( cVal.length > 1 );
           {
              ret_arr[ 0 ] = cVal.substr( 0, 1 );
              ret_arr[ 1 ] = cVal.substr( 1 );
              return ret_arr;
           }   
        }
     }   
     return ret_arr;
  }

提前致谢!

1 个答案:

答案 0 :(得分:5)

你的英语有点难以理解,但我相信你试图通过使用这样的条件(&gt; 100,!= 100,&lt;&gt; 100)进行过滤,我在我的项目下做了2个功能。原则是它将从检查第一个字符开始,如果找到这4个符号中的任何一个(&lt;,&gt;,!,=),那么我们知道它是一个条件过滤器,然后它将获取该条件直到空格为发现所以你会捕获任何带有1或2个字符的符号(&lt;,&lt; =,&lt;&gt;,!=等等)。此外,条件检查它是否是第一个数字,因为做&gt; 1对字符串和数字有2个不同的结果。

以下是我的两个功能:

function myFilter(item) {
    // Regex pattern to validate numbers
    var patRegex_no = /^[$]?[-+]?[0-9.,]*[$%]?$/; // a number negative/positive with decimals with/without $, %

    for (var columnId in columnFilters) {
        if (columnId !== undefined && columnFilters[columnId] !== "") {
            var c = grid.getColumns()[grid.getColumnIndex(columnId)];
            var filterVal = columnFilters[columnId].toLowerCase();
            var filterChar1 = filterVal.substring(0, 1); // grab the 1st Char of the filter field, so we could detect if it's a condition or not

            if(item[c.field] == null)
                return false;

            // First let see if the user supplied a condition (<, <=, >, >=, !=, <>, =, ==)
            // Substring on the 1st Char is enough to find out if it's a condition or not
            // if a condition is supplied, we might have to transform the values (row values & filter value) before comparing
            // for a String (we'll do a regular indexOf), for a number (parse to float then compare), for a date (create a Date Object then compare)
            if( filterChar1 == '<' || filterChar1 == '>' || filterChar1 == '!' || filterChar1 == '=') {
                // We found a Condition filter, find the white space index position of the condition substring (should be index 1 or 2)
                var idxFilterSpace = filterVal.indexOf(" ");

                if( idxFilterSpace > 0 ) {
                    // Split the condition & value of the full filter String
                    var condition = filterVal.substring(0, idxFilterSpace);
                    filterNoCondVal = columnFilters[columnId].substring(idxFilterSpace+1);

                    // Which type are the row values? We'll convert to proper format before applying the condition
                    // Then apply the condition comparison: String (we'll do a regular indexOf), number (parse to float then compare)
                    if( patRegex_no.test(item[c.field]) ) {                             
                        if( testCondition(condition, parseFloat(item[c.field]), parseFloat(filterNoCondVal)) == false ) 
                            return false;
                    // whatever is remain will be tested as a regular String format     
                    }else {                             
                        if ( testCondition(condition, item[c.field].toLowerCase(), filterNoCondVal.toLowerCase()) == false )
                            return false;
                    }
                } 
            }else{
                if (item[c.field].toLowerCase().indexOf(columnFilters[columnId].toLowerCase()) == -1)
                    return false;
            }
        }
    }
    return true;
}

/** Test a filter condition that is passed into String, since eval() function is a performance killer
 * I have created a switch case for all possible conditions. Performance is irrelevent this way 
 * @var String condition: condition to filter with
 * @var any value1: 1st value to compare, the type could be anything (number, String or even Date)
 * @var any value2: 2nd value to compare, the type could be anything (number, String or even Date)
 * @return boolean: a boolean result of the tested condition (true/false)
 */
function testCondition(condition, value1, value2){
    switch(condition) {
        case '<':   return (value1 < value2);
        case '<=':  return (value1 <= value2);
        case '>':   return (value1 > value2);
        case '>=':  return (value1 >= value2);
        case '!=':  
        case '<>':  return (value1 != value2);
        case '=':   
        case '==':  return (value1 == value2);
    }
    return resultCond;
}