Whats the best way to handle related data ids using Tabulator - tabulator

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",
}
}

Related

Update bottomCalc-value, based on selection (if any)

I just found Tabulator, and i love it. I'm currently using version 4.5. The bottomCalc-function isuseful, however, i would like it to be updated, based on the selection of row(s).
Is there a native option i've missed, or, should it be done manually, fx. triggered by the row-selections?
Column example:
{
title: 'Offline Amount',
field: 'offline',
bottomCalc: 'sum',
formatter: 'html',
align: 'center',
width: 150,
},
And selectField: true for columns.
Currenly, without updated bottomCalc:
And, i would like it to be updated, based on the selection (if there is any).
Update:
I guess the rowSelectionChanged-option, will be the best way to handle the update of .tabulator-footer .tabulator-calcs-holder, but i'm still unsure about, how to do that part.
If it's a question of looping through the selected values, calculate the sum, and update the value - or if i'm able to use another event, reuse a specific function, or yet another thing.
There is no built in way to achieve this but there may be a work around.
You could store a property in the row data that stores the selected state of the row. You could then use the rowSelected and rowDeselected callbacks to change this value.
You could then use a Custom Calculation Function to then generate the sum of the column values for the rows where the data object has a selected property of true.
Update
As of version 4.6, there is now a recalc function you can call on the table, so you could set up your calc function as outlined in the answer above and then use the rowSelectionChanged callback to trigger the update:
var table = new Tabulator("#example-table", {
rowSelectionChanged:function(data, rows){
table.recalc();
},
});

Tabulator. How to enable and disable editing from js

I'm using tabulator (fantastic thing), which has a built in, in line editing functionality.
The thing is though, it's either on or off by having the editor tag on a column basis.
You can disable the editor for a given cell as well.
What I'm trying to do is to have the table displayed as read only so to say.
Then click on a 'edit' button (which is in my table and I capture the click). That would in turn enable the inline, built-in editor functionality for that raw only.
Then I click a save button, write the updated data back to my DB and make the row read-only again.
So, with tabulator 4.2, I'd be looking for something like
var usrtable = new Tabulator("#usrtable", {
ajaxURL:"/account/cgi-bin/getallusers.php",
resizableColumns:false,
tooltips:true,
history:true,
pagination:"local",
paginationSize:10,
paginationSizeSelector:true,
reactiveData:true,
selectable:true,
initialSort:[{column:"username", dir:"asc"},],
columns:[
{ formatter: editIcon, width: 40, sortable: false, align: "center", cellClick: function (e, cell) {
var id = cell.getRow().getData().id;
row(id).editable=true;
/\/\/\/\/\/\/\/\/\/\/\
}
},
{title:"Id", field:"id", visible:false},
{title:"Username", field:"username", width:80, editor:"input"},
{title:"Password", field:"password", width:70, editor:"input"},
{title:"Role", field:"role", width:70, align:"center", formatter:"plaintext", editor:"select", editorParams:{values:["user","admin"]}},
{title:"Change passwd", field:"changepasswd", width:90, align:"center", formatter:"tickCross", sorter:"boolean", editor:true},
],
});
Is that somehow possible or do I have to re-invent the wheel? i.e. create a model to edit the data outside the table?
What I have tried.....
Once rendered, a cell looks like this:
<div class="tabulator-cell" role="gridcell" tabulator-field="username" tabindex="0" title="test" style="width: 80px; height: 29px;">test</div>
And when you click on the cell, it becomes editable and the class 'tabulator-editing' is added to the div.
So..... I though I could 'just' catch the click event and do something like this:
$(".tabulator-cell").on("click",function(){
($this).removeClass("tabulator-editing");
});
that didn't work so tried this:
$(".tabulator-cell").on("click",function(e){
e.preventDefault();
});
that didn't work either.
When doing this:
$(".tabulator-cell").on("click",function(){
alert("cell clicked");
});
it actually never fires... :-(
Either I'm doing something wrong or really do not know where to go from here....
This can be achieved with Tabulator's Optional Editing:
// column definition
{ title:"Name", field:"name", editor:"input", editable:editCheck }
var editCheck = function (cell) {
var isEditable = cell.getRow().getElement().classList.contains('isEditable');
return isEditable;
}
With this logic in place, you simply need to toggle isEditable class on the appropriate Tabulator Row.
In the documentation under Column Setup, there is a Data Manipulation option called editable.
http://tabulator.info/docs/4.2/columns#definition
editable - callback to check if the cell is editable (see Manipulating Data for more details)
I haven't experimented with it myself, but it appears as if this is only called when initially rendering the table, so you may have to force a re-render with table.redraw(true) when the user clicks the Edit and Save buttons.
You could also just use your cellClick event and call cell.edit().
So, I also got feedback from Oli who is the developer for Tabulator.
The short answer is that there is no option to enable editing at the row lever as I feared.
Furthermore, the option will not be added due to multiple factors.
In other words, what I'm trying to do is not available out of the box and the editing is to be done at the cell level.
There is no option for this in the tabulator. But if you want to disable a cell from editing, you can simply remove all the tabulator applied classes for the particular cell. For eg., if you want to make the last row edit disabled, you can use something like this,
$(".tabulator-row:last .tabulator-cell").removeAttr("role tabulator-field tabindex title");
I know this is a old question but my answer can be useful for visitors come here (like me) in future..
We have also a similar requirements, and we ended up by this solution. We have added "editor" in every column with another attribute "editable". In "editable", we are checking whether the row is selected or not, if row is selected then field can be editable otherwise not editable.
Other than that we have also used a "rowSelected" and "rowDeselected" to show/hide save button and on "rowSelected", we are checking that only 1 row is selected at a time.

Select Editor editorParams populate values from array in textfile?

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;
}}},

