简体   繁体   English

Dojo dgrid - 根据网格中的另一个字段格式化一个字段

[英]Dojo dgrid - formatting a field based on another field in grid

I am working with a dgrid and I am trying to use a formatter on a column but I need that formatter to return values based on the values of another column.我正在使用 dgrid,我正在尝试在列上使用格式化程序,但我需要该格式化程序根据另一列的值返回值。 The values for the density column are different based on whether or not it is a plant or animal.密度列的值根据它是植物还是动物而不同。 I also need this to be done automatically versus on a row/cell click我还需要自动完成此操作而不是单击行/单元格

Below is the section of code that handles the dgrid下面是处理 dgrid 的代码部分

/* Dojo  dgrid */
    var resultsGrid = declare([Grid, Selection, ColumnResizer]);
    var columns = [{
       label: "Object ID", 
       field: "OBJECTID",
       sortable: false,
       },{
        label: "Observer Name",
        field: "OBSERVER"
      }, {
        label: "Observation Date",
        field: "DAY",
        formatter: formatTimestamp
      }, {
        label: "Latitude",
        field: "LATITUDE"
      }, {
        label: "Longitude",
        field: "LONGITUDE"
      }, {
        label: "Common Name",
        field: "Q_NAME"
      }, {
        label: "Genus",
        field: "GENUS"
      }, {
        label: "Species",
        field: "SPECIES"
      }, {
        label: "Kingdom",
        field: "KINGDOM",
        formatter: numberToTextKingdom
      },{
        label: "Area",
        field: "OB_AREA",
        formatter: numberToTextArea
      }, {
        label: "Density",
        field: "OB_DENSITY",
        formatter: getDensity
      }, {
        label: "County",
        field: "COUNTY"
      }, {
        label: "State",
        field: "STATE"
      }, {
        label: "Country",
        field: "COUNTRY"
      }, {
        label: "Date Added",
        field: "ADD_DATE",
        formatter: formatTimestamp
      }];

    grid = new resultsGrid({
      bufferRows: Infinity,
      columns: columns,
      selectionMode: 'single'
    }, "grid");

    /* Formatters for Columns */

    function formatTimestamp(value) {
     var inputDate = new Date(value);
     return dojo.date.locale.format(inputDate, {
       selector: 'date',
       datePattern: 'MM/dd/yyyy'
     });
    }

    function numberToTextArea(value) {
      var outText = "";
      if (value == 0) outText = "NA";
      if (value == 1) outText = "Individual/few/several";
      if (value == 2) outText = "< 1,000 square feet (half tennis court)";
      if (value == 3) outText = "1,000 square feet to 0.5 acre";
      if (value == 4) outText = "0.5 acre to 1 acre (football field w/o end zones)";
      if (value == 5) outText = "> 1 acre";
      return outText;
    }

    function numberToTextDensityPlants(value) {
      var outText = "";
      if (value == 0) outText = "NA";
      if (value == 1) outText = "Sparse (scattered individual stems or very small stands)";
      if (value == 2) outText = "Patchy (a mix of sparse and dense areas)";
      if (value == 3) outText = "Dense (greater than 40% of the area)";
      if (value == 4) outText = "Monoculture (nearly 100% of area)";
      return outText;
    }

    function numberToTextDensityAnimal(value) {
      var outText = "";
      if (value == 0) outText = "NA";
      if (value == 1) outText = "Few (<5)";
      if (value == 2) outText = "Many (5+)";
      return outText;
    }

I have been working with this function to try and get something to work but I have had no luck.我一直在使用这个 function 来尝试让一些东西工作,但我没有运气。

