Combine fetching Remote Data with creating new items #141
Answered
by
oyejorge
mrwunderbar666
asked this question in
Q&A
-
I am exploring this cool library and I got stuck when I tried to enhance the GitHub remote data example. When I allow the user to create a new item with Any help would be highly appreciated! This works: new TomSelect('#select-repo',{
create: true,
valueField: 'url',
labelField: 'name',
searchField: 'name',
// fetch remote data
load: function(query, callback) {
var url = 'https://api.github.com/search/repositories?q=' + encodeURIComponent(query);
fetch(url)
.then(response => response.json())
.then(json => {
callback(json.items);
}).catch(()=>{
callback();
});
}
}); This doesn't: new TomSelect('#select-repo',{
create: function(input, callback) {
var returnData = {
value:input,
text:input,
name:input,
description:'none',
owner: {
login:'none',
avatar_url: 'none'
}
};
callback(returnData);
},
valueField: 'url',
labelField: 'name',
searchField: 'name',
// fetch remote data
load: function(query, callback) {
var url = 'https://api.github.com/search/repositories?q=' + encodeURIComponent(query);
fetch(url)
.then(response => response.json())
.then(json => {
callback(json.items);
}).catch(()=>{
callback();
});
},
// custom rendering functions for options and items
render: {
option: function(item, escape) {
return `<div class="py-2 d-flex">
<div class="icon me-3">
<img class="img-fluid" src="${item.owner.avatar_url}" />
</div>
<div>
<div class="mb-1">
<span class="h4">
${ escape(item.name) }
</span>
<span class="text-muted">by ${ escape(item.owner.login) }</span>
</div>
<div class="description">${ escape(item.description) }</div>
</div>
</div>`;
},
item: function(item, escape) {
return `<div class="py-2 d-flex">
<div class="icon me-3">
<img class="img-fluid" src="${item.owner.avatar_url}" />
</div>
<div>
<div class="mb-1">
<span class="h4">
${ escape(item.name) }
</span>
<span class="text-muted">by ${ escape(item.owner.login) }</span>
</div>
<div class="description">${ escape(item.description) }</div>
</div>
</div>`;
}
},
}); |
Beta Was this translation helpful? Give feedback.
Answered by
oyejorge
Jul 14, 2021
Replies: 1 comment 1 reply
-
The problem is the returnData in your create: function(input, callback) {
var returnData = {
value:input,
text:input,
name:input,
description:'none',
owner: {
login:'none',
avatar_url: 'none'
},
url: 'a-url',
};
callback(returnData);
}, |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
mrwunderbar666
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem is the returnData in your
create
method. The object you're trying to create doesn't have a parameter matching yourvalueField
which isurl
. This create method will work