BoboBrowser & Lucene.NET: Bool facet

I am using BoboBrowse together with Lucene.Net. There are some predefined facethandlers for different scenarios. My question: Does anyone know how to implement a handler for a bool type/property of a lucene document? I mean, from a facets search point of view theres just a field/facet with 2 different values "true" and "false", so the result contains that values of course. But the result set seems then a bit strange: 300 objects with true, 400 with false. With a bool value of false, the result set should contain all objects, with set to true only that 300.
Thanks.
With Bobo-Browse.Net, there are 2 different aspects of setting up a facet.
Facets are defined at application startup and generally map to a specific field (or sometimes more than one field).
Selections are defined at runtime and determine which values are included in the search. Generally speaking, selections act like boolean switches over each unique value in a field.
So, in the case of a "boolean" field (which is actually just plain text), you just have 2 possible values. But keep in mind, there are actually 3 selection states for this "boolean" field:
Selecting "true"
Selecting "false"
Not selecting anything
It sounds like you just want to have your runtime code add/remove a single selection for "true", which will make it include all "true" values, or (if the selection is removed) include all values.
BrowseSelection sel = new BrowseSelection("booleanField");
if (value == true)
{
// Add the selection to filter the result
sel.AddValue("true");
}
else
{
// Don't add a selection, and the result will not be filtered.
}
browseRequest.AddSelection(sel);
Have a look at my Faceted Search Prototype for an example how this can be set up on the UI (although this wasn't really meant to be a demo and has lots of commented garbage code in it, it does demonstrate the concept). If you want to omit the search part of it, you just need to use a MatchAllDocsQuery instead of parsing the query using QueryParser.

Kentico Admin - Dynamically Change and SAVE Field Name, Value and Label

For a custom page type, I have a multiple choice form filed as seen in image 1 and 2. I was able to set it up (using jquery) so that when I click on other checkboxes belonging to a another field, the checkbox Label and value of this field is changed accordingly as seen in image 3 - 'Author1', 'Author2', 'Author3' were replaced with something else.
The problem is when I hit the Save button, all of my new label/value are not Saved, but the checkbox value and label return to its original value.
Is there a way to make it so that these checkboxes can accept new value and the new value can be saved. Thanks!
Where does authors values come from? Database? You should be able to use SQL query as data source for multiple choice control. This will allow you to store actual values into database whenever you save a page on the Form tab.
So the idea is to load actual values, replace them with 'AuthorN' and change back to original value when checked.
I've set up an example which might help you. I've created a form with three fields:
FirstAuthor
Type: Integer
Form control: Radio buttons
Data source: SQL Query (select userid,username from cms_user)
Has depending fields: true
SecondAuthor
Type: Integer
Form control: Radio buttons
Data source: SQL Query (select userid,username from cms_user)
Has depending fields: true
OrderedAuthors
Type: Text (1000)
Form control: Label
Default value: {% (if(firstauthor>0) { GlobalObjects.Users.Where("userid="+firstauthor)[0].UserName } else {""}) + ", " + (if(secondauthor>0) {GlobalObjects.Users.Where("userid="+secondauthor)[0].UserName} else {""} #%}
Depends on another field: true
I guess you have a list of authors somewhere in the database so you can just replace CMS_User table with your table. Then all you need to do is to adjust the macro in the third field to give you results you want.

Resources