获取行行中的索引编辑ajax事件

时间:2015-01-28 13:56:45

标签: javascript jquery jsf primefaces datatable

我有一个可编辑的dataTable(行编辑模式)。当我编辑行并单击勾选以确认新值时,我想使用jqxvalidator验证一些列(其中一些是必需的)。因此,当单击勾选时,validateRule(#{rowIndex});函数应验证刚编辑的行,但传递给此函数的rowIndex值为undefined

有没有其他方法可以获取行索引并将其传递给JavaScript函数?

我的一些xhtml代码:

<p:dataTable id="tblRule" var="item" value="#{bean.ruleList}" binding="#{bean.dtRules}" editable="true"
    editMode="row" rowIndexVar="rowIndex" widgetVar="tblRuleWidget">
    <p:ajax event="rowEdit" listener="#{bean.doEdit}" update="tblRule" onstart="validateRule(#{rowIndex}); " />
    <p:ajax event="rowEditCancel" onstart="hideValidatorMsg();" />

    <p:column>
        <p:rowEditor />
    </p:column>
    <p:column id="colRuleId" headerText="Id" >
        <h:outputText value="#{item.id}" />
    </p:column>
    <p:column id="colRuleCode" headerText="Code">
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{item.code}" />
            </f:facet>
            <f:facet name="input">
                <p:inputText id="inRuleCode" value="#{item.code}" onblur="hideValidatorMsg(); validateRule(#{rowIndex});" />
            </f:facet>
        </p:cellEditor>
    </p:column>
</p:dataTable>

JavaScript的:

function validateRule(rowIdx) {
    var namespace = 'view' + $('[id$=hidNamespace]').val();

    $('#' + namespace + '\\:form1').jqxValidator({
        animation : 'none',
        scroll : false,
        arrow : false,
        rules : [ {
            input : '#' + namespace + '\\:form1\\:tabView\\:tblRule\\:' + rowIdx + '\\:inRuleCode',
            message : 'Required field!',
            action : 'blur',
            position : 'top',
            rule : 'required'
        } ]
    });

    if ($('#' + namespace + '\\:form1').jqxValidator('validateInput', '#' + namespace + '\\:form1\\:tabView\\:tblRule\\:' + rowIdx + '\\:inRuleCode') === false) {
        return false;
    } else {
        return true;
    }
}

2 个答案:

答案 0 :(得分:4)

在您之前的方法中,您将无法获得rowIndex,ajax事件已注册一次,因此rowIndex将不会被传递,就好像它是一个循环或一个按钮的actionListener每一行 !

适用于PrimeFaces 5.2和&lt;

rowIndex作为数据存储在行中,dataTable完全依赖于此方法。

那就是说,基于Widget的源代码,这里是如何获取触发器索引:

PF('dataTableWV').getRowMeta($(event.target).closest('tr')).index

基本上event.target是✓图标,最接近的tr将是行,将此行传递给getRowMeta函数将提取数据属性,其中一个是{{ 1}}。

从PrimeFaces 5.3开始:

index可以在ajax行为扩展的参数中找到。

rowIndex

- 注意:应该在ajax事件的ext.params[0].value 中传递(如问题所示)。

答案 1 :(得分:0)

我认为你可以采用这种方法: -

<p:dataTable binding="#{YourValue}" and so on ...>
    <p:column>
        <p:commandButton ... onclick="return getMyRowIndex(#{YourValue.rowIndex})" />
    </p:column>
</p:dataTable>
相关问题