Meteor-Community-Packages / meteor-tabular

Reactive datatables for large or small datasets
https://packosphere.com/aldeed/tabular
MIT License
363 stars 136 forks source link

Tabular with PublishComposite #218

Open fipke opened 9 years ago

fipke commented 9 years ago

Hello, sorry if my question is too easy, I'm starting with Meteor

I have the following configuration, but the columns with title "Ingrediente", "Updated By" are empty in the DataDatable

/app/lib/controllers/preco_ingredientes_controller.js

TabularTables = {};

Meteor.isClient && Template.registerHelper('TabularTables', TabularTables);

PrecoIngredientes.helpers({
  ingrediente: function () {
    console.log("PrecoIngredientes.helpers this.ingredienteId:" + JSON.stringify(this));
    return Ingredientes.find(this.ingredienteId).name;
    // return Ingrediente;
  },
  updated: function () {
    var user = Meteor.users.findOne({_id: this.updatedById});
    console.log("user:" + JSON.stringify(user));
    return user && user.name;
    // return Ingrediente;
  },

});

TabularTables.PrecoIngredientes = new Tabular.Table({
  name: "PrecoIngredientes",
  collection: PrecoIngredientes,
  pub: "precoIngredientes_Composite",
  columns: [
    {data: "quantidade", title: "Quantidade"},
    {data: "preco", title: "Preco"},
    {data: "ingrediente()", title: "Ingrediente"},
    {data: "updated()", title: "Updated By"},

  ]
});

/app/server/publish.js

Meteor.publishComposite('precoIngredientes_Composite',function (tableName, ids, fields) {
    check(tableName, String);
    check(ids, Array);
    check(fields, Match.Optional(Object));

    this.unblock(); // requires meteorhacks:unblock package
    return {
    find: function() {
       this.unblock(); // requires meteorhacks:unblock package
       console.log("Publish:" + JSON.stringify(PrecoIngredientes.findOne()));
        return PrecoIngredientes.find({});
    },  
    children: [ 
        {
            find: function(precoIngrediente) {
              this.unblock(); 
                return Meteor.users.find(
                    { _id: precoIngrediente.updatedById },
                    { limit: 1});
            }
        },
        {
            find: function(precoIngrediente) {

              // console.log("child2:");
              this.unblock(); 
                return Ingredientes.find({ _id: precoIngrediente.ingredienteId });
            }
        }
    ]
  }
});

/app/client/templates/preco_ingredientes/preco_list/preco_ingredientes_list_new.html

....
 {{> tabular table=TabularTables.PrecoIngredientes class="table table-striped table-bordered table-condensed"}}
....

/app/lib/collections/preco_ingredientes.js

PrecoIngredientes = new Mongo.Collection('precoIngredientes');

var schemasPrecoIngrediente = new SimpleSchema({
    ...,
    ingredienteId: {
    label: "Ingrediente",
    type:  String,
    optional: true,

    autoValue: function() {
      if ( this.isSet ){
        return this.value;
      } else {
        return '';
      }
    },
    autoform: {
      afFieldInput:{
        placeholder: 'Select One'
      },
      label: "Ingrediente",
      selectOnBlur: true,
      type: "select2",
      options: function() {
       var list = [];
       list.push({label: "", value: ""});
        var ingredientesList = Ingredientes.find();
        ingredientesList.map(function(ingrediente) {
          list.push({
            label: ingrediente.name,
            value: ingrediente._id
          });
        });
        return list;
      }
    }
  },
    updatedById: {
        type: String,
        label: "Updated by",
        autoValue: function() {
            if (!this.value)
                return this.userId;
        },
        optional: true
    },
...
});

Chrome Console

PrecoIngredientes.helpers this.ingredienteId:{"preco":1111.22,"quantidade":123,"_id":"g2NMe7DwCxPQWivD9"} preco_ingredientes_controller.js:53 
user:undefined

Server log from publishComposite:

I20151012-11:18:31.611(-3)? Publish:{"_id":"FWuxM5wKE7969kkMk","ingredienteId":"kwqRCm8kaNCofmPqN","quantidade":123,"preco":79.78,"createdAt":"2015-10-01T20:24:38.304Z","updatedAt":"2015-10-01T20:24:38.304Z","updatedById":"XBGiQDNdW25JdMHew"}

What am I doing wrong?

Thank you

manusharma1 commented 9 years ago

Hi Djeimi,

In Columns please try to add ID using which you are trying to fetch the data, even if you dont want to show it, You can later hide this using columnDefs.

I cannot fully understand your code, But let me give you one example very quickly. Here I was not using the ParentID but If I don't use it I cannot able to perform my queries using the publishComposite

      columns: [
        {data: "name", title: "Title"},
        {data: "parentID", title: "parentID"},
        {data: "parentcategory()", title: "Parent Category"}
      ],

    "columnDefs": [
                {
                    "targets": [ 1 ],
                    "visible": false,
                    "searchable": false
                },
                {
                    "targets": [ 2 ],
                    "visible": true,
                    "searchable": true,
                    "sortable": true
                }
            ]

    });

In publishComposite I have to use it like this:

children: [
  {
    find: function(placementcategories) {
    return Categories.find({ _id: categories.parentID });
    }
  }

So my suggestion is to try it and see if it work.

Thanks for asking if I could have given some direction that would be great.

Manu

fipke commented 9 years ago

It works. Thank you @manusharma1 . But unfortunately isn't possible to find or sort by the "updatedAt" or "ingrediente" fields. Thank you

manusharma1 commented 9 years ago

Yes, This is same thing is also happening with me. I am unable to sort or search, Can you please check if others are also facing this or they have any solution for this, otherwise you can raise this issue and I will also support you as I am also looking for the same solution.

Please do update me if you get any answer to this issue or will raise new issue.

Thanks, Manu

aldeed commented 8 years ago

In the ingrediente helper, it should be findOne instead of find. I don't see any other problems.

fipke commented 8 years ago

Hello @aldeed I tried but I still have to do like @manusharma1 commented. I have to define a column "IngredienteId" an later hide the column with the ID. If I don't specify the "ingredienteId" I do not receive a entire PrecoIngrediente Json. Is correct specify the column with ID and then hide it later? Should it work automatically with definition:

TabularTables.PrecoIngredientes = new Tabular.Table({
  name: "PrecoIngredientes",
  collection: PrecoIngredientes,
  pub: "precoIngredientes_Composite",
  columns: [
    {data: "quantidade", title: "Quantidade"},
    {data: "preco", title: "Preco"},
    {data: "ingrediente()", title: "Ingrediente"},
    {data: "updated()", title: "Updated By"},
  ]
});
BetoFrega commented 8 years ago

Hey @fipke , here is what I think is happening: When your TabularTable subscribes to your custom publication it asks for the fields present in the columns property via the fields parameter sent to you publication. You can however include an additional property called extraFields and extend the fields param with that value

The way @aldeed's original code handles that is here: https://github.com/aldeed/meteor-tabular/blob/02ff9a8fd22fd43d0a094e58c89d6b869fde2bfa/server/tabular.js#L27

And copied here for convenience:

  var table = Tabular.tablesByName[tableName];
  if (!table) {
    // We throw an error in the other pub, so no need to throw one here
    self.ready();
    return;
  }

  // Extend fields list with extra fields from the table definition
  if (table.extraFields) {
    _.extend(fields, table.extraFields);
  }