简体   繁体   English

打字稿:从选定的表格行Knockout.js获取点击数据

[英]typescript: get click data from selected Table row Knockout.js

I am new to typescript and Knockout.js and need a little help. 我是Typescript和Knockout.js的新手,需要一点帮助。 I have created a table that has details of students and their sections. 我创建了一个表格,其中包含学生及其所在部分的详细信息。

AllStudentsPageViewModel.ts: AllStudentsPageViewModel.ts:

interface StudentTableRow {
    studentName: string,

    studentUri: string,

    studentSection: StudentSummaryUtility.SimplifiedSection;

    studentSectionUri: string;
}

export default class AllStudentsPageViewModel implements ViewModel {
    public rows: KnockoutObservableArray<StudentTableRow>;

    constructor() {
        this.rows = KnockoutUtility.computedArray(...(logic involving observables)..);
    }
}

AllStudentsPage.cshtml: AllStudentsPage.cshtml:

<script type="text/html" id="AllStudentsPage">
    <div id="all-students-grid" class="vertical-content">
        <div class="all-students-page-title">
            All students
        </div>
        <table id="all-students-datatable-grid" class="row-border hover" width="100%">
            <thead>
                <tr>
                    <th>
                        Student Name
                    </th>
                    <th>
                        Section
                    </th>
                </tr>
            </thead>
            <tbody>
                <!-- ko foreach: rows -->
                <tr>
                    <td>
                        <a data-bind="text: studentName, attr: { href: studentUri }"></a>
                    </td>
                    <td>
                        <a data-bind="attr: { href: studentSectionUri }" class="all-students-section-column">
                            <!-- ko with: studentSection -->
                                <span data-bind="text: text" />
                                <!-- ko with: icon -->
                                <div class="all-students-section-icon" data-bind="visible: SectionA">
                                    @{ Html.RenderPartial("~/Views/Partial/Svg/SectionA.cshtml"); }
                                </div>
                                <div class="all-students-section-icon" data-bind="visible: SectionB">
                                    @{ Html.RenderPartial("~/Views/Partial/Svg/ErrorCircle.cshtml"); }
                                </div>
                                <div class="all-students-section-icon" data-bind="visible: SectionC">
                                    @{ Html.RenderPartial("~/Views/Partial/Svg/SectionC.cshtml"); }
                                </div>
                                <!-- /ko -->
                            <!-- /ko -->
                        </a>
                    </td>
                </tr>
                <!-- /ko -->
            </tbody>
        </table>
    </div>
</script>

The current behavior is such that when a studentName is clicked, it redirects to the studentUri page and when studentSection is clicked, it redirects to the studentSectionUri. 当前的行为是,当单击studentName时,它将重定向到studentUri页面,而当单击studentSection时,它将重定向到studentSectionUri。

I want to add functionality to log the click data. 我想添加功能来记录点击数据。 So I tried this: 所以我尝试了这个:

export default class AllStudentsPageViewModel implements ViewModel {
    public rows: KnockoutObservableArray<StudentTableRow>;

    public selectedItemLogging = function (studentTableRow: StudentTableRow): void {
        const loggingData = {
            studentName: studentTableRow.studentName,
            studentSection: studentTableRow.studentSection
        }

        Logging.trace("RowSelected", loggingData);
    }

    constructor() {
        this.rows = KnockoutUtility.computedArray(...(logic involving observables)..);
    }
}

In the cshtml, I added this function in the html using data-bind like this: 在cshtml中,我使用数据绑定在html中添加了此功能,如下所示:

            ...
            <!-- ko foreach: rows -->
            <tr data-bind="click: $parent.selectedItemLogging">
                <td>
                    <a data-bind="text: studentName, attr: { href: studentUri }"></a>...

There are two issues: 有两个问题:

  1. Clicking the row executes the selectedItemLogging as expected. 单击该行将按预期执行selectedItemLogging。 But it is not redirecting to the clicked uri(either studentUri or studentSectionUri). 但是它不会重定向到单击的uri(studentUri或studentSectionUri)。 How can the code be changed such that the redirect behavior is still the same? 如何更改代码,使重定向行为仍然相同?
  2. Currently I am getting the complete information of the row clicked. 目前,我正在单击该行的完整信息。 Additionally, I want to know whether the studentName has been clicked or whether the studentUri has been clicked to log that information. 另外,我想知道是否单击了studentName或是否单击了studentUri来记录该信息。 How can I get this information? 我如何获得此信息?

Final answer: I customized Jag's answer since I only had to log only when one of the fields is clicked and not anywhere in the row. 最终答案:我自定义了Jag的答案,因为仅在单击了其中一个字段时才需要记录日志,而不必在行中的任何位置记录日志。 Following is the code: 以下是代码:

Functions in AllStudentsPageViewModel class: AllStudentsPageViewModel类中的函数:

public selectedStudentNameLogging = function (studentTableRow: StudentTableRow): boolean {
    const loggingData = {
        studentName: studentTableRow.studentName,
        studentSection: studentTableRow.studentSection,
        selectedField: "studentName"
    }

    Logging.trace("RowSelected", loggingData);

    return true;
}

public selectedStudentSectionLogging = function (studentTableRow: StudentTableRow): boolean {
    const loggingData = {
        studentName: studentTableRow.studentName,
        studentSection: studentTableRow.studentSection,
        selectedField: "studentSection"
    }

    Logging.trace("RowSelected", loggingData);

    return true;
}

Html: HTML:

        <tbody>
            <!-- ko foreach: rows -->
            <tr>
                <td>
                    <a data-bind="text: studentName, attr: { href: studentUri }, click: $parent.selectedStudentNameLogging"></a>
                </td>
                <td>
                    <a data-bind="attr: { href: studentSectionUri }, click: $parent.selectedStudentSectionLogging" class="all-students-section-column">
                        <!-- ko with: studentSection -->
                            <span data-bind="text: text" />
  1. to propagate the event from ko to native , return true from the click function 要将事件从ko传播到native ,请从click函数返回true

  2. click function's second argument is event so use that to determine event.target click函数的第二个参数是event因此使用它来确定event.target

your click function should be like 您的点击功能应该像

public selectedItemLogging = function (studentTableRow: StudentTableRow, event): void {
    const loggingData = {
        studentName: studentTableRow.studentName,
        studentSection: studentTableRow.studentSection
    }
    // use event.target to find out which link got clicked 
    var clickedUri = event.target.href;

    Logging.trace("RowSelected", loggingData);

    // this will propagate the event    
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM