Is it possible to iterate through an array to generate the values that populate a select box, rather than hard-coding them in the template?
I'm passing an array of values to the template that I would like available in the select box, but I'm not sure how to go about iterating through it.
Here's how I currently have it:
{{ form_select('profile_status', {
'created' : 'Candidate Created',
'mailed': 'Questionnaire Mailed',
'personal_info': 'Personal Data Filled Out',
'survey': 'Survey Completed',
'primary': 'Waiting for Post-Primary Updates',
'endorsements': 'Endorsements Completed',
'essay': 'Essay Completed',
}, candidate.profile_status, {'class': 'form-control', 'id': 'profile-status'})|raw }}
What I'm not sure how to do is implement a for-loop within an already existing twig tag to replace the hard-coded list of values.
Can someone point me in the right direction?
Thanks!
Related
this one should be simple for tabulator experts (but not for me)...
i have a tabulator table. I've made one column, "edit" clickable as shown below. it works fine and returns the value of the cell i clicked. HOWEVER, that's not what i need. I need to get the value of ANOTHER cell in the same row (column "transactionID"). I know how you would do it in other languages, just use x and y values to move over 3 columns and get the value. but how is it done in tabulator? by the cloumn name? I can't find any example code on how to accomplish this.
This is the snippet from my tabulator init :
{title:"edit" , name:"edit", formatter:myformatter, cellClick:function(e, cell){alert("cell clicked - " + cell.getValue())}},
i just need to make it return value for "transactionID" instead of "edit"
and before anyone asks, no, i can't just make "transactionID" clickable. I need the clickable cell to be separate.
thanks for your help!
ok so since i got no answers to this question, i slogged thru and figured it out myself eventually. To save others time who run into this in the future, i am posting how i solved it. code with comments below :
// this goes in your tabulator column definitions. In my example, "edit" is a column
with an image button that on clicking, redirects to another page depending on what cell was clicked
{title:"" , name:"edit", formatter:openIcon, headerSort:false, cellClick:function(e, cell){
var re = cell.getRow(cell); // get the row of the cell that was clicked
var thenum = re.getData().id; // gets the value of column "id" in the same row
var theurl = "editTransaction.php?trans="+thenum; // creates my url
console.log(theurl); // perform whatever action you need to do. replace this.
}},
If you are using react-tabulator and you want to access some column data to perform a specific action ,here is the snippet for your help.
import { reactFormatter } from "react-tabulator";
// your React Component to use in a cell
function CustomFormatter(props: any) {
const rowData = props.cell._cell.row.data;
return <a onClick={() => alert(rowData.customField)}{cellData.customField }</a>;
}
const columns = [{ title: 'Custom', field: 'custom', align: 'center', formatter: reactFormatter(<CustomFormatter />) },
]
Using Tabulator, if you have a 'select' editor and want it to link to values 'Male' and 'Female' but underneath male and female are values from another physical database table with id values of 1 and 2 (which are different than the 'row' id), whats the best way to do something like this?
The 'select' editor has ways to specify the display of items in the drop down and a literal display of the selected value, but no place for a hidden (not displayed) underlying ID of the selected value to pass when saving the data.
We could wrap the select data values in divs with a data attribute for the selects values ids and then pass that when updating, but are not sure this is the best option considering how Tabulator works. We could also just pass the raw selected value and then look it up on the server to get the associated ID, but that seems like a lot of overhead and tightly couples the server to the client, which wouldn't work for something like a 3rd party API where we have no server control.
Any thoughts on how best to handle something like this are appreciated!
The select editor allows for values to be passed in a number of ways, including specifying both the items value and its user visible label
If you want to show a different lable to the value you want to store, you can pass in an object, where the key of each property is the value that will be stored if it is selected, and the value of each property will be the lable displayed for it in the list.
{title:"Name", field:"name", editor:"select", editorParams:{
values:{
"steve":"Steve Boberson",
"bob":"Bob Jimmerson",
"jim":"Jim Stevenson",
}
}}
For more complex option lists you can use an array of objects, that allows you to define option groups, and disabled options.
{title:"Name", field:"name", editor:"select", editorParams:{
values:[
{ //option group
label:"Men",
options:[ //options in option group
{
label:"Steve Boberson",
value:"steve",
},
{
label:"Bob Jimmerson",
value:"bob",
},
]
},
{ //option group
label:"Women",
options:[ //options in option group
{
label:"Jenny Jillerson",
value:"jenny",
},
{
label:"Jill Betterson",
value:"jill",
},
]
},
{//ungrouped option
label:"Other",
value:"other",
},
]
}}
For full details on how to use this editor, checkout the Editor Documentation
Finally figured this out. You need to use the lookup formatter to display the text value using the same params as the select editor. This is not obvious since none of the select editor examples in the docs show use of it. Anyway, here is a simple example showing it in both directions. Of course you would want to abstract the data out instead of literally duplicating it, but here to help show the literal use, it is duplicated:
{
title:"Example",
field:"example",
editor: "select",
editorParams:{
"1": "Cute",
"2": "Fine",
"3": "Scary",
},
formatter:"lookup", // display option, but store value
formatterParams:{
"1": "Cute",
"2": "Fine",
"3": "Scary",
}
}
I pass a list into a message using the django message framework. When it is rendered in the template, I try to access the list using {{ item.0 }} for example, but nothing is coming through. If I just use {{ item }}, I can see my list.
In short, I suspect the list isn't actually a list, it is a string that looks like a list (such as "['field1','field2']").
I arrive at this conclusion because if I try to access any list item > 0, the template renders empty, which implies there is no list item beyond position 0, which is consistent with the fact that item is a string.
Messages are populated in the View as:
for item in errorRecords:
messages.add_message(request, messages.WARNING, item)
Here, item is a list (e.g. ['field1','field2','field3']....)
Attempting to access the message list in the template:
{{ message.0 }}
works for position 0, but not for position 1 (renders blank). When access position 0, the entire list contents is shown . i.e. renders as:
['field1','field2','field3']
I want to be able to access each list's elements, so that I can populate a table with each field.
I am using Tablulator v4.1. I am creating an interface for our pharmacy organization so a member can change the meeting information without doing any coding. One field is the meeting room location which is a select editor.
Is it possible to populate the values of editorParams with a path to an array stored in a different text file? Something like:
{title:"Room", field:"room", editor:"select", editorParams:{"/textfiles/roomvalues.txt"}},
I can then create another table to add/edit the possible room choices.
This code is what I currently use.
{title:"Room", field:"room", editor:"select", editorParams:{
values:{
"A":"Room A",
"B":"Room B",
"C":"Room C"}
}},
Thank you.
You can pass a function into the values property that can then return an array of values that you can load from elsewhere
{title:"Room", field:"room", editor:"select", editorParams:{
values:function(cell){
//lookup values from data source
return values;
}}},
First I create a selection and add a few things to it. I've stripped it down for readability:
arcData = [
{data: [{label: "first"}], otherProp: value},
{data: [{label: "second"}], otherProp: value}];
arcSelection = svg.selectAll("arc").data(arcData);
arcSelection.enter().append("g").append("path").attr("d", myArcDefinition);
I try to add a nested selection using data from the parent:
arcDataSelection = arcSelection.selectAll("text").data(function(singleArc, arcIndex) {
return singleArc;
});
arcDataSelection.enter().append("text").text(function(d) {
return d.data.label;
});
But no text objects are created in the DOM. How can I properly create an element in a nested selection that uses data from a parent?
I'm trying to follow the pattern(s) shown here: http://bost.ocks.org/mike/nest/
To use nested selections, you need to have nested data. You're passing in a one-dimensional array (i.e. not nested). Have another look at the example you've linked to -- the data used there is a two-dimensional array.