This is a migrated thread and some comments may be shown as answers.

no data template failing when contains apostrophe

4 Answers 1685 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
newKendoUser
Top achievements
Rank 1
newKendoUser asked on 18 May 2018, 03:18 PM

Hello,

I have implemented the kendo UI drop down list in jQuery making use of the no data template too add a new item when the search returns no results.  My no data template is defined as follows:

 

    <script id="noDataTemplate" type="text/x-kendo-tmpl">
        <div>
            <button class="k-button" onclick="addNew('#: instance.filterInput.val() #')">Add</button>
        </div>
    </script>

and the JS function it calls:

 

 function addNew(newItemName) {
           alert(newItemName);

       //logic will go here to add the new item to the drop down list

        }

 

This works perfectly, however if the text typed into the search bar contains an apostrophe the button becomes unresponsive. Is there any way to escape the apostrophe so that the function is still called successfully, and the paramter 'newItemName' will still contain the apostrophe, as the name for the new item can validly contain one.

Many thanks

4 Answers, 1 is accepted

Sort by
0
Ivan Danchev
Telerik team
answered on 22 May 2018, 08:31 AM
Hello,

You can modify the template as follows, in order to prevent the single quote from breaking the template:
'#: instance.filterInput.val().replace("'", "\\'") #'

Here's a dojo example demonstrating the change above in action.

Regards,
Ivan Danchev
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
newKendoUser
Top achievements
Rank 1
answered on 22 May 2018, 10:30 AM

Hi Ivan,

thanks for the reply and  working example. I m slightly confused however. I see your dojo example works perfectly, however when I try to paste into my solution the JavaScript complains about an undetermined string constant. See screen shot attached. I've tried a few variations of escaping characters here to male the replace work however I cant seem to get it working. Any ideas where I might be going wrong?

 

Thanks

0
Accepted
Ivan Danchev
Telerik team
answered on 23 May 2018, 02:04 PM
Hi,

You can change the template as shown below to deal with the exception:
<button class="k-button" onclick="addNew('#: instance.element[0].id #', '#:replaceString(instance.filterInput.val())#')">Add new item</button>

and add the replaceString function, which will do the replacement:
function replaceString(value) {
    return value.replace("'", "\\'");
}



Regards,
Ivan Danchev
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
newKendoUser
Top achievements
Rank 1
answered on 23 May 2018, 02:14 PM
This works a treat, thank you Ivan. 
Tags
DropDownList
Asked by
newKendoUser
Top achievements
Rank 1
Answers by
Ivan Danchev
Telerik team
newKendoUser
Top achievements
Rank 1
Share this question
or