winJS ListView dynamic datasource edit issue - microsoft-metro

winJS ListView issue
I am working on a winJS application.I have listview.I need to add new data to an existing list view.How can i do that?

Normally the listview is bound with a WinJS.Binding.List, just add new items to that list and they'll appear inside listview.

Suggest use WinJS.Binding.List.createSorted() first and createGrouped on the sorted list. if your sort function can ensure that the newly inserted item comes ahead in sort order - it should appear first in the group.
_initializeListView: function initializeListView()
{
var data = [{ title: 'parle-ggg', brand: 'parle' }, { title: 'parle-gg', brand: 'parle' }, { title: 'parle-g', brand: 'parle' },
{ title: 'maggi-sauce', brand: 'maggi' }, { title: 'maggi-sauce-2', brand: 'maggi' }, { title: 'maggi-sauce-3', brand: 'maggi' }];
var list = new WinJS.Binding.List(data);
var sortedList = list.createSorted(function sorter(item1, item2)
{
var result;
if (item1.title < item2.title)
result = -1;
else if (item1.title == item2.title)
result = 0;
else
result = 1;
return result;
});
var groups = sortedList.createGrouped(
function groupKey(item)
{
return item.brand;
},
function groupData(item)
{
var result = { title: item.brand };
return result;
}
);
listView.winControl.itemDataSource = groups.dataSource;
listView.winControl.groupDataSource = groups.groups.dataSource;
this.sortedList = sortedList;
},
_oncmdclick: function oncmdclick(event) // this is event handler where you want to add new item
{
this.sortedList.push(newItem);
},
the above code snippet is tries to create a list sorted by title and grouped by brand. on inserting an item later in sortedList - inserted item is placed in the group properly. for example - adding an item by title 'parle-f' will place it first in the parle brand group whereas adding an item by title 'parle-h' will place it last in the parle brand group.
HTH.

Related

How to update a record and update include many records relationship by foreignKey in sequelize use db postgres