var sKingdom = "";
    grid.on("dgrid-select", function (evt) {
        let cell = grid.cell(evt);
        sKingdom = cell.row.data.Kingdom;
    });

    dPlantDensities = {0:"NA", 1:"Sparse (scattered individual stems or very small stands)", 2:"Patchy (a mix of sparse and dense areas)", 3:"Dense (greater than 40% of the area)", 4:"Monoculture (nearly 100% of area)"};
    dAnimalDensities = {0:"NA", 1:"Few (<5)", 2:"Many (5+)"};

    function getDensity(event) {
        var outText = "";
        if (KINGDOM == 0) {
            if (value == 0) outText = dPlantDensities[0];
            else if (value == 1) outText = dPlantDensities[1];
            else if (value == 2) outText = dPlantDensities[2];
            else if (value == 3) outText = dPlantDensities[3];
            else if (value == 4) outText = dPlantDensities[4];
        } else {
            if (value == 0) outText = dAnimalDensities[0];
            else if (value == 1) outText = dAnimalDensities[1];
            else if (value == 2) outText = dAnimalDensities[2];
        }
        return outText;
    }

This is an example of how the dgrid is populated这是如何填充 dgrid 的示例

          /* Populate DGrid Table at bottom of page with query results*/
      var items = prjResults
      var TableFeatures = []
      array.forEach(items, function (feature) {
       var TableAttributes = {}
       var TableFields = Object.keys(feature.attributes)
       for (var i = 0; i < TableFields.length; i++) {
           TableAttributes[TableFields[i]] = feature.attributes[TableFields[i]]
       }
       TableFeatures.push(TableAttributes)
      })

      var memStore = new Memory({
        data: TableFeatures,
        idProperty: "OBJECTID"
      });

      grid.set("collection", memStore);
      grid.set("sort", "OBJECTID", true) // sorts objectID column - shows most recent 1st
      grid.on("dgrid-select", selectedOBS);
      grid.on('dgrid-deselect', clearonChange);

You can use the renderCell function to render a cell based on other values in the row.您可以使用 renderCell function 根据行中的其他值呈现单元格。 You could specify the density column like this:您可以像这样指定密度列:

{
        label: "Density",
        field: "OB_DENSITY",
        renderCell: function(object, value, node) {

                    }
}

The object parameter contains the row from the store, so you can use it to check the species or the kingdom. object 参数包含商店中的行,因此您可以使用它来检查物种或王国。 You do not specify the version of dgrid you are using.您没有指定正在使用的 dgrid 版本。 In the latest version, the formatter function also includes the object parameter.在最新版本中,格式化程序 function 还包含 object 参数。 Check the documentation to use the correct parameters for the formatter or renderCell function:检查文档以对格式化程序或 renderCell function 使用正确的参数:

dgrid 1.2.1 分布式网格 1.2.1

dgrid 0.3 网格 0.3

Using the get method on the dgrid column, you are able to successfully return certain values based on the values of another column在 dgrid 列上使用 get 方法,您可以根据另一列的值成功返回某些值

{
        label: "Density",
        field: "OB_DENSITY",
        get: function(obj){
          if (obj.KINGDOM == 0) {if (obj.OB_DENSITY == 0) {return "NA"};}
          if (obj.KINGDOM == 0) {if (obj.OB_DENSITY == 1) {return "Sparse (scattered individual stems or very small stands)"};}
          if (obj.KINGDOM == 0) {if (obj.OB_DENSITY == 2) {return "Patchy (a mix of sparse and dense areas)"};}
          if (obj.KINGDOM == 0) {if (obj.OB_DENSITY == 3) {return "Dense (greater than 40% of the area)"};}
          if (obj.KINGDOM == 0) {if (obj.OB_DENSITY == 4) {return "Monoculture (nearly 100% of area)"};}
          if (obj.KINGDOM == 1) {if (obj.OB_DENSITY == 0) {return "NA"};}
          if (obj.KINGDOM == 1) {if (obj.OB_DENSITY == 1) {return "Few (<5)"};}
          if (obj.KINGDOM == 1) {if (obj.OB_DENSITY == 2) {return "Many (5+)"};}
        }
      },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM