SlickGrid column type

孤人 提交于 2019-12-08 09:52:38

问题


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 !

ADDED :

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 ... :-)

Here is mine source :

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

Thanks in advance !


回答1:


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.

Here are my 2 functions:

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


来源:https://stackoverflow.com/questions/16752218/slickgrid-column-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!