We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15001
    • 697 Posts
    @Bob. Thanks; you’re right. But still not working in my case.
    Maybe my records should be "checked"/"unchecked" or "true"/"false" instead of zero and ones...
      • 3749
      • 24,544 Posts
      LOL! I looked at the datagrid code (in 0.9.6 - manager/includes/controls/datagrid.class.php) and I don’t see any evidence that there is either a Checkbox or Text field type. Here are the choices:

      function formatColumnValue($row,$value,$type,&$align){
      		if(strpos($type,":")!==false) list($type,$type_format) = explode(":",$type,2);
      		switch (strtolower($type)) {
      			case "integer":
      				if($align=="") $align="right";
      				$value = number_format($value);
      				break;
      
      			case "float":
      				if($align=="") $align="right";
      				if(!$type_format) $type_format = 2;
      				$value = number_format($value,$type_format);
      				break;
      
      			case "currency":
      				if($align=="") $align="right";
      				if(!$type_format) $type_format = 2;
      				$value = "$".number_format($value,$type_format);
      				break;
      				
      			case "date":
      				if($align=="") $align="right";			
      				if(!is_numeric($value)) $value = strtotime($value);
      				if(!$type_format) $type_format = "%A %d, %B %Y";
      				$value = strftime($type_format,$value);
      				break;
      
      			case "template":
      				// replace [+value+] first
      				$value = str_replace("[+value+]",$value,$type_format); 
      				// replace other [+fields+]
      				if(strpos($value,"[+")!==false) foreach($row as $k=>$v){
      					$value = str_replace("[+$k+]",$v,$value);
      				}
      				break;
      				
      		}


      I would be inclined to try putting in another case in that function:

      case: ’MyBoolean’:

      if ($value == ’0’ || $value == 0) {
      $value = ’ ’;
      }
      break;
      }


      If you don’t want to hack the code, it looks like the template type is your best hope. I did find this example for a datagrid with only one column:

      colTypes = "template:<input type='radio' name='id' value='[+id+]' />";


      And here’s a more complex example with a checkbox:

      colTypes="template:<input name='tag[]' type='checkbox' value='[+id+]'/><img src='media/images/icons/comment.gif' width='16' height='16' align='absmiddle' /></a>||".
      					   "template:<a href='#' title='".$_lang["click_to_edit_title"]."' onclick='editTag([+id+])'>[+value+]</a><span style='display:none;'><script type=\"text/javascript\"> tagRows['[+id+]']=[\"[+name+]\",\"[+tag+]\",\"[+tagvalue+]\",\"[+http_equiv+]\"];</script>";


      IIRC, the datagrid is going away in MODx Revolution so you might want to take another approach. I think I’d just have a custom snippet to read the CSV file with something like the following:

      $file_array = file($file_name,  FILE_SKIP_EMPTY_LINES);
      
      foreach ($file_array as $line_number => $line)
      {
         $line = rtrim($line); // strip EOL characters
         $field_array = explode(',',$line');
      
          .$output .= '<span class = "field1">' . $field_array[0] . '</span>';
      
          $field_array[1] = $field_array[1] == '0' ?  ' ' : $field_array[1];
          .$output .= '<span class = "field2">' . $field_array[1] . '</span>';
      
          $field_array[2] = $field_array[2] == '0' ?  ' ' : $field_array[2];
          .$output .= '<span class = "field3">' . $field_array[2] . '</span>';
      
          $field_array[3] = $field_array[3] == '0' ?  ' ' : $field_array[3];
          .$output .= '<span class = "field4">' . $field_array[3] . '</span>';
      
          .$output .= '<br />';
         
      }
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 15001
        • 697 Posts
        Thanks a lot Bob. I looked at the code and tested the "template:" column type.
        Very interesting for me to discover this feature.

        In my case, the best solution however was hacking the code of \manager\includes\controls\datagrid.class.php
        as you suggested.

        I added this above the case "template" (but this could of course be written at some other place) :
        			case "boolean":
        				$align="center";
        				$value = number_format($value);
        				if ($value)
        					$value = '•';
        				else
        					$value = ' ';
        				break;

        Then, the string in the Column Type field of my DataGrid is : text,boolean,boolean,boolean.

        Very nice.

        It’s basic but useful improvement. I suggested on JIRA adding above lines to DataGrid.
        I’ll suggest adding it to the 0.9.6. branch of MODx.
          • 3749
          • 24,544 Posts
          Glad it worked for you. Remember that when you upgrade MODx it will be overwritten unless you temporarily rename the file.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting