Chain relationship in keystone.js - javascript

var leaves = new keystone.List('leaves');
leaves.add({
createdBy: {
type: Types.Relationship,
ref: 'user-datas',
initial: true,
label: 'Submitted By',
},
});
var userData = new keystone.List('user-datas');
userData.add({
user_id: {
type: Types.Relationship,
ref: 'Employees',
},
});
var Employees = new keystone.List('Employees');
Employees.add({
name: {
type: Types.Name,
required: true,
index: true,
initial: true,
},
});
I have 3 models/list: leaves,user-data, Employees.
So when in admin panel when I want to add a leave it shows Object id of record from user-data.
But is there any way to show name from Employees when entering a new leave. but the still refer to user-data?
I need to show user's name instead of the user ID as shown in the image below.

When you add the relationship for the leaves, you can do something like this:
submittedBy: {type: Types.Relationship, ref: 'User', many: false},
Hope this help. If not, please post more details like the model declaration.

You can achieve it by using relationship.

Related

tagify whitelist for objects with duplicate names

I asked this yesterday, but I didn't really get a solution, and after thinking more, I'm still unsure about what I can do to get by this issue.
<script>
var input = document.querySelector('input[name=tags-outside]');
let guildRoles = []
let guildIds = []
guild_roles = {{ guild_roles|tojson }};
guild_roles.map(e => {
guildRoles.push(e["name"])
guildIds.push(e["id"])
})
new Tagify(input, {
whitelist: guildRoles,
userInput: false
})
</script>
I was originally thinking of having two arrays, one for ID, one for names, but this isn't really a good solution.
I have a dropdown list using tagify, and the user needs to select a discord role from this dropdown. Each role has a unique identifier in the array of objects, it resembers something like this:
0: {color: 0, hoist: false, icon: null, id: "754852776550596638", managed: false, name: "role5"}
1: {color: 0, hoist: false, icon: null, id: "654852776550596638", managed: false, name: "role6"}
Each role can have the same name. So i need to be able to link up the ID to the role name, so that if the user selects a role, it won't use the wrong one since two roles can have the same name.
However, because whitelist only accepts an array, I do not know how I can link these IDs to their linked role names.
Any solutions would be very very helpful as I am clueless to how I can get around this...
An example of the dropdown can be found here:
https://gyazo.com/a7d691db9228cb1b61da0920f1561e57
You can configure this all according to the docs.
Since you have duplicate keys, it is important to use the ID as value and not the name. In order to still show the name as text for the tags and also to be able to search for them, You need to set tagTextProp, in the top level object. In the dropdown, you need to set mapValueTo and searchKeys.
Something along these lines:
const guildRoles = [{
color: 0,
hoist: false,
icon: null,
id: "754852776550596638",
managed: false,
name: "role5"
},
{
color: 0,
hoist: false,
icon: null,
id: "654852776550596638",
managed: false,
name: "role6"
},
{
color: 0,
hoist: false,
icon: null,
id: "343439489384938439",
managed: false,
name: "role6"
}
]
const input = document.querySelector('[name=roles]')
tagify = new Tagify(input, {
whitelist: guildRoles.map(({ id, name }) => ({ value: id, name })),
enforceWhitelist: true,
tagTextProp: 'name',
dropdown: {
enabled: 1,
position: 'name',
mapValueTo: 'name',
highlightFirst: true,
searchKeys: ['name'],
},
callbacks: {
add: ({ detail: { data }}) => console.log(data)
}
})
<script src="https://unpkg.com/#yaireo/tagify"></script>
<script src="https://unpkg.com/#yaireo/tagify/dist/tagify.polyfills.min.js"></script>
<link href="https://unpkg.com/#yaireo/tagify/dist/tagify.css" rel="stylesheet" type="text/css" />
<input name='roles' placeholder="Add Roles">

Why "duplicate key error" when inserting subdocument arrays?

I have a Mongoose Schema in which I use subdocuments. Their definitions are:
const vraagSchema = new mongoose.Schema({
vraagNummer: {
type: Number,
required: true,
min: 1
},
vraagTekst: {
type: String,
minLength: 1,
required: true
},
waarde: {
type: Number,
required: true,
min: 1
}
}, { collection: 'vragen' });
const checkSchema = new mongoose.Schema({
checkID: {
type: String,
required: true,
min: 2
},
sessieNummer: {
type: Number,
required: true,
min: 1
},
omschrijving: {
type: String,
required: true
},
vragen: {
type: [vraagSchema]
},
logData: {
type: String,
required: false,
default: ''
}
});
checkSchema.index({ sessieNummer: 1, checkID: 1 }, { unique: true })
Now, when I insert 1 Check item with an empty array for the "vragen" field ("vragen" is Dutch for "questions"), there is no problem.
But when I try to insert another Check item, with slightly different field values (so that it is unique), but also with an empty array "[]" as value for "vragen", I get an error: "MongoError: E11000 duplicate key error collection: demastermind_test.checks index: vragen.vraagNummer_1 dup key: { : null }".
Why is an empty array leading to a duplicate key error? And how can I prevent this?
I then checked what happened if I inserted Check items with non-empty arrays. So I inserted two checks with different field values (so they are unique), where 1 item has a "vragen" array with on "vraag" item in it, and 1 item has a "vragen" array with two "vraag" items in them (where I made sure that the two items had different "vraagNummer" waardes).
And that also leads to the exact same duplicate key error.
What am I missing?
I got this problem fixed. Apparently somewhere when I started working on this, I used an incorrect schema definition (or something), and that error got 'stuck' in de Collection.
I solved the problem by deleting the whole Collection (it currently is a test collection, so that wasn't a problem), and now it works as it should be.

Create scope by time range on sequelize

I have a model with an expiration date:
expireAt: {
type: DataTypes.DATE,
allowNull: false
}
I want to create a scope to find the "enabled" records, actually is:
// I add it on associate method, because need to use other model:
QuestionsModel.addScope("enableds", {
where: {
enabled: true // The question is enabled
},
include: [{
model: models.users,
where: {
enabled: true // The author is enabled
}
}]
});
But I don't know how to validate if is expired, I need to discard the expired records (when actual date is higher or equal with expireAt).
Some idea?

Angular using filter to Update?

I wanted to update my model using $filter or some other smart way without having to do multiple for each.
So basically I have my model similar to below:
$scope.projects = [
{
tasks: [
{
name: 'task name',
visible: true,
starred: true
}
],
createdAt: 'something'
},
{
tasks: [
{
name: 'second task name',
visible: true,
starred: false
}
],
createdAt: 'something'
}
]
What I wanted to do is by using $filter or some other way like underscore and so on, to update the content of the variable. So for instance, when I click a button, I'd like to set visible = true only to tasks that are starred.
Anyone have a suggestion on how to achieve that? Is it possible or I would have to do a couple of loops?
Something like:
$filter('filter')($scope.projects, {{starred = true}}).tasks.visible = true
UPDATE
With the help from #jbrown I was able to achieve what I wanted.
Just in case someone needs similar approach, the final solution was as written below:
_.forEach($scope.projectsModel.projects, function(proj){
_.forEach(_.filter(proj.tasks, {starred: true}), function(task){
task.visible = true;
});
});
Using underscore you can use a combination of filter and find to get the results you are looking for.
$scope.filteredProjects = _.filter($scope.projects, function(proj) {
return _.find(proj.tasks, {
visible: true, starred: true });
});

Select a row by the value of one cell in jqxGrid

I've got a little question about the use of jqxGrid from jqWidgets.
I'm trying to select a row of my table dynamically from another page. I explain myself:
My first page contains a list of users. I want that when I choose an user on this list, it opens a new page with, by exemple, the ID of my user returned in GET through PHP. And then, I want to generate a new grid, with less information, but with the user already selected.
I've found how to select a row by her index $('#grid').jqxGrid('selectrow', 10); but it's not working, because if one table is sorted or filtered, the index is changed...
So, is there any way to do this ?
Here's the code wich is called when selecting a row on first table:
$('#search_right').bind('rowselect', function(event){
var iSocID = $('#search_right').jqxGrid('getcellvalue', event.args.rowindex, 'id');
$("#soci_right").load('activites/soc.search.php?a=form&id='+iSocID);
$('#content').jqxTabs('select', 3);
});
And here's the generation code of my second list:
var url = 'activites/soc.search.php';
var source = {
datatype: "json",
datafields: [
{ name: 'name', type: 'string'},
{ name: 'id', type: 'int'},
],
id: 'id',
url: url,
root: 'data'
};
dataSource = new $.jqx.dataAdapter(source);
$("#soci_table").jqxGrid({
source: dataSource,
theme: jqxGlobalTheme,
columnsresize: true,
sortable: true,
filterable: true,
showfilterrow: true,
columns: [
{ text: 'Name', dataField: 'name'},
{ text: 'ID', dataField: 'id', hidden:true},
]
});
After contacting the support of jqWidgets (who couldn't help me......) I've made a little snippet that does the tricks... But I think jqWidget should add this as a default functionnality of jqxGrid !
I paste you my code here, hope it'll help some of you !
$('#search_right').bind('rowselect', function(event){
var iSocID = $('#search_right').jqxGrid('getcellvalue', event.args.rowindex, 'id');
// Create filter
var filtergroup = new $.jqx.filter();
var filtervalue = iSocID;
var filtercondition = 'EQUAL';
var filter1 = filtergroup.createfilter('stringfilter', filtervalue, filtercondition);
var filter_or_operator = 1;
filtergroup.addfilter(filter_or_operator, filter1);
$("#soci_table").jqxGrid('addfilter', 'id', filtergroup);
// Apply filter
$("#soci_table").jqxGrid('applyfilters');
// Select row
$('#soci_table').jqxGrid('selectrow', 0);
// Remove filter
$("#soci_table").jqxGrid('clearfilters');
});

Categories

Resources