I create an API that updates a record associated with a foreign key
if I just put a value to items so I want it to return remove other values that I don't put
if I edit some value in items so I want it to return the value that I edited
if I put value over value of items so I want it to return the old value of items and the value that I put over
example: const record = {id:1,name:"abc",items:[{id:1,name:"abc",recordId:1},{id:2,name:"abcd",recordId:1}]}
const update = await dbRecord.update({id,name},{where: {id: req.params.id},include:[model:'items',id:[1,2]});
You can use Sequelize mixins. Sequelize has special methods that uses the prefix get add set concatenated with the model name.
const update = await dbRecord.update(
{ id, name },
{ where: { id: req.params.id } }
);
//array get from body
ex: array = [
{ id: 1, name: "abc", recordId: 1 },
{ id: 2, name: "abcd", recordId: 1 },
];
const itemId = array.map((arr) => arr.id);
// result = [1,2]
await dbItems.bulkCreate(array, {
updateOnDuplicate: ["name"],
});
for (var i = 0; i < update.items.length; i++) {
const item = update.items[i];
if (!itemId.includes(container.id)) {
await container.destroy();
}
}
so it create update delete in once time.

delete duplicates from a list in dart

hey there i am trying to display all of the options from my database in a dropdown,i have them displaying but i only want one of each result to appear and i cant figure out how to to get ride of the duplicates this is what it looks like when i click on the dropdown
here is the code to pull in the results
void _getFieldsData() {
getUserDetails().then((data) {
final items = jsonDecode(data).cast<Map<String, dynamic>>();
var fieldListData = items.map<User>((json) {
return User.fromJson(json);
}).toSet().toList();
///set list for class
_selectedField = fieldListData[0].series;
_selectedField = fieldListData[0].classs;
setState(() {
for (Map user in items) {
_userDetails.add(User.fromJson(user));
print(_userDetails.length);
//if (_userDetails.classs != userDetail.classs.contains(_selectedText))
}
});
// update widget
setState(() {
_fieldList = fieldListData.toSet().toList();
//print(resultseries);
// print(uniqueCount);
print(_fieldList);
});
});
here is the dropdown
new DropdownButton<String>(
hint: Text("Series"),
// value: null,
items: _fieldList.map((value){
return DropdownMenuItem<String>(
value: value.series,
child: Container(
width: 100,
child: new Text(value.series),
it's not clear exactly what your User class looks like, but im assuming you have multiple fields that do not all have same values, for example, each with a unique id, that's why the following line isn't working in your case:
setState(() {
_fieldList = fieldListData.toSet().toList();
});
i would suggest using List.fold, List.any, and change the line above to check for only .series field, as below:
List initialResultsList = [];
setState(() {
// use fold (similar to .reduce()) to iterate through fieldListData and return the updated filtered 'results'
// list with each iteration:
_fieldList = fieldListData.fold(initialResultsList, (results, currentItem) {
// check to see if currentItem.series already exists in any item of filtered results:
if (!results.any((item) => item.series == currentItem.series)) {
// if not, add it to results:
results.add(currentItem);
}
// return results for next iteration
return results;
});
}

NativeScript - how can I filter an observable array with SearchBar?

Hi I'm trying to filter an observable array of data fetched via a HTTP request on keypress of the SearchBar.
I managed to get the SearchBar property change to work but I can't seem to figure out what I'm doing wrong in the filtering logic.
Ideally I want to update the list as I type in the search term in the SearchBar. I've searched the API on the Telerik site, there wasn't really any examples I could find.
XML
<Page loaded="pageLoaded">
<ActivityIndicator busy="{{ isLoading }}" />
<ActionBar title="People">
</ActionBar>
<GridLayout>
<StackLayout>
<SearchBar id="searchBar" hint="Search for someone"></SearchBar>
<ListView items="{{ peopleList }}" itemTap="showDetail">
<ListView.itemTemplate>
<StackLayout>
<Label text="{{ fullName }}" horiztonalAlignment="left" verticalAlignment="center"></Label>
<Label text="{{ company }}" class="info"></Label>
</StackLayout>
</ListView.itemTemplate>
</ListView>
</StackLayout>
</GridLayout>
</Page>
JS
var frames = require("ui/frame");
var Observable = require("data/observable").Observable;
var PeopleListViewModel = require("../../shared/people-viewModel");
var activityIndicatorModule = require("ui/activity-indicator");
var page;
var userkey;
var peopleList = new PeopleListViewModel([]);
var pageData = new Observable({ peopleList: peopleList });
exports.pageLoaded = function(args) {
page = args.object;
page.bindingContext = pageData;
userkey = userkey || page.navigationContext.userkey;
peopleList.load(userkey); // fetch data from the backend
var searchBar = page.getViewById("searchBar");
searchBar.on("propertyChange", function (args) {
var searchText = args.object.text;
if (searchText === "") {
// NOT SURE WHAT TO DO HERE.
} else {
peopleList.filter(function (element, index, array) {
// DOESN"T WORK PROPERLY
console.log("element: ", JSON.stringify(element));
return element.fullName == searchText;
});
console.log("Text types: ", searchText);
}
});
};
exports.showDetail = function(args) {
var person = peopleList.getItem(args.index);
var navigateEntry = {
moduleName: "views/people/people-detail",
context: { person: person },
animated: false
};
frames.topmost().navigate(navigateEntry);
};
PeopleListViewModel.js
var config = require("./config");
var fetchModule = require("fetch");
var ObservableArray = require("data/observable-array").ObservableArray;
function PeopleListViewModel(people) {
var viewModel = new ObservableArray(people);
viewModel.load = function (userKey) {
return fetchModule.fetch(config.baseUrl + "/api/people/all/" + userKey)
.then(function (response) {
return response.json();
})
.then(function (data) {
data.forEach(function (person) {
viewModel.push(person);
});
}, function (error) {
console.log("Error: ", error);
});
};
viewModel.empty = function () {
while (viewModel.length) {
viewModel.pop();
}
};
return viewModel;
}
function handleErrors(response) {
if (!response.ok) {
console.log("Error occurred");
}
}
module.exports = PeopleListViewModel;
Updated people-list
var frames = require("ui/frame");
var Observable = require("data/observable").Observable;
var ObservableArray = require("data/observable-array").ObservableArray;
var PeopleListViewModel = require("../../shared/people-viewModel");
var activityIndicatorModule = require("ui/activity-indicator");
var page;
var userkey;
var peopleList = new PeopleListViewModel([]);
var pageData = new Observable({ peopleList: peopleList });
var resultList = new ObservableArray([]);
exports.pageLoaded = function(args) {
page = args.object;
page.bindingContext = pageData;
userkey = userkey || page.navigationContext.userkey;
peopleList.load(userkey);
var searchBar = page.getViewById("searchBar");
searchBar.on("propertyChange", function (args) {
var searchText = args.object.text;
if (searchText === "") {
} else {
while (resultList.length > 0) {
resultList.pop();
}
peopleList.forEach(function (element) {
if (element.fullName === searchText) {
resultList.push(element);
}
});
}
});
};
I had the same issue. If you want to filter your data after every character has changed in search-bar you can try my solution.
Definitions
My playerList is your peopleList. This is the data from view-model.
resultList is an array where the data will be pushed.
var observableArrayModule = require("data/observable-array").ObservableArray;
var playerList = new PlayerListViewModel([]);
var resultList = new observableArrayModule([]);
var pageData = new observableModule.Observable({
resultList: resultList,
player: ""
});
Inside expors.loaded()
page = args.object;
searchBar = page.getViewById("search-bar");
page.bindingContext = pageData;
Load Initial Data - inside expors.loaded()
We are loading initial data when user navigates to the screen for the first time. We are also pushing the same data to resultList since we are using {{resultList}} in xml. You can add loadingIndicator while the list is populated.
playerList
.load()
.then(function() {
setTimeout(function() {
playerList.forEach(function (element) {
pageData.resultList.push(element);
});
}, 1000);
})
.catch(function(error) {
dialogsModule.alert({
message: "An error occurred while loading players.",
okButtonText: "OK"
});
});
Clear autofocus - inside expors.loaded()
This is to prevent keyboard from opening on initial screen navigation.
if (searchBar.ios) {
searchBar.ios.endEditing(true);
} else if (searchBar.android) {
searchBar.android.clearFocus();
}
Search data when character has changed - inside expors.loaded()
I am calling filter functionality. Lodash _.debounce function is used to delay looping through resultList array. Without it, the app would loop every time letter is typed. Now we are waiting for user to stop typing to start looping.
searchBar.on('propertyChange', _.debounce(searchList, 500));
searchList Function
This is the actual loop. You can change element.name for your needs.
function searchList(args) {
var searchText = args.object.text;
while(resultList.length > 0) {
resultList.pop();
}
playerList.forEach(function (element) {
if (element.name.toLowerCase().indexOf(searchText) >= 0) {
resultList.push(element);
}
});
}
Hide keyboard if search-bar is cleared - inside exports.loaded()
And finally we want to hide the keyboard if user clears the search-bar.
searchBar.on(searchBarModule.SearchBar.clearEvent, function (args) {
setTimeout(function() {
searchBar.dismissSoftInput();
}, 10);
});
PS
You probably solved your issue, but this could help someone else in the future.
Okay so your problem is a Javascript problem than a NativeScript problem. For the sake of this problem, think of observable arrays as just your ordinary arrays.
In your JS you're creating a new PeopleListViewModel which you're then attaching to the bindingContext via the pageData object. So far so good. Then you're calling the load method on the PeopleListViewModel (It returns a promise which you're not really doing anything with but for this specific problem it doesn't matter).
However, when text is inputed you're not really doing anything. This is your code:
peopleList.filter(function (element, index, array) {
// DOESN"T WORK PROPERLY
console.log("element: ", JSON.stringify(element));
return element.fullName == searchText;
});
peopleList is an instance of PeopleListViewModel which returns an ObservableArray. The ObservableArray does indeed have a method called filter (which works just like filter of a regular array. Check out the NativeScript documentation and Javascript documentation of filter).
What you need to understand here is that filter returns a new array with the filtered results. Doing peopleList.filter() will send that new array into empty space. You want to var yourNewFilteredArray = peopleList.filter(). But you don't really want to redefine the array bound to the binding context, you want to modify the content of it.
Here's an example of how you could do that:
/*
* Attach a new obsersable array to the binding context.
* you can prepopulate it with the data from the
* PeopleListViewModel if you want to
*/
var resultList = new ObservableArray([]);
var pageData = new Observable({ resultList: resultList });
/*
* Then on search/filter you want to modify this new
* array. Here I first remove every item in it and then
* push matching items to it.
*/
searchBar.on("propertyChange", function (args) {
var searchText = args.object.text;
// ...
while(resultList.length > 0) {
resultList.pop();
}
peopleList.forEach(function (element) {
if (element.fullName === searchText) {
resultList.push(element);
}
});
});

How to move items in listbox to its original position

I have a listbox with several listitems, I post one for example:
var listboxChooseColumns = new sap.ui.commons.ListBox("chooseColumns", {
allowMultiSelect: true,
items: [
nameItem,
new sap.ui.core.ListItem({
text: "Functional Location",
test: "lastName"
})
]
})
The listitems from one box can be moved to another one:
var moveAllToTableColumn = new sap.ui.commons.Button({
press: function() {
//var selectedItems = listboxChooseColumns.getSelectedItems();
var selectedItems = listboxChooseColumns.getItems();
for (var i=0; i<selectedItems.length;i++) {
var listItem = selectedItems[i];
listBoxChosenColumns.addItem(listItem);
}
}
});
If I want to bring the item back from the second list to the first one, the item goes to the end of the list, not in its initial position.
var moveFromTableColumn = new sap.ui.commons.Button({
press: function(){
var selectedItems = listBoxChosenColumns.getSelectedItems();
for (var i=0; i<selectedItems.length;i++) {
var listItem = selectedItems[i];
listBoxChosenColumns.removeItem(listItem);
listboxChooseColumns.addItem(listItem);
}
}
});
How can I return item from the second list to the first list into initial position?
Instead of addItem(), consider using insertItem(oItem, iIndex) which allows you to position the item being inserted (e.g. at the top, if you specify 0 for iIndex):
listboxChooseColumns.insertItem(listItem, 0);

Autocomplete with multiple value

I am using autocomplete which returns ID and Value, when i am selecting the item from autocomplete drop down the ID part, i am saving in hidden field, but suppose if item is there but i am not selecting from drop down simply i am writing whole text into autocomplte text box the ID is not saved into hidden field(it should not be) but i want that so please provide some hint to accomplish that.
function AutoComplete(id, url) {
$(item).focus().autocomplete(url, {
dataType: 'json',
parse: function (data) {
var rows = new Array();
for (var i = 0; i < data.length; i++) {
rows[i] = { data: data[i], value: data[i].ID, result: data[i].Name };
}
return rows;
},
formatItem: function (row, i, n) {
return row.Name;
}
}).result(function (evt, data, formatted) {
$(hiddenfield).val(data.ID)
});
}
Try this demo here with Autcomplete with multiple selection.
[http://jsfiddle.net/jcxbgomd/][1]