0

i need filter a Backgrid by a model sub field. I have the next JSON string from the server:

[{"iduser":
    {"iduser":1224,"apellido":"Agostini","nombre":"Juan Ignacio","dni":47121281}
  },
  {"iduser":
     {"iduser":1225,"apellido":"Alvarez","nombre":"Pedro","dni":4712312}
  }]

So, i show the user fullname with the next custom StringCell render:

{name: 'fullname', label: 'Nombre completo', cell: Backgrid.StringCell.extend({
        render: function(){
            var user = this.model.get('iduser');
            var fullname = user.apellido + ", " + user.nombre;
            this.$el.html(fullname);
            return this;
        }
}), editable: false, sortable: true}

Now, i try set the filter by "fullname", but not works. Any ideas ?

ramiromd
  • 2,019
  • 8
  • 33
  • 62

1 Answers1

-1

Because by default each Backgrid column name corresponds to an actual Model attribute name, and sorting will sort on the values of that attribute in every model. In your case, you don't have an Model attribute called "fullname", but you can approximate that by defining a sortValue column attribute.

var columns = [{
  name: "fullname",
  label: "Nombre completo",
  cell: MyStringCell,
  editable: false,
  sortable: true,
  sortValue: function (model, colName) {
    var user = model.get("iduser");
    return user.apellido + ', ' + user.nombre;
  }
}]
Y.H Wong
  • 7,151
  • 3
  • 33
  • 35