Magic Suggest - Pre-select multiple items from MVC Model - model-view-controller

I'm looking for a way to populate the Magic Suggest control with multiple values. I am using ASP.NET MVC and would like to set these values based on properties in the model.
Part 1: Magic Suggest Support for Multiple Values? A related question on SOF addresses adding a single value but not multiple. Is this possible?
Part 2: Ideally, I'd like to bind the control the MVC model somehow. If this is not possible, I'd at least like to set the pre-selected values dynamically. I have access the model via razor syntax. Something similar to how Magic Suggest allows you to set the data perhaps.
$(function () {
$('#magicsuggest').magicSuggest({
data: '/controller/GetValuesJson?mealId=#Model.Id',
valueField: 'Id',
displayField: 'Name',
/* The property below allows pre-selection */
/* How can I use Razor and set from my MVC model?* /
value: */ Some Code Here */
});
});
EDIT: Part 2: Attempting to set the value property by any variable seems to fail. I've tried variations of strings, quotes etc. to no avail.
var returnedIds = [1,2]; // Or some variable population
$(function () {
$('#magicsuggest').magicSuggest({
data: '/controller/GetValuesJson?mealId=#Model.Id',
valueField: 'Id',
displayField: 'Name',
value: [returnedIds]
});
});
Solved: Thanks to #karlipoppins posts below and some tinkering.
$(function ()
{
$.ajax({
url: '#Url.Action("GetSomeValuesJson")',
data: { Id: 1},
type: 'post',
success: function (returnedVal)
{
var ms = $('#magicsuggest').magicSuggest(
{
data: '/Meal/GetSomeValuesJson?&Id=#Model.Id',
valueField: 'Id',
displayField: 'Name',
});
ms.setValue(returnedVal);
}
});
});

From what I understand you are trying to set multiple values when the component is loaded up. On your code you are using Id as the value field and Name as the displayField.
This means that magicsuggest is expecting data to be in the form of
[{"Id": 1, "Name": "hello"}, {"Id": 2, "Name": "world"}, {"Id": 3, "Name": "no"}]
Now to set multiple values, you can
1) do that when defining the component in js:
$('#magicsuggest').magicSuggest({
data: '/controller/GetValuesJson?mealId=#Model.Id',
valueField: 'Id',
displayField: 'Name',
value: [1, 2] // select 'hello', 'world'
});
2) or you can also define it directly in your DOM containing field:
<div id="magicsuggest" value="[1,2]"></div>
and then create the component like this:
$('#magicsuggest').magicSuggest({
data: '/controller/GetValuesJson?mealId=#Model.Id',
valueField: 'Id',
displayField: 'Name'
});
See both examples here: http://jsfiddle.net/Kpz6y/
Cheers

Related

Pass additional parameters to handsontable autocomplete source function

