Dear Support,
I have an sfTextBox defined as:
SfTextBox Multiline="true" style="height: 842px;width:595px" @bind-Value="Phrase"></SfTextBox>
I currently add phrases to the end of the textbox, through a datagrid recordClickHandler3 which is shown below. sometimes instead of adding to the end of the textbox, i need to add to another location of the textbox
-How can i get the location of the Keyboard/mouse cursor. Can you provide a working sample/OR working Code?
thanks
public void RecordClickHandler3(RecordClickEventArgs<Model.TemplateReportDetail> args)
{
//Console.WriteLine(args.RowData.Phrase);
if (args.RowData.Phrase != null)
{
if (Phrase != null)
{
Phrase = Phrase + "\n" + args.RowData.Phrase;
}
else
{
Phrase = Phrase + args.RowData.Phrase;
}
}
}
|
@using Syncfusion.Blazor.Inputs
@inject IJSRuntime JSRuntime
<SfTextBox ID="Default" Placeholder='Enter First name' FloatLabelType='@FloatLabelType.Always'></SfTextBox>
<button class="e-btn apply-limit" @onclick="@Apply">Insert</button>
@code {
//This method inserts the phrase at current cursor position
public async Task Apply()
{
await JSRuntime.InvokeVoidAsync("insertAtCursor", "Default", "!!Hello!!");
}
} |
|
window.insertAtCursor = function (id, myValue) {
var myField = document.getElementById(id);
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA and others
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
} |
hello, thank you for your reply. The code you provided is working But there is problem with saving the added text:
please see below our code
when we save the report,the content that was added dynamically is not saved.
can you please supporrt?
OUR CODE
<SfTextBox ID="Default"Multiline="true" @bind-Value="Phrase"></SfTextBox>
@code {
public string Phrase { get; set; }
public async Task RecordClickHandler3(RecordClickEventArgs<Model.TemplateReportDetail> args)
{
//Console.WriteLine(args.RowData.Phrase);
//
if (args.RowData.Phrase != null)
{
if (Phrase != null)
{
//Phrase = Phrase + "\n" + args.RowData.Phrase;
await JsRuntime.InvokeVoidAsync("insertAtCursor", "Default", "\n"+args.RowData.Phrase);
}
else
{
// Phrase = Phrase + args.RowData.Phrase;
await JsRuntime.InvokeVoidAsync("insertAtCursor", "Default", args.RowData.Phrase);
}
}
}
|
@code {
public string val { get; set; }
//This method inserts the phrase at current cursor position
public async Task Apply()
{
var dotNetReference = DotNetObjectReference.Create(this);
await JSRuntime.InvokeVoidAsync("insertAtCursor", "Default", "\n!!Hello!!", dotNetReference);
}
[JSInvokable]
public async Task UpdateValue(string updatedValue)
{
this.val = updatedValue;
}
} |
|
window.insertAtCursor = function (id, myValue,dotnetref) {
var myField = document.getElementById(id);
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA and others
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
//myField.value = myField.value.substring(0, startPos)
// + myValue
// + myField.value.substring(endPos, myField.value.length);
var value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length);
dotnetref.invokeMethodAsync('UpdateValue', value);
} else {
//myField.value += myValue;
dotnetref.invokeMethodAsync('UpdateValue', myField.value + myValue);
}
} |