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

Datepicker template as function

5 Answers 85 Views
Date/Time Pickers
This is a migrated thread and some comments may be shown as answers.
Josetuttu
Top achievements
Rank 1
Josetuttu asked on 23 Feb 2021, 05:46 AM

How can I use javascript function for defining template for datepicker. Only strings are working now for templates. And if I use strings for template the corresponding function used inside the template should be global.

 

$("#datepicker").kendoDatePicker({
                        value: today,
                        dates: birthdays,
                        weekNumber: true,
                        month: {
                            // template for dates in month view
                            content: function (e) { alert(e);},
                            weekNumber:  '<a class="italic">#= data.weekNumber #</a>'
                        },
                        footer: "Today - #=kendo.toString(data, 'd') #",
                    });

5 Answers, 1 is accepted

Sort by
0
Anton Mironov
Telerik team
answered on 25 Feb 2021, 05:14 PM

Hello Josetuttu,

Thank you for the code snippet provided.

In order to achieve the desired behavior, I would recommend building the following syntax for the needed global function handler:

// DatePicker content property for the month:
month: {
        // template for dates in month view
         content: returnContent(),
         weekNumber:  '<a class="italic">#= data.weekNumber #</a>'
},

// Function handler:
function returnContent(){
         return "test"
}
The complete implementation and behavior could be found in the following dojo example:

Give a try to the approach above and let me know if further assistance is needed.

 

Kind Regards,
Anton Mironov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Josetuttu
Top achievements
Rank 1
answered on 26 Feb 2021, 06:09 PM

Hello Anton,

I'm afraid the given solution doesn't take into consideration the parameter "e". Usually, the arg for the function for template contains the current item/context of the template. 

In the example for template https://demos.telerik.com/kendo-ui/datepicker/template , the comparison function is declared globally. If we are able to use function syntax we will be able to use a local method within the context where the datepicker is created. Hope you understood what I mean.
So if I have 2 or more datepickers with in a page in different sections, I can define different comparison methods for each without conflict.

 

0
Tsvetomir
Telerik team
answered on 02 Mar 2021, 03:19 PM

Hi Josetuttu,

My name is Tsvetomir and I will be temporarily covering for my colleague Anton. 

If you would like to pass the data item within the JavaScript function handler, declare the template as follows:

content: "#=returnContent(data)#",

Check out the updated Dojo sample here:

https://dojo.telerik.com/eRoXoyuJ

 

Regards,
Tsvetomir
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Josetuttu
Top achievements
Rank 1
answered on 22 Mar 2021, 09:26 AM

Dear Tsvetomir,

Thanks for the reply. Unfortunately the function returnContent in this case should be defined in the global/window context which is not what I want. I am looking for a solution similar to how we define function as a template in grid>column>template. In that case, I can bind a local function instead of a global function.

 

<div id="grid"></div>
<script>
$("#grid").kendoGrid({
  columns: [ {
    field: "name",
    template: function(dataItem) {
      return "<strong>" + kendo.htmlEncode(dataItem.name) + "</strong>";
    }
  }],
  dataSource: [ { name: "Jane Doe" }, { name: "John Doe" } ]
});
</script>

 

0
Anton Mironov
Telerik team
answered on 25 Mar 2021, 09:01 AM

Hi Josetuttu,

Thank you for the additional details provided.

Talking about the DatePicker the syntax for the content could include a global function that will work per the context of the current instance of the object that is calling it.

In order to use this functionality locally in the component, I would recommend trying the "data" property with the "#=#" syntax. It includes all the needed information:

  • "cssClass"
  • "date"
  • "dateString"
  • "dates" - a collection of all dates
  • "linkClass"
  • "ns"
  • "title"
  • "url"
  • "value"

Here is an example with the syntax above that includes the value parameter:

Find more information about the Kendo UI Templates in the following article:

I hope this information helps.


Kind Regards,
Anton Mironov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Josetuttu
Top achievements
Rank 1
commented on 27 May 2021, 12:05 PM

Dear Anton,

This is the problem I face when I use string templates for datepicker.
https://dojo.telerik.com/OnOVaWIb
As you can see everything is inside a self executing function. As a result the function isInArray is not accessible for the template.
On the other hand if there is a provision for function as template like we have for grid columns, this error will not come.
Anton Mironov
Telerik team
commented on 01 Jun 2021, 08:21 AM

Hi Josetuttu,

Thank you for the dojo example.

I dive deeply into the implementation and assume that there is two nested "document.ready" event handlers. Try to use the "isInArray" function in the global scope and observe the result. Here is an example:

https://dojo.telerik.com/@anton.mironov/ogIYuDAd

 

Kind Regards,
Anton Mironov

Tags
Date/Time Pickers
Asked by
Josetuttu
Top achievements
Rank 1
Answers by
Anton Mironov
Telerik team
Josetuttu
Top achievements
Rank 1
Tsvetomir
Telerik team
Share this question
or