I am implementing HandsOnTable autocomplete ajax as documented here: https://docs.handsontable.com/3.0.0/demo-autocomplete.html#strict-ajax
But I would like to pass additional parameter to the autocomplete source function, something like row.id below:
hot3 = new Handsontable(container3, {
data: getCarData(),
colHeaders: ['Car', 'Year', 'Chassis color', 'Bumper color'],
columns: [
{
type: 'autocomplete',
source: function (query, process, row.id) {
$.ajax({
//url: 'php/cars.php', // commented out because our website is hosted as a set of static pages
url: 'scripts/json/autocomplete.json',
dataType: 'json',
data: {
query: query
},
From the documentation, source function only accepts two parameters (query and process), anyone knows how can I pass additional parameters?
You can't add an argument to source function. It does not "accept" parameters, it provides. This function is called internally by Handsontable with predefined arguments.
But you can still access the data you are looking for. Check the context that source function is called with. It's a ColumnSettings object which contains (among others) column and row ids.
Something like:
columns: [
{
type: 'autocomplete',
source: function (query, process) {
var rowId = this.row;
var columnId = this.col;
$.ajax({
... // do whatever you want
});
}
}
]

jqgrid data comes as undefined while grouping

I am using free jqgrid.I am using following code as a simple example.Data comes out be correct without grouping but with grouping it is unable to capture data(placeid) not shown in the row.I understand that on grouping, rowObject does not have placeid but how do I go about resolving the issue.
jsfiddle link: http://jsfiddle.net/johnnash03/6jhqgxw8/5/
var rows = [{
"place": "Kerala",
"placeid": "61",
"code": "kc10",
}]
var cols = ["place","code"];
$("#grid").jqGrid({
datatype: "local",
height: 250,
colNames: cols,
colModel: [{
name: 'place',
}, {
name: 'code',
formatter: function( cellvalue, options, rowObject ) {
return rowObject['placeid']
}
}]
});
for(var i=0;i<rows.length;i++)
$("#grid").jqGrid('addRowData',i+1,rows[i]);
// Data comes correct on commenting following line.
$("#grid").jqGrid( 'groupingGroupBy', 'place');
The main problem in your demo is the usage of the property placeid, which isn't in any columns of the grid. You fills additionally the grid using addRowData method called in the loop. It's the slowest way to fill the grid, which I know. The most important in your case is that the internal data of the grid will be filled only with place, code and id properties. No placeid property will be saved locally. Thus one don't see it during the later groping the grid.
One can solves the problem by usage additionalProperties option, which specify the names of additional properties of input items, which needed be saved in the local data. Adding
additionalProperties: ["placeid"]
option solves the issue. See the modified demo http://jsfiddle.net/OlegKi/6jhqgxw8/8/
I'd strictly recommend you to modify the code to about the following:
var rows = [{
"place": "Kerala",
"placeid": "61",
"code": "kc10",
}];
$("#grid").jqGrid({
data: rows,
colModel: [
{ name: "place" },
{ name: "code",
formatter: function (cellvalue, options) {
return options.rowData.placeid;
}
}
],
grouping: true,
groupingView : {
groupField : ["place"]
},
localReader: { id: "placeid" }
});
Where one can add optionally additionalProperties: ["placeid"] too. See http://jsfiddle.net/OlegKi/6jhqgxw8/9/.
The above code create, fills and group the data at once. It uses localReader: { id: "placeid" } additionally to inform that placeid property has the rowid information (the unique values used as the values of id property of the rows: <tr> elements) instead of default id property.

Extjs - Sending extraparameters using model.save method

I was wondering if there is any way to pass an array of objects as extra param of model.save method ?
For simple types params seem to work.
Ext.define('RightGridModel', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'int'},
{name: 'rightText', type: 'string'},
{name: 'digit',type:'int'}
]
});
var mod = Ext.create('RightGridModel');
mod.set('rightText', 'some text');
mod.save({
url: "Home/Insert",
params: {
par: 'additional parameter'
}
});
However if I want to send and array as an extra param,(of course I changed server side function to be approperiate ) par variable is an empty list. Here is a code I use to send an model and array
mod.save({
url: "Home/Insert",
params: {
par: Ext.encode(array)
}
});
Is there any way to send a model and an array as extra parameter ?? What is the best way to achieve that ?
Check out getParams function of Proxy class. You can just subclass existing proxy classes into your own and use it while communicating with your backend.

ExtJS 4 rendering new grid based on a combobox selection

I have a grid which uses a remote store and remote pagination because I have too many records.The store for the main grid is:
Ext.define('ArD.store.RecordsListStore', {
extend: 'Ext.data.Store',
model: 'ArD.model.RecordsListModel',
autoLoad: true,
autoSync: true,
remoteSort: true,
remoteFilter: true,
pageSize: 15,
proxy: {
type: 'ajax',
actionMethods: 'POST',
api: {
read: g_settings.baseUrl + 'index.php/recordslist/getAll',
destroy: g_settings.baseUrl + 'index.php/recordslist/deleteRecord'
},
reader: {
type: 'json',
root: 'data',
totalProperty: 'totalCount',
successProperty: 'success'
},
writer: {
root: 'data',
writeAllFields: true,
encode: true
}
}
});
then I populate my grid and and it's all fine. But the problem is that I have a combobox which looks like this:
{
xtype: 'combo',
id: 'records_list_form_id',
emptyText: 'Choose Form',
editable: false,
store: 'FilterRecordsByForm',
displayField: 'title',
valueField: 'id',
lastQuery: '',
triggerAction: 'all',
queryMode: 'remote',
typeAhead: false,
width: 200,
listeners: {
select: this._filterRecords
}
}
And when I select something from the combobox there's the function :
_filterRecords: function()
{
var recStore = Ext.getStore('RecordsListStore');
var a = Ext.getCmp('records_list_form_id').getValue( );
var rec = Ext.getStore('FilterRecordsByForm').getAt(a);
console.log(recStore);
},
mostly just trying some things but I can get the ID of the selected element from the combobox and that is where I am.
What I need is having the id to make a new query usign my AJAX api (PHP/SQL backend) and populate the grid with the info related with this id. In my case I have 1:M relations so when I pass the Id i expect M records which I want to render on the place of the old grid.
Thanks
Leron
Use filter() method. Provide information that you need to filter by and store object will automatically request updated info from the server (you already have remoteFilter configured).
Look at Ext.Ajax() to make an on-demand ajax call to the server-side to load up your data and then use Store.loadData() or something like that to re-populate the Grid.

Endless loading of an ajax store with 2 comboboxes

I have two instances of the class AddressPanel on the panel.
Ext.define('AddressPanel', {
extend: 'Ext.tab.Panel',
initComponent: function() {
this.items = [
{
title: 'Stations',
itemId : 'pointStation',
closable: false,
items:[
{
xtype: 'combo',
fieldLabel: 'station',
store: stationStore,
queryMode: 'remote',
displayField: 'name',
valueField: 'id',
editable : false
}
Both of them contain comboboxes that are associated with the same very basic store
var stationStore = Ext.create('Ext.data.Store', {
fields: ['id', 'name'],
proxy: {
type: 'ajax',
url : '/address/stationname'
}
});
I can open the combo from the first instance and choose a station.
Then I can open the combo from the second instance and choose another station.
It works fine.
But when I open the combobox from the first instance of AddressPanel again I get an endless loading.
How can I fix it?
Thank you in advance.
You could add an id to your combobox and when you go from the first instance to the second you can reset your combobox with
Ext.getCmp('id').reset();
I made two copies of the store and set the store config of the the first combo to the first copy of the store and the store config of the second combo to the second copy.
It helps.

Resources