Encode special characters in javascript function argument
I defined an ext js column model like this:
new Ext.grid.ColumnModel({
defaults: {
sortable: true
},
columns: [
{id:'msgId',hidden: true, dataIndex: 'msgId'},
{header: 'info',xtype: 'templatecolumn',tpl: '<a href="#" onCli开发者_开发问答ck = "viewMessage({msgDetails})">View Message Details</a>'}
]
}),
...
..
The function call onClick = "viewMessage({msgDetails})"
fails because msgDetails has got double quotes and special characters which i believe need some kind of encoding before it can be passed around as function argument.
What can be done here?
EDIT: This is the source of msgDetails:
var records = Ext.data.Record.create([{name: 'msgId', type:'string', mapping: 'msgId'},
{name: 'msgDetails',type:'string', mapping: 'msgDetails'}]);
I don't see anything wrong with the single and double quotes there. You shouldn't need to encode them to use them as function arguments.
I think the problem is that the following bit is not valid JavaScript:
viewMessage({msgDetails})
If you have an object literal using { } you have to supply one or more key-value pairs. I'm not sure what you're trying to do there, but presumably what you really mean is:
viewMessage(msgDetails)
// or
viewMessage({msgDetails : 'some details'})
If you show where and how you define msgDetails
I could offer more specific advice.
You should use HTML defined character sets for special characters. For Example
& ampersand &
< less than sign <
> greater than sign >
" the double quote sign "
' single quote '
When msgDetails field is a string, then you should at least quote it:
tpl: '<a href="#" onClick="viewMessage(\'{msgDetails}\')">View Message Details</a>'
But instead of writing fragile inline JavaScript you should simply bind a listener to the cellclick event:
var infoColumnIndex = 1;
grid.on("cellclick", function(grid, rowIndex, colIndex) {
if (colIndex === infoColumnIndex) {
viewMessage(grid.getStore().getAt(rowIndex).get("msgDetails"));
}
});
Or take a look at the ActionColumn.
精彩评论