i m trying here to dispaly my account user name in listing. i m using account-link to attach the user data but how can i display the username instead of _id
.
can anyone help me on above?
I see two options:
Create a custom renderer for the entries view, that calls /accounts/find
to get the related user data and display that instead of the id.
Use the event system to replace the ids before they are fetched and displayed.
event system
See Account-Link Display Option - #2 by raffaelj
By the way, this is the first result if you search for “account link” in this forum
renderer
The account-link field doesn’t have it’s own renderer, so the default one is used to display a string.
App.Utils.renderer.default = function (v) {
v = String(v === undefined ? '' : (['number', 'string'].indexOf(typeof (v)) > -1) ? v : JSON.stringify(v));
return v.length > 30 ? v.substr(0, 30) + '...' : v;
};
I wrote a mini tutorial about custom renderers here:
# Custom renderers
Sometimes the field content renderer in entries view doesn't show the content in the preferred way.
## Example for set field
To see the full functionality, use the next branch >= [this commit][1] (2018-11-07). Before this update, field definitions weren't passed to the renderer. For older versions, there is a fallback to display a simple items counter instead. Have a look at [the discussion on discourse][2] for some more background. Scroll down for screenshots.
If you want to write your own renderers, have a look at [app.utils.js][3] and discover the core renderers `App.Utils.renderer.{fieldname}`. You can overwrite everything, you want with a custom js file.
[1]: https://github.com/agentejo/cockpit/commit/907c1de5ba92f7bbab25635ad20d8b1d5d43a099
[2]: https://discourse.getcockpit.com/t/renderer-for-set-field-in-backend/372
[3]: https://github.com/agentejo/cockpit/blob/next/assets/app/js/app.utils.js
**bootstrap.php**
```php
if (COCKPIT_ADMIN && !COCKPIT_API_REQUEST) {
This file has been truncated. show original
The interesting bit for the id replacement can be found in the core source
var value = this.refs.txtfilter.value, options = {};
if (this.filter) {
options.filter = this.filter;
}
options.limt = 10;
this.loading = true;
App.request('/accounts/find', {options: options}).then(function(response) {
$this._accounts = response && Array.isArray(response.accounts) ? response.accounts : [];
$this.loading = false;
$this.update();
});
}
this.linkAccount = function(e) {
var account = e.item.account;
$this->app->storage->remove('cockpit/accounts', ['_id' => $data['_id']]);
return '{"success":true}';
}
}
return false;
}
public function find() {
\session_write_close();
$options = array_merge([
'sort' => ['user' => 1]
], $this->param('options', []));
if (isset($options['filter']) && is_string($options['filter'])) {
$filter = null;