SlickGrid列类型 [英] SlickGrid column type

查看:99
本文介绍了SlickGrid列类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为SlickGrid过滤器做自己的过滤功能,它通常会统一并可以99%使用。我在Slickgrid中错过了什么 - 列中使用了哪种类型的数据?也许它已经存在,但在审查了我没有找到的来源之后。如果存在 - 如果你引导我走向真正的道路,我将感激不尽。 Slick.Editors类型?但是,如果列不是用于编辑? ...

在SlickGrid的示例中,通常使用不允许数据类型的过滤器,只存在一些带有具体字段Id的示例。通常,数据是字符,日期,布尔值和数字。
对于nums类型,我想用<,>和其他数字操作数符号来改进过滤器,可以使用日期类型完成相同的操作。此时我只能使用字段ID来执行此操作 - 我可以使用fieldIds和类型指示自己的全局数组,然后从中识别列的类型。但是这个解决方案并不清楚 - 如果从网格中检测列类型会更好。

I want to do own filtering function for SlickGrid filters, which ussually will be unify and can used in 99 % . What I missed in Slickgrid - which type of data is used in column ? Maybe it alreday exist, but after reviewing sources I didn't found . If that exist - I will be thankfulll if you direct me to true path . Slick.Editors type ? But if column isn't for editing ? ...
In samples of SlickGrid ussually use filters not allowing to data type , exist only some examples with concrete field Id. Ussually data are chars, dates, booleans and nums. For nums types I want to improve filters with <,> and other numeric operands symbols , the same can be done and with dates types . At this time I can do that only with field ID - I can direct own global array , with fieldIds and types , and then recognize type of column from that . But this solution ins't clear - better it will be if detect column type from grid .

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

Many thanks in advance for any help and ideas !

增加:

找到一些搜索后,我可以使用SlickGrid数据值类型。我是Javascript的新手,所以欢迎任何有关改进以下内容的帮助和建议...: - )

After some search found , that I can work with SlickGrid data values types . I'm newbie with Javascript , so any help and suggestion to improve source below are welcome ... :-)

这是我的来源:

 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;
  }

提前致谢!

推荐答案

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

Your english is a little hard to understand but I believe you are trying to do filtering by using some conditions like this ( > 100, != 100, <> 100 ), well I made it under my project with 2 functions. The principle is that it will start by checking the first character and if found any of these 4 symbols ( <, >, !, =) then we know it's a conditional filter, then after it will grab that condition until a space is found so you would catch any symbols with 1 or 2 chars (<, <=, <>, !=, etc...). Also the condition checks if it's a number first, because doing > 1 on a string and on a number has 2 different results.

这是我的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;
}

这篇关于SlickGrid列类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